载入中...
搜索中...
未找到
Box3DWorldJoints.cpp
浏览该文件的文档.
1#include <cmath>
2#include "physics/Body3D.h"
3#include "physics/Joint3D.h"
7namespace eve::physics::box3d {
8namespace {
9
10
11void requireFiniteJointParams(const float* values, int count, const char* operation) {
12 for (int i = 0; i < count; ++i) {
13 if (!std::isfinite(values[i])) throw eve::Exception("%s: parameters must be finite", operation);
14 }
15}
16
17void requireDistinctBodies(World3D* world, Body3D* bodyA, Body3D* bodyB, const char* operation) {
18 if (!world || !world->isValid() || !bodyA || !bodyB || !bodyA->isValid() || !bodyB->isValid() ||
19 bodyA->getWorld() != world || bodyB->getWorld() != world || bodyA == bodyB)
20 throw eve::Exception("%s: bodies must be distinct and belong to this world", operation);
21}
22
23b3Vec3 normalizedAxis(float x, float y, float z, const char* operation) {
24 const float length = std::sqrt(x * x + y * y + z * z);
25 if (length <= 1e-8f) throw eve::Exception("%s: axis length must be > 0", operation);
26 return b3Vec3{x / length, y / length, z / length};
27}
28
29
30} // namespace
31
33 float anchorX, float anchorY, float anchorZ,
34 bool collideConnected) {
35 return checked("physics.world3d.newWeldJoint", [&] {
36 auto* bodyA = &bodyARef;
37 auto* bodyB = &bodyBRef;
38
39 requireDistinctBodies(owner_, bodyA, bodyB, "World3D.newWeldJoint");
40 const float values[] = {anchorX, anchorY, anchorZ};
41 requireFiniteJointParams(values, 3, "World3D.newWeldJoint");
42 const b3Pos anchor{anchorX, anchorY, anchorZ};
43 const b3WorldTransform xfA = b3Body_GetTransform(box3d::Access::bodyId(*bodyA));
44 const b3WorldTransform xfB = b3Body_GetTransform(box3d::Access::bodyId(*bodyB));
45 b3WeldJointDef def = b3DefaultWeldJointDef();
46 def.base.bodyIdA = box3d::Access::bodyId(*bodyA);
47 def.base.bodyIdB = box3d::Access::bodyId(*bodyB);
48 def.base.localFrameA.p = b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyA), anchor);
49 def.base.localFrameB.p = b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyB), anchor);
50 def.base.localFrameA.q = b3InvMulQuat(xfA.q, b3Quat_identity);
51 def.base.localFrameB.q = b3InvMulQuat(xfB.q, b3Quat_identity);
52 def.base.collideConnected = collideConnected;
53 const b3JointId id = b3CreateWeldJoint(worldId_, &def);
54
55 if (!b3Joint_IsValid(id))
57 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "Box3D rejected constraint creation"));
59 });
60}
61
63 bool collideConnected) {
64 return checked("physics.world3d.newMotorJoint", [&] {
65 auto* bodyA = &bodyARef;
66 auto* bodyB = &bodyBRef;
67
68 requireDistinctBodies(owner_, bodyA, bodyB, "World3D.newMotorJoint");
69 b3MotorJointDef def = b3DefaultMotorJointDef();
70 def.base.bodyIdA = box3d::Access::bodyId(*bodyA);
71 def.base.bodyIdB = box3d::Access::bodyId(*bodyB);
72 def.base.collideConnected = collideConnected;
73 const b3JointId id = b3CreateMotorJoint(worldId_, &def);
74
75 if (!b3Joint_IsValid(id))
77 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "Box3D rejected constraint creation"));
79 });
80}
81
83 float axisX, float axisY, float axisZ,
84 bool collideConnected) {
85 return checked("physics.world3d.newParallelJoint", [&] {
86 auto* bodyA = &bodyARef;
87 auto* bodyB = &bodyBRef;
88
89 requireDistinctBodies(owner_, bodyA, bodyB, "World3D.newParallelJoint");
90 const float values[] = {axisX, axisY, axisZ};
91 requireFiniteJointParams(values, 3, "World3D.newParallelJoint");
92 const b3Vec3 worldAxis = normalizedAxis(axisX, axisY, axisZ, "World3D.newParallelJoint");
93 b3ParallelJointDef def = b3DefaultParallelJointDef();
94 def.base.bodyIdA = box3d::Access::bodyId(*bodyA);
95 def.base.bodyIdB = box3d::Access::bodyId(*bodyB);
96 def.base.localFrameA.q = b3ComputeQuatBetweenUnitVectors(
97 b3Vec3_axisZ, b3Body_GetLocalVector(box3d::Access::bodyId(*bodyA), worldAxis));
98 def.base.localFrameB.q = b3ComputeQuatBetweenUnitVectors(
99 b3Vec3_axisZ, b3Body_GetLocalVector(box3d::Access::bodyId(*bodyB), worldAxis));
100 def.base.collideConnected = collideConnected;
101 const b3JointId id = b3CreateParallelJoint(worldId_, &def);
102
103 if (!b3Joint_IsValid(id))
105 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "Box3D rejected constraint creation"));
107 });
108}
109
111 return checked("physics.world3d.newFilterJoint", [&] {
112 auto* bodyA = &bodyARef;
113 auto* bodyB = &bodyBRef;
114
115 requireDistinctBodies(owner_, bodyA, bodyB, "World3D.newFilterJoint");
116 b3FilterJointDef def = b3DefaultFilterJointDef();
117 def.base.bodyIdA = box3d::Access::bodyId(*bodyA);
118 def.base.bodyIdB = box3d::Access::bodyId(*bodyB);
119 const b3JointId id = b3CreateFilterJoint(worldId_, &def);
120
121 if (!b3Joint_IsValid(id))
123 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "Box3D rejected constraint creation"));
125 });
126}
127
129 float anchorAX, float anchorAY,
130 float anchorAZ, float anchorBX,
131 float anchorBY, float anchorBZ,
132 float length, bool collideConnected) {
133 return checked("physics.world3d.newDistanceJoint", [&] {
134 auto* bodyA = &bodyARef;
135 auto* bodyB = &bodyBRef;
136
137 if (!isValid() || !bodyA || !bodyB || !bodyA->isValid() || !bodyB->isValid() || bodyA->getWorld() != owner_ ||
138 bodyB->getWorld() != owner_ || bodyA == bodyB)
139 throw eve::Exception("World3D.newDistanceJoint: bodies must be distinct and belong to this world");
140 const float values[] = {anchorAX, anchorAY, anchorAZ, anchorBX, anchorBY, anchorBZ, length};
141 for (float value : values) {
142 if (!std::isfinite(value)) throw eve::Exception("World3D.newDistanceJoint: parameters must be finite");
143 }
144 if (length < 0.f) throw eve::Exception("World3D.newDistanceJoint: length must be >= 0");
145 b3DistanceJointDef def = b3DefaultDistanceJointDef();
146 def.base.bodyIdA = box3d::Access::bodyId(*bodyA);
147 def.base.bodyIdB = box3d::Access::bodyId(*bodyB);
148 def.base.localFrameA.p =
149 b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyA), b3Pos{anchorAX, anchorAY, anchorAZ});
150 def.base.localFrameB.p =
151 b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyB), b3Pos{anchorBX, anchorBY, anchorBZ});
152 def.base.collideConnected = collideConnected;
153 def.length = length;
154 b3JointId id = b3CreateDistanceJoint(worldId_, &def);
155
156 if (!b3Joint_IsValid(id))
158 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "Box3D rejected constraint creation"));
160 });
161}
162
164 float anchorX, float anchorY,
165 float anchorZ, float axisX, float axisY,
166 float axisZ, bool collideConnected) {
167 return checked("physics.world3d.newRevoluteJoint", [&] {
168 auto* bodyA = &bodyARef;
169 auto* bodyB = &bodyBRef;
170
171 if (!isValid() || !bodyA || !bodyB || !bodyA->isValid() || !bodyB->isValid() || bodyA->getWorld() != owner_ ||
172 bodyB->getWorld() != owner_ || bodyA == bodyB)
173 throw eve::Exception("World3D.newRevoluteJoint: bodies must be distinct and belong to this world");
174 const float values[] = {anchorX, anchorY, anchorZ, axisX, axisY, axisZ};
175 for (float value : values) {
176 if (!std::isfinite(value)) throw eve::Exception("World3D.newRevoluteJoint: parameters must be finite");
177 }
178 const float axisLength = std::sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ);
179 if (axisLength <= 1e-8f) throw eve::Exception("World3D.newRevoluteJoint: axis length must be > 0");
180 const b3Vec3 worldAxis{axisX / axisLength, axisY / axisLength, axisZ / axisLength};
181 b3RevoluteJointDef def = b3DefaultRevoluteJointDef();
182 def.base.bodyIdA = box3d::Access::bodyId(*bodyA);
183 def.base.bodyIdB = box3d::Access::bodyId(*bodyB);
184 const b3Pos anchor{anchorX, anchorY, anchorZ};
185 def.base.localFrameA.p = b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyA), anchor);
186 def.base.localFrameB.p = b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyB), anchor);
187 const b3Vec3 localAxisA = b3Body_GetLocalVector(box3d::Access::bodyId(*bodyA), worldAxis);
188 const b3Vec3 localAxisB = b3Body_GetLocalVector(box3d::Access::bodyId(*bodyB), worldAxis);
189 def.base.localFrameA.q = b3ComputeQuatBetweenUnitVectors(b3Vec3_axisZ, localAxisA);
190 def.base.localFrameB.q = b3ComputeQuatBetweenUnitVectors(b3Vec3_axisZ, localAxisB);
191 def.base.collideConnected = collideConnected;
192 b3JointId id = b3CreateRevoluteJoint(worldId_, &def);
193
194 if (!b3Joint_IsValid(id))
196 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "Box3D rejected constraint creation"));
198 });
199}
200
202 float anchorX, float anchorY,
203 float anchorZ, float axisX, float axisY,
204 float axisZ, bool collideConnected) {
205 return checked("physics.world3d.newPrismaticJoint", [&] {
206 auto* bodyA = &bodyARef;
207 auto* bodyB = &bodyBRef;
208
209 if (!isValid() || !bodyA || !bodyB || !bodyA->isValid() || !bodyB->isValid() || bodyA->getWorld() != owner_ ||
210 bodyB->getWorld() != owner_ || bodyA == bodyB)
211 throw eve::Exception("World3D.newPrismaticJoint: bodies must be distinct and belong to this world");
212 const float values[] = {anchorX, anchorY, anchorZ, axisX, axisY, axisZ};
213 for (float value : values) {
214 if (!std::isfinite(value)) throw eve::Exception("World3D.newPrismaticJoint: parameters must be finite");
215 }
216 const float axisLength = std::sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ);
217 if (axisLength <= 1e-8f) throw eve::Exception("World3D.newPrismaticJoint: axis length must be > 0");
218 const b3Vec3 worldAxis{axisX / axisLength, axisY / axisLength, axisZ / axisLength};
219 const b3Pos anchor{anchorX, anchorY, anchorZ};
220 b3PrismaticJointDef def = b3DefaultPrismaticJointDef();
221 def.base.bodyIdA = box3d::Access::bodyId(*bodyA);
222 def.base.bodyIdB = box3d::Access::bodyId(*bodyB);
223 def.base.localFrameA.p = b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyA), anchor);
224 def.base.localFrameB.p = b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyB), anchor);
225 def.base.localFrameA.q = b3ComputeQuatBetweenUnitVectors(
226 b3Vec3_axisX, b3Body_GetLocalVector(box3d::Access::bodyId(*bodyA), worldAxis));
227 def.base.localFrameB.q = b3ComputeQuatBetweenUnitVectors(
228 b3Vec3_axisX, b3Body_GetLocalVector(box3d::Access::bodyId(*bodyB), worldAxis));
229 def.base.collideConnected = collideConnected;
230 const b3JointId id = b3CreatePrismaticJoint(worldId_, &def);
231
232 if (!b3Joint_IsValid(id))
234 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "Box3D rejected constraint creation"));
236 });
237}
238
240 float anchorX, float anchorY,
241 float anchorZ, float axisX, float axisY,
242 float axisZ, bool collideConnected) {
243 return checked("physics.world3d.newSphericalJoint", [&] {
244 auto* bodyA = &bodyARef;
245 auto* bodyB = &bodyBRef;
246
247 if (!isValid() || !bodyA || !bodyB || !bodyA->isValid() || !bodyB->isValid() || bodyA->getWorld() != owner_ ||
248 bodyB->getWorld() != owner_ || bodyA == bodyB)
249 throw eve::Exception("World3D.newSphericalJoint: bodies must be distinct and belong to this world");
250 const float values[] = {anchorX, anchorY, anchorZ, axisX, axisY, axisZ};
251 for (float value : values) {
252 if (!std::isfinite(value)) throw eve::Exception("World3D.newSphericalJoint: parameters must be finite");
253 }
254 const float axisLength = std::sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ);
255 if (axisLength <= 1e-8f) throw eve::Exception("World3D.newSphericalJoint: axis length must be > 0");
256 const b3Vec3 worldAxis{axisX / axisLength, axisY / axisLength, axisZ / axisLength};
257 const b3Pos anchor{anchorX, anchorY, anchorZ};
258 b3SphericalJointDef def = b3DefaultSphericalJointDef();
259 def.base.bodyIdA = box3d::Access::bodyId(*bodyA);
260 def.base.bodyIdB = box3d::Access::bodyId(*bodyB);
261 def.base.localFrameA.p = b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyA), anchor);
262 def.base.localFrameB.p = b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyB), anchor);
263 def.base.localFrameA.q = b3ComputeQuatBetweenUnitVectors(
264 b3Vec3_axisZ, b3Body_GetLocalVector(box3d::Access::bodyId(*bodyA), worldAxis));
265 def.base.localFrameB.q = b3ComputeQuatBetweenUnitVectors(
266 b3Vec3_axisZ, b3Body_GetLocalVector(box3d::Access::bodyId(*bodyB), worldAxis));
267 def.base.collideConnected = collideConnected;
268 const b3JointId id = b3CreateSphericalJoint(worldId_, &def);
269
270 if (!b3Joint_IsValid(id))
272 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "Box3D rejected constraint creation"));
274 });
275}
276
278 float anchorX, float anchorY, float anchorZ,
279 float suspensionAxisX, float suspensionAxisY,
280 float suspensionAxisZ, float wheelAxisX,
281 float wheelAxisY, float wheelAxisZ,
282 bool collideConnected) {
283 return checked("physics.world3d.newWheelJoint", [&] {
284 auto* bodyA = &bodyARef;
285 auto* bodyB = &bodyBRef;
286
287 if (!isValid() || !bodyA || !bodyB || !bodyA->isValid() || !bodyB->isValid() || bodyA->getWorld() != owner_ ||
288 bodyB->getWorld() != owner_ || bodyA == bodyB)
289 throw eve::Exception("World3D.newWheelJoint: bodies must be distinct and belong to this world");
290 const float values[] = {anchorX, anchorY, anchorZ, suspensionAxisX, suspensionAxisY,
291 suspensionAxisZ, wheelAxisX, wheelAxisY, wheelAxisZ};
292 for (float value : values) {
293 if (!std::isfinite(value)) throw eve::Exception("World3D.newWheelJoint: parameters must be finite");
294 }
295 const float suspensionLength = std::sqrt(suspensionAxisX * suspensionAxisX + suspensionAxisY * suspensionAxisY +
296 suspensionAxisZ * suspensionAxisZ);
297 const float wheelLength =
298 std::sqrt(wheelAxisX * wheelAxisX + wheelAxisY * wheelAxisY + wheelAxisZ * wheelAxisZ);
299 if (suspensionLength <= 1e-8f || wheelLength <= 1e-8f)
300 throw eve::Exception("World3D.newWheelJoint: both axis lengths must be > 0");
301 const b3Vec3 suspensionAxis{suspensionAxisX / suspensionLength, suspensionAxisY / suspensionLength,
302 suspensionAxisZ / suspensionLength};
303 const b3Vec3 wheelAxis{wheelAxisX / wheelLength, wheelAxisY / wheelLength, wheelAxisZ / wheelLength};
304 if (std::fabs(b3Dot(suspensionAxis, wheelAxis)) > 0.999f)
305 throw eve::Exception("World3D.newWheelJoint: suspension and wheel axes must not be parallel");
306 const b3Pos anchor{anchorX, anchorY, anchorZ};
307 b3WheelJointDef def = b3DefaultWheelJointDef();
308 def.base.bodyIdA = box3d::Access::bodyId(*bodyA);
309 def.base.bodyIdB = box3d::Access::bodyId(*bodyB);
310 def.base.localFrameA.p = b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyA), anchor);
311 def.base.localFrameB.p = b3Body_GetLocalPoint(box3d::Access::bodyId(*bodyB), anchor);
312 def.base.localFrameA.q = b3ComputeQuatBetweenUnitVectors(
313 b3Vec3_axisX, b3Body_GetLocalVector(box3d::Access::bodyId(*bodyA), suspensionAxis));
314 def.base.localFrameB.q = b3ComputeQuatBetweenUnitVectors(
315 b3Vec3_axisZ, b3Body_GetLocalVector(box3d::Access::bodyId(*bodyB), wheelAxis));
316 def.base.collideConnected = collideConnected;
317 const b3JointId id = b3CreateWheelJoint(worldId_, &def);
318
319 if (!b3Joint_IsValid(id))
321 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "Box3D rejected constraint creation"));
323 });
324}
325
326} // namespace eve::physics::box3d
double z
double y
double x
ActionParameterOperation operation
bool collideConnected
float length
std::set< std::string > values
Object::Ptr value
float axisZ
Definition RockMesh.cpp:22
float axisY
Definition RockMesh.cpp:22
float axisX
Definition RockMesh.cpp:22
int count
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Backend-neutral 3D rigid body in meter-space coordinates (+Y up by convention). Owned by a World3D; c...
Definition Body3D.h:31
eve::Result< std::unique_ptr< IJointBackend3D > > newWeldJoint(Body3D &bodyA, Body3D &bodyB, float anchorX, float anchorY, float anchorZ, bool collideConnected) override
Creates an owning Weld constraint; bodies are synchronous non-owning inputs. @cost One native constra...
eve::Result< std::unique_ptr< IJointBackend3D > > newSphericalJoint(Body3D &bodyA, Body3D &bodyB, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, bool collideConnected) override
Creates an owning Spherical constraint; bodies are synchronous non-owning inputs. @cost One native co...
eve::Result< std::unique_ptr< IJointBackend3D > > newPrismaticJoint(Body3D &bodyA, Body3D &bodyB, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, bool collideConnected) override
Creates an owning Prismatic constraint; bodies are synchronous non-owning inputs. @cost One native co...
bool isValid() const noexcept override
Native world liveness.
eve::Result< std::unique_ptr< IJointBackend3D > > newDistanceJoint(Body3D &bodyA, Body3D &bodyB, float anchorAX, float anchorAY, float anchorAZ, float anchorBX, float anchorBY, float anchorBZ, float length, bool collideConnected) override
Creates an owning Distance constraint; bodies are synchronous non-owning inputs. @cost One native con...
eve::Result< std::unique_ptr< IJointBackend3D > > newFilterJoint(Body3D &bodyA, Body3D &bodyB) override
Creates an owning Filter constraint; bodies are synchronous non-owning inputs. @cost One native const...
eve::Result< std::unique_ptr< IJointBackend3D > > newParallelJoint(Body3D &bodyA, Body3D &bodyB, float axisX, float axisY, float axisZ, bool collideConnected) override
Creates an owning Parallel constraint; bodies are synchronous non-owning inputs. @cost One native con...
eve::Result< std::unique_ptr< IJointBackend3D > > newRevoluteJoint(Body3D &bodyA, Body3D &bodyB, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, bool collideConnected) override
Creates an owning Revolute constraint; bodies are synchronous non-owning inputs. @cost One native con...
eve::Result< std::unique_ptr< IJointBackend3D > > newWheelJoint(Body3D &bodyA, Body3D &bodyB, float anchorX, float anchorY, float anchorZ, float suspensionAxisX, float suspensionAxisY, float suspensionAxisZ, float wheelAxisX, float wheelAxisY, float wheelAxisZ, bool collideConnected) override
Creates an owning Wheel constraint; bodies are synchronous non-owning inputs. @cost One native constr...
eve::Result< std::unique_ptr< IJointBackend3D > > newMotorJoint(Body3D &bodyA, Body3D &bodyB, bool collideConnected) override
Creates an owning Motor constraint; bodies are synchronous non-owning inputs. @cost One native constr...
std::unique_ptr< IJointBackend3D > adoptJoint(b3JointId id, JointKind3D kind)
Adopts a live Box3D constraint; internal native composition boundary, simulation thread only.
static b3BodyId bodyId(const Body3D &body)
Borrows the native identifier synchronously; facade must belong to a Box3D world.