box2d-ruby 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +336 -0
  4. data/examples/contact_events.rb +22 -0
  5. data/examples/debug_draw.rb +15 -0
  6. data/examples/falling_ball.rb +19 -0
  7. data/examples/rugl_debug_draw.rb +68 -0
  8. data/examples/stagecraft_debug_draw.rb +64 -0
  9. data/examples/support/box2d_debug_lines.rb +125 -0
  10. data/ext/box2d/CMakeLists.txt +35 -0
  11. data/ext/box2d/extconf.rb +42 -0
  12. data/ext/box2d/native.c +9 -0
  13. data/ext/box2d/vendor/box2d/LICENSE +21 -0
  14. data/ext/box2d/vendor/box2d/VERSION +1 -0
  15. data/ext/box2d/vendor/box2d/include/box2d/base.h +131 -0
  16. data/ext/box2d/vendor/box2d/include/box2d/box2d.h +1222 -0
  17. data/ext/box2d/vendor/box2d/include/box2d/collision.h +830 -0
  18. data/ext/box2d/vendor/box2d/include/box2d/id.h +144 -0
  19. data/ext/box2d/vendor/box2d/include/box2d/math_functions.h +761 -0
  20. data/ext/box2d/vendor/box2d/include/box2d/types.h +1457 -0
  21. data/ext/box2d/vendor/box2d/src/CMakeLists.txt +223 -0
  22. data/ext/box2d/vendor/box2d/src/aabb.c +132 -0
  23. data/ext/box2d/vendor/box2d/src/aabb.h +56 -0
  24. data/ext/box2d/vendor/box2d/src/arena_allocator.c +112 -0
  25. data/ext/box2d/vendor/box2d/src/arena_allocator.h +48 -0
  26. data/ext/box2d/vendor/box2d/src/array.c +8 -0
  27. data/ext/box2d/vendor/box2d/src/array.h +179 -0
  28. data/ext/box2d/vendor/box2d/src/atomic.h +79 -0
  29. data/ext/box2d/vendor/box2d/src/bitset.c +67 -0
  30. data/ext/box2d/vendor/box2d/src/bitset.h +65 -0
  31. data/ext/box2d/vendor/box2d/src/body.c +1884 -0
  32. data/ext/box2d/vendor/box2d/src/body.h +194 -0
  33. data/ext/box2d/vendor/box2d/src/box2d.natvis +41 -0
  34. data/ext/box2d/vendor/box2d/src/broad_phase.c +524 -0
  35. data/ext/box2d/vendor/box2d/src/broad_phase.h +83 -0
  36. data/ext/box2d/vendor/box2d/src/constants.h +54 -0
  37. data/ext/box2d/vendor/box2d/src/constraint_graph.c +322 -0
  38. data/ext/box2d/vendor/box2d/src/constraint_graph.h +58 -0
  39. data/ext/box2d/vendor/box2d/src/contact.c +650 -0
  40. data/ext/box2d/vendor/box2d/src/contact.h +148 -0
  41. data/ext/box2d/vendor/box2d/src/contact_solver.c +2120 -0
  42. data/ext/box2d/vendor/box2d/src/contact_solver.h +54 -0
  43. data/ext/box2d/vendor/box2d/src/core.c +178 -0
  44. data/ext/box2d/vendor/box2d/src/core.h +149 -0
  45. data/ext/box2d/vendor/box2d/src/ctz.h +112 -0
  46. data/ext/box2d/vendor/box2d/src/distance.c +1415 -0
  47. data/ext/box2d/vendor/box2d/src/distance_joint.c +556 -0
  48. data/ext/box2d/vendor/box2d/src/dynamic_tree.c +1989 -0
  49. data/ext/box2d/vendor/box2d/src/geometry.c +1028 -0
  50. data/ext/box2d/vendor/box2d/src/hull.c +328 -0
  51. data/ext/box2d/vendor/box2d/src/id_pool.c +79 -0
  52. data/ext/box2d/vendor/box2d/src/id_pool.h +35 -0
  53. data/ext/box2d/vendor/box2d/src/island.c +977 -0
  54. data/ext/box2d/vendor/box2d/src/island.h +89 -0
  55. data/ext/box2d/vendor/box2d/src/joint.c +1272 -0
  56. data/ext/box2d/vendor/box2d/src/joint.h +335 -0
  57. data/ext/box2d/vendor/box2d/src/manifold.c +1726 -0
  58. data/ext/box2d/vendor/box2d/src/math_functions.c +159 -0
  59. data/ext/box2d/vendor/box2d/src/motor_joint.c +283 -0
  60. data/ext/box2d/vendor/box2d/src/mouse_joint.c +214 -0
  61. data/ext/box2d/vendor/box2d/src/mover.c +73 -0
  62. data/ext/box2d/vendor/box2d/src/prismatic_joint.c +656 -0
  63. data/ext/box2d/vendor/box2d/src/revolute_joint.c +534 -0
  64. data/ext/box2d/vendor/box2d/src/sensor.c +389 -0
  65. data/ext/box2d/vendor/box2d/src/sensor.h +36 -0
  66. data/ext/box2d/vendor/box2d/src/shape.c +1714 -0
  67. data/ext/box2d/vendor/box2d/src/shape.h +123 -0
  68. data/ext/box2d/vendor/box2d/src/solver.c +2038 -0
  69. data/ext/box2d/vendor/box2d/src/solver.h +155 -0
  70. data/ext/box2d/vendor/box2d/src/solver_set.c +613 -0
  71. data/ext/box2d/vendor/box2d/src/solver_set.h +57 -0
  72. data/ext/box2d/vendor/box2d/src/table.c +238 -0
  73. data/ext/box2d/vendor/box2d/src/table.h +37 -0
  74. data/ext/box2d/vendor/box2d/src/timer.c +185 -0
  75. data/ext/box2d/vendor/box2d/src/types.c +151 -0
  76. data/ext/box2d/vendor/box2d/src/weld_joint.c +310 -0
  77. data/ext/box2d/vendor/box2d/src/wheel_joint.c +551 -0
  78. data/ext/box2d/vendor/box2d/src/world.c +3301 -0
  79. data/ext/box2d/vendor/box2d/src/world.h +192 -0
  80. data/generator/generate.rb +316 -0
  81. data/generator/layout_probe.c +507 -0
  82. data/generator/verify_layouts.rb +32 -0
  83. data/lib/box2d/body.rb +172 -0
  84. data/lib/box2d/body_definition.rb +38 -0
  85. data/lib/box2d/body_shapes.rb +135 -0
  86. data/lib/box2d/chain.rb +61 -0
  87. data/lib/box2d/debug_draw.rb +118 -0
  88. data/lib/box2d/events.rb +103 -0
  89. data/lib/box2d/fixed_stepper.rb +39 -0
  90. data/lib/box2d/handle.rb +46 -0
  91. data/lib/box2d/hit.rb +5 -0
  92. data/lib/box2d/joint.rb +197 -0
  93. data/lib/box2d/native.rb +1462 -0
  94. data/lib/box2d/native_loader.rb +45 -0
  95. data/lib/box2d/pixel_scale.rb +29 -0
  96. data/lib/box2d/shape.rb +109 -0
  97. data/lib/box2d/shape_definition.rb +44 -0
  98. data/lib/box2d/value_conversion.rb +80 -0
  99. data/lib/box2d/version.rb +7 -0
  100. data/lib/box2d/world.rb +135 -0
  101. data/lib/box2d/world_joints.rb +156 -0
  102. data/lib/box2d/world_queries.rb +122 -0
  103. data/lib/box2d/world_registry.rb +95 -0
  104. data/lib/box2d.rb +31 -0
  105. data/script/build_platform_gem.rb +24 -0
  106. data/script/deterministic_scene.rb +56 -0
  107. data/script/update_deterministic_snapshot.rb +11 -0
  108. data/script/verify_platform_gem.rb +24 -0
  109. metadata +164 -0
@@ -0,0 +1,830 @@
1
+ // SPDX-FileCopyrightText: 2023 Erin Catto
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ #pragma once
5
+
6
+ #include "base.h"
7
+ #include "math_functions.h"
8
+
9
+ #include <stdbool.h>
10
+
11
+ typedef struct b2SimplexCache b2SimplexCache;
12
+ typedef struct b2Hull b2Hull;
13
+
14
+ /**
15
+ * @defgroup geometry Geometry
16
+ * @brief Geometry types and algorithms
17
+ *
18
+ * Definitions of circles, capsules, segments, and polygons. Various algorithms to compute hulls, mass properties, and so on.
19
+ * @{
20
+ */
21
+
22
+ /// The maximum number of vertices on a convex polygon. Changing this affects performance even if you
23
+ /// don't use more vertices.
24
+ #define B2_MAX_POLYGON_VERTICES 8
25
+
26
+ /// Low level ray cast input data
27
+ typedef struct b2RayCastInput
28
+ {
29
+ /// Start point of the ray cast
30
+ b2Vec2 origin;
31
+
32
+ /// Translation of the ray cast
33
+ b2Vec2 translation;
34
+
35
+ /// The maximum fraction of the translation to consider, typically 1
36
+ float maxFraction;
37
+ } b2RayCastInput;
38
+
39
+ /// A distance proxy is used by the GJK algorithm. It encapsulates any shape.
40
+ /// You can provide between 1 and B2_MAX_POLYGON_VERTICES and a radius.
41
+ typedef struct b2ShapeProxy
42
+ {
43
+ /// The point cloud
44
+ b2Vec2 points[B2_MAX_POLYGON_VERTICES];
45
+
46
+ /// The number of points. Must be greater than 0.
47
+ int count;
48
+
49
+ /// The external radius of the point cloud. May be zero.
50
+ float radius;
51
+ } b2ShapeProxy;
52
+
53
+ /// Low level shape cast input in generic form. This allows casting an arbitrary point
54
+ /// cloud wrap with a radius. For example, a circle is a single point with a non-zero radius.
55
+ /// A capsule is two points with a non-zero radius. A box is four points with a zero radius.
56
+ typedef struct b2ShapeCastInput
57
+ {
58
+ /// A generic shape
59
+ b2ShapeProxy proxy;
60
+
61
+ /// The translation of the shape cast
62
+ b2Vec2 translation;
63
+
64
+ /// The maximum fraction of the translation to consider, typically 1
65
+ float maxFraction;
66
+
67
+ /// Allow shape cast to encroach when initially touching. This only works if the radius is greater than zero.
68
+ bool canEncroach;
69
+ } b2ShapeCastInput;
70
+
71
+ /// Low level ray cast or shape-cast output data
72
+ typedef struct b2CastOutput
73
+ {
74
+ /// The surface normal at the hit point
75
+ b2Vec2 normal;
76
+
77
+ /// The surface hit point
78
+ b2Vec2 point;
79
+
80
+ /// The fraction of the input translation at collision
81
+ float fraction;
82
+
83
+ /// The number of iterations used
84
+ int iterations;
85
+
86
+ /// Did the cast hit?
87
+ bool hit;
88
+ } b2CastOutput;
89
+
90
+ /// This holds the mass data computed for a shape.
91
+ typedef struct b2MassData
92
+ {
93
+ /// The mass of the shape, usually in kilograms.
94
+ float mass;
95
+
96
+ /// The position of the shape's centroid relative to the shape's origin.
97
+ b2Vec2 center;
98
+
99
+ /// The rotational inertia of the shape about the local origin.
100
+ float rotationalInertia;
101
+ } b2MassData;
102
+
103
+ /// A solid circle
104
+ typedef struct b2Circle
105
+ {
106
+ /// The local center
107
+ b2Vec2 center;
108
+
109
+ /// The radius
110
+ float radius;
111
+ } b2Circle;
112
+
113
+ /// A solid capsule can be viewed as two semicircles connected
114
+ /// by a rectangle.
115
+ typedef struct b2Capsule
116
+ {
117
+ /// Local center of the first semicircle
118
+ b2Vec2 center1;
119
+
120
+ /// Local center of the second semicircle
121
+ b2Vec2 center2;
122
+
123
+ /// The radius of the semicircles
124
+ float radius;
125
+ } b2Capsule;
126
+
127
+ /// A solid convex polygon. It is assumed that the interior of the polygon is to
128
+ /// the left of each edge.
129
+ /// Polygons have a maximum number of vertices equal to B2_MAX_POLYGON_VERTICES.
130
+ /// In most cases you should not need many vertices for a convex polygon.
131
+ /// @warning DO NOT fill this out manually, instead use a helper function like
132
+ /// b2MakePolygon or b2MakeBox.
133
+ typedef struct b2Polygon
134
+ {
135
+ /// The polygon vertices
136
+ b2Vec2 vertices[B2_MAX_POLYGON_VERTICES];
137
+
138
+ /// The outward normal vectors of the polygon sides
139
+ b2Vec2 normals[B2_MAX_POLYGON_VERTICES];
140
+
141
+ /// The centroid of the polygon
142
+ b2Vec2 centroid;
143
+
144
+ /// The external radius for rounded polygons
145
+ float radius;
146
+
147
+ /// The number of polygon vertices
148
+ int count;
149
+ } b2Polygon;
150
+
151
+ /// A line segment with two-sided collision.
152
+ typedef struct b2Segment
153
+ {
154
+ /// The first point
155
+ b2Vec2 point1;
156
+
157
+ /// The second point
158
+ b2Vec2 point2;
159
+ } b2Segment;
160
+
161
+ /// A line segment with one-sided collision. Only collides on the right side.
162
+ /// Several of these are generated for a chain shape.
163
+ /// ghost1 -> point1 -> point2 -> ghost2
164
+ typedef struct b2ChainSegment
165
+ {
166
+ /// The tail ghost vertex
167
+ b2Vec2 ghost1;
168
+
169
+ /// The line segment
170
+ b2Segment segment;
171
+
172
+ /// The head ghost vertex
173
+ b2Vec2 ghost2;
174
+
175
+ /// The owning chain shape index (internal usage only)
176
+ int chainId;
177
+ } b2ChainSegment;
178
+
179
+ /// Validate ray cast input data (NaN, etc)
180
+ B2_API bool b2IsValidRay( const b2RayCastInput* input );
181
+
182
+ /// Make a convex polygon from a convex hull. This will assert if the hull is not valid.
183
+ /// @warning Do not manually fill in the hull data, it must come directly from b2ComputeHull
184
+ B2_API b2Polygon b2MakePolygon( const b2Hull* hull, float radius );
185
+
186
+ /// Make an offset convex polygon from a convex hull. This will assert if the hull is not valid.
187
+ /// @warning Do not manually fill in the hull data, it must come directly from b2ComputeHull
188
+ B2_API b2Polygon b2MakeOffsetPolygon( const b2Hull* hull, b2Vec2 position, b2Rot rotation );
189
+
190
+ /// Make an offset convex polygon from a convex hull. This will assert if the hull is not valid.
191
+ /// @warning Do not manually fill in the hull data, it must come directly from b2ComputeHull
192
+ B2_API b2Polygon b2MakeOffsetRoundedPolygon( const b2Hull* hull, b2Vec2 position, b2Rot rotation, float radius );
193
+
194
+ /// Make a square polygon, bypassing the need for a convex hull.
195
+ /// @param halfWidth the half-width
196
+ B2_API b2Polygon b2MakeSquare( float halfWidth );
197
+
198
+ /// Make a box (rectangle) polygon, bypassing the need for a convex hull.
199
+ /// @param halfWidth the half-width (x-axis)
200
+ /// @param halfHeight the half-height (y-axis)
201
+ B2_API b2Polygon b2MakeBox( float halfWidth, float halfHeight );
202
+
203
+ /// Make a rounded box, bypassing the need for a convex hull.
204
+ /// @param halfWidth the half-width (x-axis)
205
+ /// @param halfHeight the half-height (y-axis)
206
+ /// @param radius the radius of the rounded extension
207
+ B2_API b2Polygon b2MakeRoundedBox( float halfWidth, float halfHeight, float radius );
208
+
209
+ /// Make an offset box, bypassing the need for a convex hull.
210
+ /// @param halfWidth the half-width (x-axis)
211
+ /// @param halfHeight the half-height (y-axis)
212
+ /// @param center the local center of the box
213
+ /// @param rotation the local rotation of the box
214
+ B2_API b2Polygon b2MakeOffsetBox( float halfWidth, float halfHeight, b2Vec2 center, b2Rot rotation );
215
+
216
+ /// Make an offset rounded box, bypassing the need for a convex hull.
217
+ /// @param halfWidth the half-width (x-axis)
218
+ /// @param halfHeight the half-height (y-axis)
219
+ /// @param center the local center of the box
220
+ /// @param rotation the local rotation of the box
221
+ /// @param radius the radius of the rounded extension
222
+ B2_API b2Polygon b2MakeOffsetRoundedBox( float halfWidth, float halfHeight, b2Vec2 center, b2Rot rotation, float radius );
223
+
224
+ /// Transform a polygon. This is useful for transferring a shape from one body to another.
225
+ B2_API b2Polygon b2TransformPolygon( b2Transform transform, const b2Polygon* polygon );
226
+
227
+ /// Compute mass properties of a circle
228
+ B2_API b2MassData b2ComputeCircleMass( const b2Circle* shape, float density );
229
+
230
+ /// Compute mass properties of a capsule
231
+ B2_API b2MassData b2ComputeCapsuleMass( const b2Capsule* shape, float density );
232
+
233
+ /// Compute mass properties of a polygon
234
+ B2_API b2MassData b2ComputePolygonMass( const b2Polygon* shape, float density );
235
+
236
+ /// Compute the bounding box of a transformed circle
237
+ B2_API b2AABB b2ComputeCircleAABB( const b2Circle* shape, b2Transform transform );
238
+
239
+ /// Compute the bounding box of a transformed capsule
240
+ B2_API b2AABB b2ComputeCapsuleAABB( const b2Capsule* shape, b2Transform transform );
241
+
242
+ /// Compute the bounding box of a transformed polygon
243
+ B2_API b2AABB b2ComputePolygonAABB( const b2Polygon* shape, b2Transform transform );
244
+
245
+ /// Compute the bounding box of a transformed line segment
246
+ B2_API b2AABB b2ComputeSegmentAABB( const b2Segment* shape, b2Transform transform );
247
+
248
+ /// Test a point for overlap with a circle in local space
249
+ B2_API bool b2PointInCircle( b2Vec2 point, const b2Circle* shape );
250
+
251
+ /// Test a point for overlap with a capsule in local space
252
+ B2_API bool b2PointInCapsule( b2Vec2 point, const b2Capsule* shape );
253
+
254
+ /// Test a point for overlap with a convex polygon in local space
255
+ B2_API bool b2PointInPolygon( b2Vec2 point, const b2Polygon* shape );
256
+
257
+ /// Ray cast versus circle shape in local space. Initial overlap is treated as a miss.
258
+ B2_API b2CastOutput b2RayCastCircle( const b2RayCastInput* input, const b2Circle* shape );
259
+
260
+ /// Ray cast versus capsule shape in local space. Initial overlap is treated as a miss.
261
+ B2_API b2CastOutput b2RayCastCapsule( const b2RayCastInput* input, const b2Capsule* shape );
262
+
263
+ /// Ray cast versus segment shape in local space. Optionally treat the segment as one-sided with hits from
264
+ /// the left side being treated as a miss.
265
+ B2_API b2CastOutput b2RayCastSegment( const b2RayCastInput* input, const b2Segment* shape, bool oneSided );
266
+
267
+ /// Ray cast versus polygon shape in local space. Initial overlap is treated as a miss.
268
+ B2_API b2CastOutput b2RayCastPolygon( const b2RayCastInput* input, const b2Polygon* shape );
269
+
270
+ /// Shape cast versus a circle. Initial overlap is treated as a miss.
271
+ B2_API b2CastOutput b2ShapeCastCircle( const b2ShapeCastInput* input, const b2Circle* shape );
272
+
273
+ /// Shape cast versus a capsule. Initial overlap is treated as a miss.
274
+ B2_API b2CastOutput b2ShapeCastCapsule( const b2ShapeCastInput* input, const b2Capsule* shape );
275
+
276
+ /// Shape cast versus a line segment. Initial overlap is treated as a miss.
277
+ B2_API b2CastOutput b2ShapeCastSegment( const b2ShapeCastInput* input, const b2Segment* shape );
278
+
279
+ /// Shape cast versus a convex polygon. Initial overlap is treated as a miss.
280
+ B2_API b2CastOutput b2ShapeCastPolygon( const b2ShapeCastInput* input, const b2Polygon* shape );
281
+
282
+ /// A convex hull. Used to create convex polygons.
283
+ /// @warning Do not modify these values directly, instead use b2ComputeHull()
284
+ typedef struct b2Hull
285
+ {
286
+ /// The final points of the hull
287
+ b2Vec2 points[B2_MAX_POLYGON_VERTICES];
288
+
289
+ /// The number of points
290
+ int count;
291
+ } b2Hull;
292
+
293
+ /// Compute the convex hull of a set of points. Returns an empty hull if it fails.
294
+ /// Some failure cases:
295
+ /// - all points very close together
296
+ /// - all points on a line
297
+ /// - less than 3 points
298
+ /// - more than B2_MAX_POLYGON_VERTICES points
299
+ /// This welds close points and removes collinear points.
300
+ /// @warning Do not modify a hull once it has been computed
301
+ B2_API b2Hull b2ComputeHull( const b2Vec2* points, int count );
302
+
303
+ /// This determines if a hull is valid. Checks for:
304
+ /// - convexity
305
+ /// - collinear points
306
+ /// This is expensive and should not be called at runtime.
307
+ B2_API bool b2ValidateHull( const b2Hull* hull );
308
+
309
+ /**@}*/
310
+
311
+ /**
312
+ * @defgroup distance Distance
313
+ * Functions for computing the distance between shapes.
314
+ *
315
+ * These are advanced functions you can use to perform distance calculations. There
316
+ * are functions for computing the closest points between shapes, doing linear shape casts,
317
+ * and doing rotational shape casts. The latter is called time of impact (TOI).
318
+ * @{
319
+ */
320
+
321
+ /// Result of computing the distance between two line segments
322
+ typedef struct b2SegmentDistanceResult
323
+ {
324
+ /// The closest point on the first segment
325
+ b2Vec2 closest1;
326
+
327
+ /// The closest point on the second segment
328
+ b2Vec2 closest2;
329
+
330
+ /// The barycentric coordinate on the first segment
331
+ float fraction1;
332
+
333
+ /// The barycentric coordinate on the second segment
334
+ float fraction2;
335
+
336
+ /// The squared distance between the closest points
337
+ float distanceSquared;
338
+ } b2SegmentDistanceResult;
339
+
340
+ /// Compute the distance between two line segments, clamping at the end points if needed.
341
+ B2_API b2SegmentDistanceResult b2SegmentDistance( b2Vec2 p1, b2Vec2 q1, b2Vec2 p2, b2Vec2 q2 );
342
+
343
+ /// Used to warm start the GJK simplex. If you call this function multiple times with nearby
344
+ /// transforms this might improve performance. Otherwise you can zero initialize this.
345
+ /// The distance cache must be initialized to zero on the first call.
346
+ /// Users should generally just zero initialize this structure for each call.
347
+ typedef struct b2SimplexCache
348
+ {
349
+ /// The number of stored simplex points
350
+ uint16_t count;
351
+
352
+ /// The cached simplex indices on shape A
353
+ uint8_t indexA[3];
354
+
355
+ /// The cached simplex indices on shape B
356
+ uint8_t indexB[3];
357
+ } b2SimplexCache;
358
+
359
+ static const b2SimplexCache b2_emptySimplexCache = B2_ZERO_INIT;
360
+
361
+ /// Input for b2ShapeDistance
362
+ typedef struct b2DistanceInput
363
+ {
364
+ /// The proxy for shape A
365
+ b2ShapeProxy proxyA;
366
+
367
+ /// The proxy for shape B
368
+ b2ShapeProxy proxyB;
369
+
370
+ /// The world transform for shape A
371
+ b2Transform transformA;
372
+
373
+ /// The world transform for shape B
374
+ b2Transform transformB;
375
+
376
+ /// Should the proxy radius be considered?
377
+ bool useRadii;
378
+ } b2DistanceInput;
379
+
380
+ /// Output for b2ShapeDistance
381
+ typedef struct b2DistanceOutput
382
+ {
383
+ b2Vec2 pointA; ///< Closest point on shapeA
384
+ b2Vec2 pointB; ///< Closest point on shapeB
385
+ b2Vec2 normal; ///< Normal vector that points from A to B
386
+ float distance; ///< The final distance, zero if overlapped
387
+ int iterations; ///< Number of GJK iterations used
388
+ int simplexCount; ///< The number of simplexes stored in the simplex array
389
+ } b2DistanceOutput;
390
+
391
+ /// Simplex vertex for debugging the GJK algorithm
392
+ typedef struct b2SimplexVertex
393
+ {
394
+ b2Vec2 wA; ///< support point in proxyA
395
+ b2Vec2 wB; ///< support point in proxyB
396
+ b2Vec2 w; ///< wB - wA
397
+ float a; ///< barycentric coordinate for closest point
398
+ int indexA; ///< wA index
399
+ int indexB; ///< wB index
400
+ } b2SimplexVertex;
401
+
402
+ /// Simplex from the GJK algorithm
403
+ typedef struct b2Simplex
404
+ {
405
+ b2SimplexVertex v1, v2, v3; ///< vertices
406
+ int count; ///< number of valid vertices
407
+ } b2Simplex;
408
+
409
+ /// Compute the closest points between two shapes represented as point clouds.
410
+ /// b2SimplexCache cache is input/output. On the first call set b2SimplexCache.count to zero.
411
+ /// The underlying GJK algorithm may be debugged by passing in debug simplexes and capacity. You may pass in NULL and 0 for these.
412
+ B2_API b2DistanceOutput b2ShapeDistance( const b2DistanceInput* input, b2SimplexCache* cache, b2Simplex* simplexes,
413
+ int simplexCapacity );
414
+
415
+ /// Input parameters for b2ShapeCast
416
+ typedef struct b2ShapeCastPairInput
417
+ {
418
+ b2ShapeProxy proxyA; ///< The proxy for shape A
419
+ b2ShapeProxy proxyB; ///< The proxy for shape B
420
+ b2Transform transformA; ///< The world transform for shape A
421
+ b2Transform transformB; ///< The world transform for shape B
422
+ b2Vec2 translationB; ///< The translation of shape B
423
+ float maxFraction; ///< The fraction of the translation to consider, typically 1
424
+ bool canEncroach; ///< Allows shapes with a radius to move slightly closer if already touching
425
+ } b2ShapeCastPairInput;
426
+
427
+ /// Perform a linear shape cast of shape B moving and shape A fixed. Determines the hit point, normal, and translation fraction.
428
+ /// You may optionally supply an array to hold debug data.
429
+ B2_API b2CastOutput b2ShapeCast( const b2ShapeCastPairInput* input );
430
+
431
+ /// Make a proxy for use in overlap, shape cast, and related functions. This is a deep copy of the points.
432
+ B2_API b2ShapeProxy b2MakeProxy( const b2Vec2* points, int count, float radius );
433
+
434
+ /// Make a proxy with a transform. This is a deep copy of the points.
435
+ B2_API b2ShapeProxy b2MakeOffsetProxy( const b2Vec2* points, int count, float radius, b2Vec2 position, b2Rot rotation );
436
+
437
+ /// This describes the motion of a body/shape for TOI computation. Shapes are defined with respect to the body origin,
438
+ /// which may not coincide with the center of mass. However, to support dynamics we must interpolate the center of mass
439
+ /// position.
440
+ typedef struct b2Sweep
441
+ {
442
+ b2Vec2 localCenter; ///< Local center of mass position
443
+ b2Vec2 c1; ///< Starting center of mass world position
444
+ b2Vec2 c2; ///< Ending center of mass world position
445
+ b2Rot q1; ///< Starting world rotation
446
+ b2Rot q2; ///< Ending world rotation
447
+ } b2Sweep;
448
+
449
+ /// Evaluate the transform sweep at a specific time.
450
+ B2_API b2Transform b2GetSweepTransform( const b2Sweep* sweep, float time );
451
+
452
+ /// Input parameters for b2TimeOfImpact
453
+ typedef struct b2TOIInput
454
+ {
455
+ b2ShapeProxy proxyA; ///< The proxy for shape A
456
+ b2ShapeProxy proxyB; ///< The proxy for shape B
457
+ b2Sweep sweepA; ///< The movement of shape A
458
+ b2Sweep sweepB; ///< The movement of shape B
459
+ float maxFraction; ///< Defines the sweep interval [0, maxFraction]
460
+ } b2TOIInput;
461
+
462
+ /// Describes the TOI output
463
+ typedef enum b2TOIState
464
+ {
465
+ b2_toiStateUnknown,
466
+ b2_toiStateFailed,
467
+ b2_toiStateOverlapped,
468
+ b2_toiStateHit,
469
+ b2_toiStateSeparated
470
+ } b2TOIState;
471
+
472
+ /// Output parameters for b2TimeOfImpact.
473
+ typedef struct b2TOIOutput
474
+ {
475
+ b2TOIState state; ///< The type of result
476
+ float fraction; ///< The sweep time of the collision
477
+ } b2TOIOutput;
478
+
479
+ /// Compute the upper bound on time before two shapes penetrate. Time is represented as
480
+ /// a fraction between [0,tMax]. This uses a swept separating axis and may miss some intermediate,
481
+ /// non-tunneling collisions. If you change the time interval, you should call this function
482
+ /// again.
483
+ B2_API b2TOIOutput b2TimeOfImpact( const b2TOIInput* input );
484
+
485
+ /**@}*/
486
+
487
+ /**
488
+ * @defgroup collision Collision
489
+ * @brief Functions for colliding pairs of shapes
490
+ * @{
491
+ */
492
+
493
+ /// A manifold point is a contact point belonging to a contact manifold.
494
+ /// It holds details related to the geometry and dynamics of the contact points.
495
+ /// Box2D uses speculative collision so some contact points may be separated.
496
+ /// You may use the totalNormalImpulse to determine if there was an interaction during
497
+ /// the time step.
498
+ typedef struct b2ManifoldPoint
499
+ {
500
+ /// Location of the contact point in world space. Subject to precision loss at large coordinates.
501
+ /// @note Should only be used for debugging.
502
+ b2Vec2 point;
503
+
504
+ /// Location of the contact point relative to shapeA's origin in world space
505
+ /// @note When used internally to the Box2D solver, this is relative to the body center of mass.
506
+ b2Vec2 anchorA;
507
+
508
+ /// Location of the contact point relative to shapeB's origin in world space
509
+ /// @note When used internally to the Box2D solver, this is relative to the body center of mass.
510
+ b2Vec2 anchorB;
511
+
512
+ /// The separation of the contact point, negative if penetrating
513
+ float separation;
514
+
515
+ /// The impulse along the manifold normal vector.
516
+ float normalImpulse;
517
+
518
+ /// The friction impulse
519
+ float tangentImpulse;
520
+
521
+ /// The total normal impulse applied across sub-stepping and restitution. This is important
522
+ /// to identify speculative contact points that had an interaction in the time step.
523
+ float totalNormalImpulse;
524
+
525
+ /// Relative normal velocity pre-solve. Used for hit events. If the normal impulse is
526
+ /// zero then there was no hit. Negative means shapes are approaching.
527
+ float normalVelocity;
528
+
529
+ /// Uniquely identifies a contact point between two shapes
530
+ uint16_t id;
531
+
532
+ /// Did this contact point exist the previous step?
533
+ bool persisted;
534
+ } b2ManifoldPoint;
535
+
536
+ /// A contact manifold describes the contact points between colliding shapes.
537
+ /// @note Box2D uses speculative collision so some contact points may be separated.
538
+ typedef struct b2Manifold
539
+ {
540
+ /// The unit normal vector in world space, points from shape A to bodyB
541
+ b2Vec2 normal;
542
+
543
+ /// Angular impulse applied for rolling resistance. N * m * s = kg * m^2 / s
544
+ float rollingImpulse;
545
+
546
+ /// The manifold points, up to two are possible in 2D
547
+ b2ManifoldPoint points[2];
548
+
549
+ /// The number of contacts points, will be 0, 1, or 2
550
+ int pointCount;
551
+
552
+ } b2Manifold;
553
+
554
+ /// Compute the contact manifold between two circles
555
+ B2_API b2Manifold b2CollideCircles( const b2Circle* circleA, b2Transform xfA, const b2Circle* circleB, b2Transform xfB );
556
+
557
+ /// Compute the contact manifold between a capsule and circle
558
+ B2_API b2Manifold b2CollideCapsuleAndCircle( const b2Capsule* capsuleA, b2Transform xfA, const b2Circle* circleB,
559
+ b2Transform xfB );
560
+
561
+ /// Compute the contact manifold between an segment and a circle
562
+ B2_API b2Manifold b2CollideSegmentAndCircle( const b2Segment* segmentA, b2Transform xfA, const b2Circle* circleB,
563
+ b2Transform xfB );
564
+
565
+ /// Compute the contact manifold between a polygon and a circle
566
+ B2_API b2Manifold b2CollidePolygonAndCircle( const b2Polygon* polygonA, b2Transform xfA, const b2Circle* circleB,
567
+ b2Transform xfB );
568
+
569
+ /// Compute the contact manifold between a capsule and circle
570
+ B2_API b2Manifold b2CollideCapsules( const b2Capsule* capsuleA, b2Transform xfA, const b2Capsule* capsuleB, b2Transform xfB );
571
+
572
+ /// Compute the contact manifold between an segment and a capsule
573
+ B2_API b2Manifold b2CollideSegmentAndCapsule( const b2Segment* segmentA, b2Transform xfA, const b2Capsule* capsuleB,
574
+ b2Transform xfB );
575
+
576
+ /// Compute the contact manifold between a polygon and capsule
577
+ B2_API b2Manifold b2CollidePolygonAndCapsule( const b2Polygon* polygonA, b2Transform xfA, const b2Capsule* capsuleB,
578
+ b2Transform xfB );
579
+
580
+ /// Compute the contact manifold between two polygons
581
+ B2_API b2Manifold b2CollidePolygons( const b2Polygon* polygonA, b2Transform xfA, const b2Polygon* polygonB, b2Transform xfB );
582
+
583
+ /// Compute the contact manifold between an segment and a polygon
584
+ B2_API b2Manifold b2CollideSegmentAndPolygon( const b2Segment* segmentA, b2Transform xfA, const b2Polygon* polygonB,
585
+ b2Transform xfB );
586
+
587
+ /// Compute the contact manifold between a chain segment and a circle
588
+ B2_API b2Manifold b2CollideChainSegmentAndCircle( const b2ChainSegment* segmentA, b2Transform xfA, const b2Circle* circleB,
589
+ b2Transform xfB );
590
+
591
+ /// Compute the contact manifold between a chain segment and a capsule
592
+ B2_API b2Manifold b2CollideChainSegmentAndCapsule( const b2ChainSegment* segmentA, b2Transform xfA, const b2Capsule* capsuleB,
593
+ b2Transform xfB, b2SimplexCache* cache );
594
+
595
+ /// Compute the contact manifold between a chain segment and a rounded polygon
596
+ B2_API b2Manifold b2CollideChainSegmentAndPolygon( const b2ChainSegment* segmentA, b2Transform xfA, const b2Polygon* polygonB,
597
+ b2Transform xfB, b2SimplexCache* cache );
598
+
599
+ /**@}*/
600
+
601
+ /**
602
+ * @defgroup tree Dynamic Tree
603
+ * The dynamic tree is a binary AABB tree to organize and query large numbers of geometric objects
604
+ *
605
+ * Box2D uses the dynamic tree internally to sort collision shapes into a binary bounding volume hierarchy.
606
+ * This data structure may have uses in games for organizing other geometry data and may be used independently
607
+ * of Box2D rigid body simulation.
608
+ *
609
+ * A dynamic AABB tree broad-phase, inspired by Nathanael Presson's btDbvt.
610
+ * A dynamic tree arranges data in a binary tree to accelerate
611
+ * queries such as AABB queries and ray casts. Leaf nodes are proxies
612
+ * with an AABB. These are used to hold a user collision object.
613
+ * Nodes are pooled and relocatable, so I use node indices rather than pointers.
614
+ * The dynamic tree is made available for advanced users that would like to use it to organize
615
+ * spatial game data besides rigid bodies.
616
+ * @{
617
+ */
618
+
619
+ /// The dynamic tree structure. This should be considered private data.
620
+ /// It is placed here for performance reasons.
621
+ typedef struct b2DynamicTree
622
+ {
623
+ /// The tree nodes
624
+ struct b2TreeNode* nodes;
625
+
626
+ /// The root index
627
+ int root;
628
+
629
+ /// The number of nodes
630
+ int nodeCount;
631
+
632
+ /// The allocated node space
633
+ int nodeCapacity;
634
+
635
+ /// Node free list
636
+ int freeList;
637
+
638
+ /// Number of proxies created
639
+ int proxyCount;
640
+
641
+ /// Leaf indices for rebuild
642
+ int* leafIndices;
643
+
644
+ /// Leaf bounding boxes for rebuild
645
+ b2AABB* leafBoxes;
646
+
647
+ /// Leaf bounding box centers for rebuild
648
+ b2Vec2* leafCenters;
649
+
650
+ /// Bins for sorting during rebuild
651
+ int* binIndices;
652
+
653
+ /// Allocated space for rebuilding
654
+ int rebuildCapacity;
655
+ } b2DynamicTree;
656
+
657
+ /// These are performance results returned by dynamic tree queries.
658
+ typedef struct b2TreeStats
659
+ {
660
+ /// Number of internal nodes visited during the query
661
+ int nodeVisits;
662
+
663
+ /// Number of leaf nodes visited during the query
664
+ int leafVisits;
665
+ } b2TreeStats;
666
+
667
+ /// Constructing the tree initializes the node pool.
668
+ B2_API b2DynamicTree b2DynamicTree_Create( void );
669
+
670
+ /// Destroy the tree, freeing the node pool.
671
+ B2_API void b2DynamicTree_Destroy( b2DynamicTree* tree );
672
+
673
+ /// Create a proxy. Provide an AABB and a userData value.
674
+ B2_API int b2DynamicTree_CreateProxy( b2DynamicTree* tree, b2AABB aabb, uint64_t categoryBits, uint64_t userData );
675
+
676
+ /// Destroy a proxy. This asserts if the id is invalid.
677
+ B2_API void b2DynamicTree_DestroyProxy( b2DynamicTree* tree, int proxyId );
678
+
679
+ /// Move a proxy to a new AABB by removing and reinserting into the tree.
680
+ B2_API void b2DynamicTree_MoveProxy( b2DynamicTree* tree, int proxyId, b2AABB aabb );
681
+
682
+ /// Enlarge a proxy and enlarge ancestors as necessary.
683
+ B2_API void b2DynamicTree_EnlargeProxy( b2DynamicTree* tree, int proxyId, b2AABB aabb );
684
+
685
+ /// Modify the category bits on a proxy. This is an expensive operation.
686
+ B2_API void b2DynamicTree_SetCategoryBits( b2DynamicTree* tree, int proxyId, uint64_t categoryBits );
687
+
688
+ /// Get the category bits on a proxy.
689
+ B2_API uint64_t b2DynamicTree_GetCategoryBits( b2DynamicTree* tree, int proxyId );
690
+
691
+ /// This function receives proxies found in the AABB query.
692
+ /// @return true if the query should continue
693
+ typedef bool b2TreeQueryCallbackFcn( int proxyId, uint64_t userData, void* context );
694
+
695
+ /// Query an AABB for overlapping proxies. The callback class is called for each proxy that overlaps the supplied AABB.
696
+ /// @return performance data
697
+ B2_API b2TreeStats b2DynamicTree_Query( const b2DynamicTree* tree, b2AABB aabb, uint64_t maskBits,
698
+ b2TreeQueryCallbackFcn* callback, void* context );
699
+
700
+ /// This function receives clipped ray cast input for a proxy. The function
701
+ /// returns the new ray fraction.
702
+ /// - return a value of 0 to terminate the ray cast
703
+ /// - return a value less than input->maxFraction to clip the ray
704
+ /// - return a value of input->maxFraction to continue the ray cast without clipping
705
+ typedef float b2TreeRayCastCallbackFcn( const b2RayCastInput* input, int proxyId, uint64_t userData, void* context );
706
+
707
+ /// Ray cast against the proxies in the tree. This relies on the callback
708
+ /// to perform a exact ray cast in the case were the proxy contains a shape.
709
+ /// The callback also performs the any collision filtering. This has performance
710
+ /// roughly equal to k * log(n), where k is the number of collisions and n is the
711
+ /// number of proxies in the tree.
712
+ /// Bit-wise filtering using mask bits can greatly improve performance in some scenarios.
713
+ /// However, this filtering may be approximate, so the user should still apply filtering to results.
714
+ /// @param tree the dynamic tree to ray cast
715
+ /// @param input the ray cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1)
716
+ /// @param maskBits mask bit hint: `bool accept = (maskBits & node->categoryBits) != 0;`
717
+ /// @param callback a callback class that is called for each proxy that is hit by the ray
718
+ /// @param context user context that is passed to the callback
719
+ /// @return performance data
720
+ B2_API b2TreeStats b2DynamicTree_RayCast( const b2DynamicTree* tree, const b2RayCastInput* input, uint64_t maskBits,
721
+ b2TreeRayCastCallbackFcn* callback, void* context );
722
+
723
+ /// This function receives clipped ray cast input for a proxy. The function
724
+ /// returns the new ray fraction.
725
+ /// - return a value of 0 to terminate the ray cast
726
+ /// - return a value less than input->maxFraction to clip the ray
727
+ /// - return a value of input->maxFraction to continue the ray cast without clipping
728
+ typedef float b2TreeShapeCastCallbackFcn( const b2ShapeCastInput* input, int proxyId, uint64_t userData, void* context );
729
+
730
+ /// Ray cast against the proxies in the tree. This relies on the callback
731
+ /// to perform a exact ray cast in the case were the proxy contains a shape.
732
+ /// The callback also performs the any collision filtering. This has performance
733
+ /// roughly equal to k * log(n), where k is the number of collisions and n is the
734
+ /// number of proxies in the tree.
735
+ /// @param tree the dynamic tree to ray cast
736
+ /// @param input the ray cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
737
+ /// @param maskBits filter bits: `bool accept = (maskBits & node->categoryBits) != 0;`
738
+ /// @param callback a callback class that is called for each proxy that is hit by the shape
739
+ /// @param context user context that is passed to the callback
740
+ /// @return performance data
741
+ B2_API b2TreeStats b2DynamicTree_ShapeCast( const b2DynamicTree* tree, const b2ShapeCastInput* input, uint64_t maskBits,
742
+ b2TreeShapeCastCallbackFcn* callback, void* context );
743
+
744
+ /// Get the height of the binary tree.
745
+ B2_API int b2DynamicTree_GetHeight( const b2DynamicTree* tree );
746
+
747
+ /// Get the ratio of the sum of the node areas to the root area.
748
+ B2_API float b2DynamicTree_GetAreaRatio( const b2DynamicTree* tree );
749
+
750
+ /// Get the bounding box that contains the entire tree
751
+ B2_API b2AABB b2DynamicTree_GetRootBounds( const b2DynamicTree* tree );
752
+
753
+ /// Get the number of proxies created
754
+ B2_API int b2DynamicTree_GetProxyCount( const b2DynamicTree* tree );
755
+
756
+ /// Rebuild the tree while retaining subtrees that haven't changed. Returns the number of boxes sorted.
757
+ B2_API int b2DynamicTree_Rebuild( b2DynamicTree* tree, bool fullBuild );
758
+
759
+ /// Get the number of bytes used by this tree
760
+ B2_API int b2DynamicTree_GetByteCount( const b2DynamicTree* tree );
761
+
762
+ /// Get proxy user data
763
+ B2_API uint64_t b2DynamicTree_GetUserData( const b2DynamicTree* tree, int proxyId );
764
+
765
+ /// Get the AABB of a proxy
766
+ B2_API b2AABB b2DynamicTree_GetAABB( const b2DynamicTree* tree, int proxyId );
767
+
768
+ /// Validate this tree. For testing.
769
+ B2_API void b2DynamicTree_Validate( const b2DynamicTree* tree );
770
+
771
+ /// Validate this tree has no enlarged AABBs. For testing.
772
+ B2_API void b2DynamicTree_ValidateNoEnlarged( const b2DynamicTree* tree );
773
+
774
+ /**@}*/
775
+
776
+ /**
777
+ * @defgroup character Character mover
778
+ * Character movement solver
779
+ * @{
780
+ */
781
+
782
+ /// These are the collision planes returned from b2World_CollideMover
783
+ typedef struct b2PlaneResult
784
+ {
785
+ /// The collision plane between the mover and a convex shape
786
+ b2Plane plane;
787
+
788
+ /// Did the collision register a hit? If not this plane should be ignored.
789
+ bool hit;
790
+ } b2PlaneResult;
791
+
792
+ /// These are collision planes that can be fed to b2SolvePlanes. Normally
793
+ /// this is assembled by the user from plane results in b2PlaneResult
794
+ typedef struct b2CollisionPlane
795
+ {
796
+ /// The collision plane between the mover and some shape
797
+ b2Plane plane;
798
+
799
+ /// Setting this to FLT_MAX makes the plane as rigid as possible. Lower values can
800
+ /// make the plane collision soft. Usually in meters.
801
+ float pushLimit;
802
+
803
+ /// The push on the mover determined by b2SolvePlanes. Usually in meters.
804
+ float push;
805
+
806
+ /// Indicates if b2ClipVector should clip against this plane. Should be false for soft collision.
807
+ bool clipVelocity;
808
+ } b2CollisionPlane;
809
+
810
+ /// Result returned by b2SolvePlanes
811
+ typedef struct b2PlaneSolverResult
812
+ {
813
+ /// The final position of the mover
814
+ b2Vec2 position;
815
+
816
+ /// The number of iterations used by the plane solver. For diagnostics.
817
+ int iterationCount;
818
+ } b2PlaneSolverResult;
819
+
820
+ /// Solves the position of a mover that satisfies the given collision planes.
821
+ /// @param position this must be the position used to generate the collision planes
822
+ /// @param planes the collision planes
823
+ /// @param count the number of collision planes
824
+ B2_API b2PlaneSolverResult b2SolvePlanes( b2Vec2 position, b2CollisionPlane* planes, int count );
825
+
826
+ /// Clips the velocity against the given collision planes. Planes with zero push or clipVelocity
827
+ /// set to false are skipped.
828
+ B2_API b2Vec2 b2ClipVector( b2Vec2 vector, const b2CollisionPlane* planes, int count );
829
+
830
+ /**@}*/