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,194 @@
1
+ // SPDX-FileCopyrightText: 2023 Erin Catto
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ #pragma once
5
+
6
+ #include "array.h"
7
+
8
+ #include "box2d/math_functions.h"
9
+ #include "box2d/types.h"
10
+
11
+ typedef struct b2World b2World;
12
+
13
+ // Body organizational details that are not used in the solver.
14
+ typedef struct b2Body
15
+ {
16
+ char name[32];
17
+
18
+ void* userData;
19
+
20
+ // index of solver set stored in b2World
21
+ // may be B2_NULL_INDEX
22
+ int setIndex;
23
+
24
+ // body sim and state index within set
25
+ // may be B2_NULL_INDEX
26
+ int localIndex;
27
+
28
+ // [31 : contactId | 1 : edgeIndex]
29
+ int headContactKey;
30
+ int contactCount;
31
+
32
+ // todo maybe move this to the body sim
33
+ int headShapeId;
34
+ int shapeCount;
35
+
36
+ int headChainId;
37
+
38
+ // [31 : jointId | 1 : edgeIndex]
39
+ int headJointKey;
40
+ int jointCount;
41
+
42
+ // All enabled dynamic and kinematic bodies are in an island.
43
+ int islandId;
44
+
45
+ // doubly-linked island list
46
+ int islandPrev;
47
+ int islandNext;
48
+
49
+ float mass;
50
+
51
+ // Rotational inertia about the center of mass.
52
+ float inertia;
53
+
54
+ float sleepThreshold;
55
+ float sleepTime;
56
+
57
+ // this is used to adjust the fellAsleep flag in the body move array
58
+ int bodyMoveIndex;
59
+
60
+ int id;
61
+
62
+ b2BodyType type;
63
+
64
+ // This is monotonically advanced when a body is allocated in this slot
65
+ // Used to check for invalid b2BodyId
66
+ uint16_t generation;
67
+
68
+ bool enableSleep;
69
+ bool fixedRotation;
70
+ bool isSpeedCapped;
71
+ bool isMarked;
72
+ } b2Body;
73
+
74
+ // Body State
75
+ // The body state is designed for fast conversion to and from SIMD via scatter-gather.
76
+ // Only awake dynamic and kinematic bodies have a body state.
77
+ // This is used in the performance critical constraint solver
78
+ //
79
+ // The solver operates on the body state. The body state array does not hold static bodies. Static bodies are shared
80
+ // across worker threads. It would be okay to read their states, but writing to them would cause cache thrashing across
81
+ // workers, even if the values don't change.
82
+ // This causes some trouble when computing anchors. I rotate joint anchors using the body rotation every sub-step. For static
83
+ // bodies the anchor doesn't rotate. Body A or B could be static and this can lead to lots of branching. This branching
84
+ // should be minimized.
85
+ //
86
+ // Solution 1:
87
+ // Use delta rotations. This means anchors need to be prepared in world space. The delta rotation for static bodies will be
88
+ // identity using a dummy state. Base separation and angles need to be computed. Manifolds will be behind a frame, but that
89
+ // is probably best if bodies move fast.
90
+ //
91
+ // Solution 2:
92
+ // Use full rotation. The anchors for static bodies will be in world space while the anchors for dynamic bodies will be in local
93
+ // space. Potentially confusing and bug prone.
94
+ //
95
+ // Note:
96
+ // I rotate joint anchors each sub-step but not contact anchors. Joint stability improves a lot by rotating joint anchors
97
+ // according to substep progress. Contacts have reduced stability when anchors are rotated during substeps, especially for
98
+ // round shapes.
99
+
100
+ // 32 bytes
101
+ typedef struct b2BodyState
102
+ {
103
+ b2Vec2 linearVelocity; // 8
104
+ float angularVelocity; // 4
105
+ int flags; // 4
106
+
107
+ // Using delta position reduces round-off error far from the origin
108
+ b2Vec2 deltaPosition; // 8
109
+
110
+ // Using delta rotation because I cannot access the full rotation on static bodies in
111
+ // the solver and must use zero delta rotation for static bodies (c,s) = (1,0)
112
+ b2Rot deltaRotation; // 8
113
+ } b2BodyState;
114
+
115
+ // Identity body state, notice the deltaRotation is {1, 0}
116
+ static const b2BodyState b2_identityBodyState = { { 0.0f, 0.0f }, 0.0f, 0, { 0.0f, 0.0f }, { 1.0f, 0.0f } };
117
+
118
+ // Body simulation data used for integration of position and velocity
119
+ // Transform data used for collision and solver preparation.
120
+ typedef struct b2BodySim
121
+ {
122
+ // todo better to have transform in sim or in base body? Try both!
123
+ // transform for body origin
124
+ b2Transform transform;
125
+
126
+ // center of mass position in world space
127
+ b2Vec2 center;
128
+
129
+ // previous rotation and COM for TOI
130
+ b2Rot rotation0;
131
+ b2Vec2 center0;
132
+
133
+ // location of center of mass relative to the body origin
134
+ b2Vec2 localCenter;
135
+
136
+ b2Vec2 force;
137
+ float torque;
138
+
139
+ // inverse inertia
140
+ float invMass;
141
+ float invInertia;
142
+
143
+ float minExtent;
144
+ float maxExtent;
145
+ float linearDamping;
146
+ float angularDamping;
147
+ float gravityScale;
148
+
149
+ // body data can be moved around, the id is stable (used in b2BodyId)
150
+ int bodyId;
151
+
152
+ // This flag is used for debug draw
153
+ bool isFast;
154
+
155
+ bool isBullet;
156
+ bool isSpeedCapped;
157
+ bool allowFastRotation;
158
+ bool enlargeAABB;
159
+ } b2BodySim;
160
+
161
+ // Get a validated body from a world using an id.
162
+ b2Body* b2GetBodyFullId( b2World* world, b2BodyId bodyId );
163
+
164
+ b2Transform b2GetBodyTransformQuick( b2World* world, b2Body* body );
165
+ b2Transform b2GetBodyTransform( b2World* world, int bodyId );
166
+
167
+ // Create a b2BodyId from a raw id.
168
+ b2BodyId b2MakeBodyId( b2World* world, int bodyId );
169
+
170
+ bool b2ShouldBodiesCollide( b2World* world, b2Body* bodyA, b2Body* bodyB );
171
+
172
+ b2BodySim* b2GetBodySim( b2World* world, b2Body* body );
173
+ b2BodyState* b2GetBodyState( b2World* world, b2Body* body );
174
+
175
+ // careful calling this because it can invalidate body, state, joint, and contact pointers
176
+ bool b2WakeBody( b2World* world, b2Body* body );
177
+
178
+ void b2UpdateBodyMassData( b2World* world, b2Body* body );
179
+
180
+ static inline b2Sweep b2MakeSweep( const b2BodySim* bodySim )
181
+ {
182
+ b2Sweep s;
183
+ s.c1 = bodySim->center0;
184
+ s.c2 = bodySim->center;
185
+ s.q1 = bodySim->rotation0;
186
+ s.q2 = bodySim->transform.q;
187
+ s.localCenter = bodySim->localCenter;
188
+ return s;
189
+ }
190
+
191
+ // Define inline functions for arrays
192
+ B2_ARRAY_INLINE( b2Body, b2Body )
193
+ B2_ARRAY_INLINE( b2BodySim, b2BodySim )
194
+ B2_ARRAY_INLINE( b2BodyState, b2BodyState )
@@ -0,0 +1,41 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
3
+ <Type Name="__m128">
4
+ <DisplayString>[{m128_f32[0]}, {m128_f32[1]}, {m128_f32[2]}, {m128_f32[3]}]</DisplayString>
5
+ <Expand>
6
+ <Item Name="x">m128_f32[0]</Item>
7
+ <Item Name="y">m128_f32[1]</Item>
8
+ <Item Name="z">m128_f32[2]</Item>
9
+ <Item Name="w">m128_f32[3]</Item>
10
+ <Item Name="Address">(void*)this</Item>
11
+ </Expand>
12
+ </Type>
13
+ <Type Name="__m256">
14
+ <DisplayString>[{m256_f32[0]}, {m256_f32[1]}, {m256_f32[2]}, {m256_f32[3]}, {m256_f32[4]}, {m256_f32[5]}, {m256_f32[6]}, {m256_f32[7]}]</DisplayString>
15
+ <Expand>
16
+ <Item Name="x1">m256_f32[0]</Item>
17
+ <Item Name="y1">m256_f32[1]</Item>
18
+ <Item Name="z1">m256_f32[2]</Item>
19
+ <Item Name="w1">m256_f32[3]</Item>
20
+ <Item Name="x2">m256_f32[4]</Item>
21
+ <Item Name="y2">m256_f32[5]</Item>
22
+ <Item Name="z2">m256_f32[6]</Item>
23
+ <Item Name="w2">m256_f32[7]</Item>
24
+ <Item Name="Address">(void*)this</Item>
25
+ </Expand>
26
+ </Type>
27
+
28
+ <Type Name="b2WorldId">
29
+ <DisplayString>index: {index1}</DisplayString>
30
+ <Expand>
31
+ <ExpandedItem>b2_worlds[ index1 - 1 ]</ExpandedItem>
32
+ </Expand>
33
+ </Type>
34
+
35
+ <Type Name="b2BodyId">
36
+ <DisplayString>
37
+ {b2_worlds[world0].bodies.data[index1-1].name,na},
38
+ {b2_worlds[world0].bodies.data[index1-1].type}
39
+ </DisplayString>
40
+ </Type>
41
+ </AutoVisualizer>