box2d 3.0.0
A 2D physics engine for games
 
Loading...
Searching...
No Matches
dynamic_tree.h
1// SPDX-FileCopyrightText: 2023 Erin Catto
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include "box2d/api.h"
7#include "box2d/constants.h"
8#include "box2d/types.h"
9
10#define b2_defaultCategoryBits (0x00000001)
11#define b2_defaultMaskBits (0xFFFFFFFF)
12
15typedef struct b2TreeNode
16{
17 b2AABB aabb; // 16
18
19 // Category bits for collision filtering
20 uint32_t categoryBits; // 4
21
22 union
23 {
24 int32_t parent;
25 int32_t next;
26 }; // 4
27
28 int32_t child1; // 4
29 int32_t child2; // 4
30
31 // todo could be union with child index
32 int32_t userData; // 4
33
34 // leaf = 0, free node = -1
35 int16_t height; // 2
36
37 bool enlarged; // 1
38
39 char pad[9];
41
49typedef struct b2DynamicTree
50{
51 b2TreeNode* nodes;
52
53 int32_t root;
54 int32_t nodeCount;
55 int32_t nodeCapacity;
56 int32_t freeList;
57 int32_t proxyCount;
58
59 int32_t* leafIndices;
60 b2AABB* leafBoxes;
61 b2Vec2* leafCenters;
62 int32_t* binIndices;
63 int32_t rebuildCapacity;
65
67BOX2D_API b2DynamicTree b2DynamicTree_Create(void);
68
70BOX2D_API void b2DynamicTree_Destroy(b2DynamicTree* tree);
71
73BOX2D_API int32_t b2DynamicTree_CreateProxy(b2DynamicTree* tree, b2AABB aabb, uint32_t categoryBits, int32_t userData);
74
76BOX2D_API void b2DynamicTree_DestroyProxy(b2DynamicTree* tree, int32_t proxyId);
77
78// Clone one tree to another, reusing storage in the outTree if possible
79BOX2D_API void b2DynamicTree_Clone(b2DynamicTree* outTree, const b2DynamicTree* inTree);
80
82BOX2D_API void b2DynamicTree_MoveProxy(b2DynamicTree* tree, int32_t proxyId, b2AABB aabb);
83
85BOX2D_API void b2DynamicTree_EnlargeProxy(b2DynamicTree* tree, int32_t proxyId, b2AABB aabb);
86
89typedef bool b2TreeQueryCallbackFcn(int32_t proxyId, int32_t userData, void* context);
90
93BOX2D_API void b2DynamicTree_QueryFiltered(const b2DynamicTree* tree, b2AABB aabb, uint32_t maskBits,
94 b2TreeQueryCallbackFcn* callback, void* context);
95
98BOX2D_API void b2DynamicTree_Query(const b2DynamicTree* tree, b2AABB aabb, b2TreeQueryCallbackFcn* callback, void* context);
99
105typedef float b2TreeRayCastCallbackFcn(const b2RayCastInput* input, int32_t proxyId, int32_t userData, void* context);
106
114BOX2D_API void b2DynamicTree_RayCast(const b2DynamicTree* tree, const b2RayCastInput* input, uint32_t maskBits,
115 b2TreeRayCastCallbackFcn* callback, void* context);
116
122typedef float b2TreeShapeCastCallbackFcn(const b2ShapeCastInput* input, int32_t proxyId, int32_t userData, void* context);
123
131BOX2D_API void b2DynamicTree_ShapeCast(const b2DynamicTree* tree, const b2ShapeCastInput* input, uint32_t maskBits,
132 b2TreeShapeCastCallbackFcn* callback, void* context);
133
135BOX2D_API void b2DynamicTree_Validate(const b2DynamicTree* tree);
136
139BOX2D_API int32_t b2DynamicTree_GetHeight(const b2DynamicTree* tree);
140
142BOX2D_API int32_t b2DynamicTree_GetMaxBalance(const b2DynamicTree* tree);
143
145BOX2D_API float b2DynamicTree_GetAreaRatio(const b2DynamicTree* tree);
146
148BOX2D_API void b2DynamicTree_RebuildBottomUp(b2DynamicTree* tree);
149
151BOX2D_API int32_t b2DynamicTree_GetProxyCount(const b2DynamicTree* tree);
152
154BOX2D_API int32_t b2DynamicTree_Rebuild(b2DynamicTree* tree, bool fullBuild);
155
159BOX2D_API void b2DynamicTree_ShiftOrigin(b2DynamicTree* tree, b2Vec2 newOrigin);
160
163static inline int32_t b2DynamicTree_GetUserData(const b2DynamicTree* tree, int32_t proxyId)
164{
165 return tree->nodes[proxyId].userData;
166}
167
169static inline b2AABB b2DynamicTree_GetAABB(const b2DynamicTree* tree, int32_t proxyId)
170{
171 return tree->nodes[proxyId].aabb;
172}
Definition dynamic_tree.h:50
Definition dynamic_tree.h:16
types used by the Box2D API
Axis-aligned bounding box.
Definition types.h:75
Low level ray-cast input data.
Definition types.h:82
Low level hape cast input in generic form.
Definition types.h:89
Definition types.h:47