载入中...
搜索中...
未找到
Box3DJoint.cpp
浏览该文件的文档.
2#include <box3d/box3d.h>
3#include <cmath>
4#include "common/Exception.h"
5#include "physics/Joint3D.h"
7namespace eve::physics::box3d {
8namespace {
9
10
11void nonNegative(float value, const char* operation, const char* name) {
12 if (!std::isfinite(value) || value < 0.f) throw eve::Exception("%s: %s must be finite and >= 0", operation, name);
13}
14
15void finite(float value, const char* operation, const char* name) {
16 if (!std::isfinite(value)) throw eve::Exception("%s: %s must be finite", operation, name);
17}
18
19
20class Box3DJoint final : public IJointBackend3D {
21 friend struct eve::physics::box3d::Access;
22 template <class F>
23 auto checked(const char* operationName, F&& operation) const -> decltype(operation()) {
24 using R = decltype(operation());
25 if (!isValid())
26 return R::failure(
27 eve::Diagnostic::error(eve::DiagnosticCode::StaleHandle, "Joint is no longer valid", operationName));
28 try {
29 return operation();
30 } catch (const eve::Exception& error) {
31 return R::failure(
33 }
34 }
35
36public:
37 using Kind = JointKind3D;
38 Box3DJoint(b3JointId id, Kind kind) : jointId_(id), kind_(kind) {}
39 ~Box3DJoint() override { destroy(); }
40 void bindOwner(Joint3D& owner) noexcept override {
41 if (isValid()) b3Joint_SetUserData(jointId_, &owner);
42 }
43 bool isValid() const noexcept override { return b3Joint_IsValid(jointId_); }
44 void destroy() noexcept override {
45 if (isValid()) b3DestroyJoint(jointId_, true);
46 jointId_ = {};
47 }
48 void invalidate() noexcept override {
49 if (isValid()) b3Joint_SetUserData(jointId_, nullptr);
50 jointId_ = {};
51 }
52 eve::Result<JointFrames3D> localFrames() const override {
53 return checked("physics.joint3d.localFrames", [&] {
54 const auto a = b3Joint_GetLocalFrameA(jointId_);
55 const auto b = b3Joint_GetLocalFrameB(jointId_);
56 return eve::Result<JointFrames3D>::success({{static_cast<float>(a.p.x), static_cast<float>(a.p.y),
57 static_cast<float>(a.p.z), a.q.v.x, a.q.v.y, a.q.v.z, a.q.s},
58 {static_cast<float>(b.p.x), static_cast<float>(b.p.y),
59 static_cast<float>(b.p.z), b.q.v.x, b.q.v.y, b.q.v.z, b.q.s}});
60 });
61 }
62 eve::Result<void> setCollideConnected(bool collide) override {
63 return checked("physics.joint3d.setCollideConnected", [&] {
64 b3Joint_SetCollideConnected(jointId_, collide);
66 });
67 }
68 eve::Result<bool> getCollideConnected() const override {
69 return checked("physics.joint3d.getCollideConnected", [&] {
70 return eve::Result<bool>::success(isValid() ? b3Joint_GetCollideConnected(jointId_) : false);
71 });
72 }
73 eve::Result<void> setForceThreshold(float threshold) override {
74 return checked("physics.joint3d.setForceThreshold", [&] {
75 nonNegative(threshold, "Joint3D.setForceThreshold", "threshold");
76 b3Joint_SetForceThreshold(jointId_, threshold);
77
79 });
80 }
81 eve::Result<float> getForceThreshold() const override {
82 return checked("physics.joint3d.getForceThreshold", [&] {
83 return eve::Result<float>::success(isValid() ? b3Joint_GetForceThreshold(jointId_) : 0.f);
84 });
85 }
86 eve::Result<void> setTorqueThreshold(float threshold) override {
87 return checked("physics.joint3d.setTorqueThreshold", [&] {
88 nonNegative(threshold, "Joint3D.setTorqueThreshold", "threshold");
89 b3Joint_SetTorqueThreshold(jointId_, threshold);
90
92 });
93 }
94 eve::Result<float> getTorqueThreshold() const override {
95 return checked("physics.joint3d.getTorqueThreshold", [&] {
96 return eve::Result<float>::success(isValid() ? b3Joint_GetTorqueThreshold(jointId_) : 0.f);
97 });
98 }
99
100 eve::Result<float> getConstraintForceX() const override {
101 return checked("physics.joint3d.getConstraintForceX", [&] {
102 return eve::Result<float>::success(isValid() ? b3Joint_GetConstraintForce(jointId_).x : 0.f);
103 });
104 }
105 eve::Result<float> getConstraintForceY() const override {
106 return checked("physics.joint3d.getConstraintForceY", [&] {
107 return eve::Result<float>::success(isValid() ? b3Joint_GetConstraintForce(jointId_).y : 0.f);
108 });
109 }
110 eve::Result<float> getConstraintForceZ() const override {
111 return checked("physics.joint3d.getConstraintForceZ", [&] {
112 return eve::Result<float>::success(isValid() ? b3Joint_GetConstraintForce(jointId_).z : 0.f);
113 });
114 }
115 eve::Result<float> getConstraintTorqueX() const override {
116 return checked("physics.joint3d.getConstraintTorqueX", [&] {
117 return eve::Result<float>::success(isValid() ? b3Joint_GetConstraintTorque(jointId_).x : 0.f);
118 });
119 }
120 eve::Result<float> getConstraintTorqueY() const override {
121 return checked("physics.joint3d.getConstraintTorqueY", [&] {
122 return eve::Result<float>::success(isValid() ? b3Joint_GetConstraintTorque(jointId_).y : 0.f);
123 });
124 }
125 eve::Result<float> getConstraintTorqueZ() const override {
126 return checked("physics.joint3d.getConstraintTorqueZ", [&] {
127 return eve::Result<float>::success(isValid() ? b3Joint_GetConstraintTorque(jointId_).z : 0.f);
128 });
129 }
130 eve::Result<float> getLinearSeparation() const override {
131 return checked("physics.joint3d.getLinearSeparation", [&] {
132 return eve::Result<float>::success(isValid() ? b3Joint_GetLinearSeparation(jointId_) : 0.f);
133 });
134 }
135 eve::Result<float> getAngularSeparation() const override {
136 return checked("physics.joint3d.getAngularSeparation", [&] {
137 return eve::Result<float>::success(isValid() ? b3Joint_GetAngularSeparation(jointId_) : 0.f);
138 });
139 }
141 return checked("physics.joint3d.setDistanceLength", [&] {
142 requireKind(Kind::Distance, "Joint3D.setDistanceLength");
143 nonNegative(length, "Joint3D.setDistanceLength", "length");
144 b3DistanceJoint_SetLength(jointId_, length);
145
147 });
148 }
149 eve::Result<float> getDistanceLength() const override {
150 return checked("physics.joint3d.getDistanceLength", [&] {
151 requireKind(Kind::Distance, "Joint3D.getDistanceLength");
152 return eve::Result<float>::success(b3DistanceJoint_GetLength(jointId_));
153 });
154 }
155 eve::Result<float> getDistanceCurrentLength() const override {
156 return checked("physics.joint3d.getDistanceCurrentLength", [&] {
157 requireKind(Kind::Distance, "Joint3D.getDistanceCurrentLength");
158 return eve::Result<float>::success(b3DistanceJoint_GetCurrentLength(jointId_));
159 });
160 }
161 eve::Result<void> setDistanceSpring(bool enabled, float hertz, float dampingRatio) override {
162 return checked("physics.joint3d.setDistanceSpring", [&] {
163 requireKind(Kind::Distance, "Joint3D.setDistanceSpring");
164 nonNegative(hertz, "Joint3D.setDistanceSpring", "hertz");
165 nonNegative(dampingRatio, "Joint3D.setDistanceSpring", "dampingRatio");
166 b3DistanceJoint_SetSpringHertz(jointId_, hertz);
167 b3DistanceJoint_SetSpringDampingRatio(jointId_, dampingRatio);
168 b3DistanceJoint_EnableSpring(jointId_, enabled);
169
171 });
172 }
173 eve::Result<void> setDistanceLimits(bool enabled, float minimum, float maximum) override {
174 return checked("physics.joint3d.setDistanceLimits", [&] {
175 requireKind(Kind::Distance, "Joint3D.setDistanceLimits");
176 nonNegative(minimum, "Joint3D.setDistanceLimits", "minimum");
177 nonNegative(maximum, "Joint3D.setDistanceLimits", "maximum");
178 if (minimum > maximum) throw eve::Exception("Joint3D.setDistanceLimits: minimum must be <= maximum");
179 b3DistanceJoint_SetLengthRange(jointId_, minimum, maximum);
180 b3DistanceJoint_EnableLimit(jointId_, enabled);
181
183 });
184 }
185 eve::Result<void> setDistanceMotor(bool enabled, float speed, float maxForce) override {
186 return checked("physics.joint3d.setDistanceMotor", [&] {
187 requireKind(Kind::Distance, "Joint3D.setDistanceMotor");
188 finite(speed, "Joint3D.setDistanceMotor", "speed");
189 nonNegative(maxForce, "Joint3D.setDistanceMotor", "maxForce");
190 b3DistanceJoint_SetMotorSpeed(jointId_, speed);
191 b3DistanceJoint_SetMaxMotorForce(jointId_, maxForce);
192 b3DistanceJoint_EnableMotor(jointId_, enabled);
193
195 });
196 }
197 eve::Result<void> setRevoluteSpring(bool enabled, float targetAngle, float hertz, float dampingRatio) override {
198 return checked("physics.joint3d.setRevoluteSpring", [&] {
199 requireKind(Kind::Revolute, "Joint3D.setRevoluteSpring");
200 finite(targetAngle, "Joint3D.setRevoluteSpring", "targetAngle");
201 nonNegative(hertz, "Joint3D.setRevoluteSpring", "hertz");
202 nonNegative(dampingRatio, "Joint3D.setRevoluteSpring", "dampingRatio");
203 b3RevoluteJoint_SetTargetAngle(jointId_, targetAngle);
204 b3RevoluteJoint_SetSpringHertz(jointId_, hertz);
205 b3RevoluteJoint_SetSpringDampingRatio(jointId_, dampingRatio);
206 b3RevoluteJoint_EnableSpring(jointId_, enabled);
207
209 });
210 }
211 eve::Result<void> setRevoluteLimits(bool enabled, float lower, float upper) override {
212 return checked("physics.joint3d.setRevoluteLimits", [&] {
213 requireKind(Kind::Revolute, "Joint3D.setRevoluteLimits");
214 finite(lower, "Joint3D.setRevoluteLimits", "lower");
215 finite(upper, "Joint3D.setRevoluteLimits", "upper");
216 if (lower > upper) throw eve::Exception("Joint3D.setRevoluteLimits: lower must be <= upper");
217 b3RevoluteJoint_SetLimits(jointId_, lower, upper);
218 b3RevoluteJoint_EnableLimit(jointId_, enabled);
219
221 });
222 }
223 eve::Result<void> setRevoluteMotor(bool enabled, float speed, float maxTorque) override {
224 return checked("physics.joint3d.setRevoluteMotor", [&] {
225 requireKind(Kind::Revolute, "Joint3D.setRevoluteMotor");
226 finite(speed, "Joint3D.setRevoluteMotor", "speed");
227 nonNegative(maxTorque, "Joint3D.setRevoluteMotor", "maxTorque");
228 b3RevoluteJoint_SetMotorSpeed(jointId_, speed);
229 b3RevoluteJoint_SetMaxMotorTorque(jointId_, maxTorque);
230 b3RevoluteJoint_EnableMotor(jointId_, enabled);
231
233 });
234 }
235 eve::Result<float> getRevoluteAngle() const override {
236 return checked("physics.joint3d.getRevoluteAngle", [&] {
237 requireKind(Kind::Revolute, "Joint3D.getRevoluteAngle");
238 return eve::Result<float>::success(b3RevoluteJoint_GetAngle(jointId_));
239 });
240 }
241 eve::Result<float> getRevoluteMotorTorque() const override {
242 return checked("physics.joint3d.getRevoluteMotorTorque", [&] {
243 requireKind(Kind::Revolute, "Joint3D.getRevoluteMotorTorque");
244 return eve::Result<float>::success(b3RevoluteJoint_GetMotorTorque(jointId_));
245 });
246 }
247 eve::Result<void> setPrismaticSpring(bool enabled, float targetTranslation, float hertz,
248 float dampingRatio) override {
249 return checked("physics.joint3d.setPrismaticSpring", [&] {
250 requireKind(Kind::Prismatic, "Joint3D.setPrismaticSpring");
251 finite(targetTranslation, "Joint3D.setPrismaticSpring", "targetTranslation");
252 nonNegative(hertz, "Joint3D.setPrismaticSpring", "hertz");
253 nonNegative(dampingRatio, "Joint3D.setPrismaticSpring", "dampingRatio");
254 b3PrismaticJoint_SetTargetTranslation(jointId_, targetTranslation);
255 b3PrismaticJoint_SetSpringHertz(jointId_, hertz);
256 b3PrismaticJoint_SetSpringDampingRatio(jointId_, dampingRatio);
257 b3PrismaticJoint_EnableSpring(jointId_, enabled);
258
260 });
261 }
262 eve::Result<void> setPrismaticLimits(bool enabled, float lower, float upper) override {
263 return checked("physics.joint3d.setPrismaticLimits", [&] {
264 requireKind(Kind::Prismatic, "Joint3D.setPrismaticLimits");
265 finite(lower, "Joint3D.setPrismaticLimits", "lower");
266 finite(upper, "Joint3D.setPrismaticLimits", "upper");
267 if (lower > upper) throw eve::Exception("Joint3D.setPrismaticLimits: lower must be <= upper");
268 b3PrismaticJoint_SetLimits(jointId_, lower, upper);
269 b3PrismaticJoint_EnableLimit(jointId_, enabled);
270
272 });
273 }
274 eve::Result<void> setPrismaticMotor(bool enabled, float speed, float maxForce) override {
275 return checked("physics.joint3d.setPrismaticMotor", [&] {
276 requireKind(Kind::Prismatic, "Joint3D.setPrismaticMotor");
277 finite(speed, "Joint3D.setPrismaticMotor", "speed");
278 nonNegative(maxForce, "Joint3D.setPrismaticMotor", "maxForce");
279 b3PrismaticJoint_SetMotorSpeed(jointId_, speed);
280 b3PrismaticJoint_SetMaxMotorForce(jointId_, maxForce);
281 b3PrismaticJoint_EnableMotor(jointId_, enabled);
282
284 });
285 }
286 eve::Result<float> getPrismaticTranslation() const override {
287 return checked("physics.joint3d.getPrismaticTranslation", [&] {
288 requireKind(Kind::Prismatic, "Joint3D.getPrismaticTranslation");
289 return eve::Result<float>::success(b3PrismaticJoint_GetTranslation(jointId_));
290 });
291 }
292 eve::Result<float> getPrismaticSpeed() const override {
293 return checked("physics.joint3d.getPrismaticSpeed", [&] {
294 requireKind(Kind::Prismatic, "Joint3D.getPrismaticSpeed");
295 return eve::Result<float>::success(b3PrismaticJoint_GetSpeed(jointId_));
296 });
297 }
298 eve::Result<float> getPrismaticMotorForce() const override {
299 return checked("physics.joint3d.getPrismaticMotorForce", [&] {
300 requireKind(Kind::Prismatic, "Joint3D.getPrismaticMotorForce");
301 return eve::Result<float>::success(b3PrismaticJoint_GetMotorForce(jointId_));
302 });
303 }
304 eve::Result<void> setSphericalConeLimit(bool enabled, float angle) override {
305 return checked("physics.joint3d.setSphericalConeLimit", [&] {
306 requireKind(Kind::Spherical, "Joint3D.setSphericalConeLimit");
307 finite(angle, "Joint3D.setSphericalConeLimit", "angle");
308 if (angle < 0.f || angle > B3_PI)
309 throw eve::Exception("Joint3D.setSphericalConeLimit: angle must be in [0, pi]");
310 b3SphericalJoint_SetConeLimit(jointId_, angle);
311 b3SphericalJoint_EnableConeLimit(jointId_, enabled);
312
314 });
315 }
316 eve::Result<void> setSphericalTwistLimits(bool enabled, float lower, float upper) override {
317 return checked("physics.joint3d.setSphericalTwistLimits", [&] {
318 requireKind(Kind::Spherical, "Joint3D.setSphericalTwistLimits");
319 finite(lower, "Joint3D.setSphericalTwistLimits", "lower");
320 finite(upper, "Joint3D.setSphericalTwistLimits", "upper");
321 if (lower > upper || lower < -0.99f * B3_PI || upper > 0.99f * B3_PI)
322 throw eve::Exception("Joint3D.setSphericalTwistLimits: require -0.99*pi <= lower <= upper <= 0.99*pi");
323 b3SphericalJoint_SetTwistLimits(jointId_, lower, upper);
324 b3SphericalJoint_EnableTwistLimit(jointId_, enabled);
325
327 });
328 }
329 eve::Result<void> setSphericalMotor(bool enabled, float velocityX, float velocityY, float velocityZ,
330 float maxTorque) override {
331 return checked("physics.joint3d.setSphericalMotor", [&] {
332 requireKind(Kind::Spherical, "Joint3D.setSphericalMotor");
333 finite(velocityX, "Joint3D.setSphericalMotor", "velocityX");
334 finite(velocityY, "Joint3D.setSphericalMotor", "velocityY");
335 finite(velocityZ, "Joint3D.setSphericalMotor", "velocityZ");
336 nonNegative(maxTorque, "Joint3D.setSphericalMotor", "maxTorque");
337 b3SphericalJoint_SetMotorVelocity(jointId_, {velocityX, velocityY, velocityZ});
338 b3SphericalJoint_SetMaxMotorTorque(jointId_, maxTorque);
339 b3SphericalJoint_EnableMotor(jointId_, enabled);
340
342 });
343 }
344 eve::Result<float> getSphericalConeAngle() const override {
345 return checked("physics.joint3d.getSphericalConeAngle", [&] {
346 requireKind(Kind::Spherical, "Joint3D.getSphericalConeAngle");
347 return eve::Result<float>::success(b3SphericalJoint_GetConeAngle(jointId_));
348 });
349 }
350 eve::Result<float> getSphericalTwistAngle() const override {
351 return checked("physics.joint3d.getSphericalTwistAngle", [&] {
352 requireKind(Kind::Spherical, "Joint3D.getSphericalTwistAngle");
353 return eve::Result<float>::success(b3SphericalJoint_GetTwistAngle(jointId_));
354 });
355 }
356 eve::Result<void> setWheelSuspension(bool enabled, float hertz, float dampingRatio) override {
357 return checked("physics.joint3d.setWheelSuspension", [&] {
358 requireKind(Kind::Wheel, "Joint3D.setWheelSuspension");
359 nonNegative(hertz, "Joint3D.setWheelSuspension", "hertz");
360 nonNegative(dampingRatio, "Joint3D.setWheelSuspension", "dampingRatio");
361 b3WheelJoint_SetSuspensionHertz(jointId_, hertz);
362 b3WheelJoint_SetSuspensionDampingRatio(jointId_, dampingRatio);
363 b3WheelJoint_EnableSuspension(jointId_, enabled);
364
366 });
367 }
368 eve::Result<void> setWheelSuspensionLimits(bool enabled, float lower, float upper) override {
369 return checked("physics.joint3d.setWheelSuspensionLimits", [&] {
370 requireKind(Kind::Wheel, "Joint3D.setWheelSuspensionLimits");
371 finite(lower, "Joint3D.setWheelSuspensionLimits", "lower");
372 finite(upper, "Joint3D.setWheelSuspensionLimits", "upper");
373 if (lower > upper) throw eve::Exception("Joint3D.setWheelSuspensionLimits: lower must be <= upper");
374 b3WheelJoint_SetSuspensionLimits(jointId_, lower, upper);
375 b3WheelJoint_EnableSuspensionLimit(jointId_, enabled);
376
378 });
379 }
380 eve::Result<void> setWheelSpinMotor(bool enabled, float speed, float maxTorque) override {
381 return checked("physics.joint3d.setWheelSpinMotor", [&] {
382 requireKind(Kind::Wheel, "Joint3D.setWheelSpinMotor");
383 finite(speed, "Joint3D.setWheelSpinMotor", "speed");
384 nonNegative(maxTorque, "Joint3D.setWheelSpinMotor", "maxTorque");
385 b3WheelJoint_SetSpinMotorSpeed(jointId_, speed);
386 b3WheelJoint_SetMaxSpinTorque(jointId_, maxTorque);
387 b3WheelJoint_EnableSpinMotor(jointId_, enabled);
388
390 });
391 }
392 eve::Result<void> setWheelSteering(bool enabled, float targetAngle, float hertz, float dampingRatio,
393 float maxTorque) override {
394 return checked("physics.joint3d.setWheelSteering", [&] {
395 requireKind(Kind::Wheel, "Joint3D.setWheelSteering");
396 finite(targetAngle, "Joint3D.setWheelSteering", "targetAngle");
397 nonNegative(hertz, "Joint3D.setWheelSteering", "hertz");
398 nonNegative(dampingRatio, "Joint3D.setWheelSteering", "dampingRatio");
399 nonNegative(maxTorque, "Joint3D.setWheelSteering", "maxTorque");
400 b3WheelJoint_SetTargetSteeringAngle(jointId_, targetAngle);
401 b3WheelJoint_SetSteeringHertz(jointId_, hertz);
402 b3WheelJoint_SetSteeringDampingRatio(jointId_, dampingRatio);
403 b3WheelJoint_SetMaxSteeringTorque(jointId_, maxTorque);
404 b3WheelJoint_EnableSteering(jointId_, enabled);
405
407 });
408 }
409 eve::Result<void> setWheelSteeringLimits(bool enabled, float lower, float upper) override {
410 return checked("physics.joint3d.setWheelSteeringLimits", [&] {
411 requireKind(Kind::Wheel, "Joint3D.setWheelSteeringLimits");
412 finite(lower, "Joint3D.setWheelSteeringLimits", "lower");
413 finite(upper, "Joint3D.setWheelSteeringLimits", "upper");
414 if (lower > upper) throw eve::Exception("Joint3D.setWheelSteeringLimits: lower must be <= upper");
415 b3WheelJoint_SetSteeringLimits(jointId_, lower, upper);
416 b3WheelJoint_EnableSteeringLimit(jointId_, enabled);
417
419 });
420 }
421 eve::Result<float> getWheelSpinSpeed() const override {
422 return checked("physics.joint3d.getWheelSpinSpeed", [&] {
423 requireKind(Kind::Wheel, "Joint3D.getWheelSpinSpeed");
424 return eve::Result<float>::success(b3WheelJoint_GetSpinSpeed(jointId_));
425 });
426 }
427 eve::Result<float> getWheelSpinTorque() const override {
428 return checked("physics.joint3d.getWheelSpinTorque", [&] {
429 requireKind(Kind::Wheel, "Joint3D.getWheelSpinTorque");
430 return eve::Result<float>::success(b3WheelJoint_GetSpinTorque(jointId_));
431 });
432 }
433 eve::Result<float> getWheelSteeringAngle() const override {
434 return checked("physics.joint3d.getWheelSteeringAngle", [&] {
435 requireKind(Kind::Wheel, "Joint3D.getWheelSteeringAngle");
436 return eve::Result<float>::success(b3WheelJoint_GetSteeringAngle(jointId_));
437 });
438 }
439 eve::Result<float> getWheelSteeringTorque() const override {
440 return checked("physics.joint3d.getWheelSteeringTorque", [&] {
441 requireKind(Kind::Wheel, "Joint3D.getWheelSteeringTorque");
442 return eve::Result<float>::success(b3WheelJoint_GetSteeringTorque(jointId_));
443 });
444 }
445 eve::Result<void> setWeldLinearSpring(float hertz, float dampingRatio) override {
446 return checked("physics.joint3d.setWeldLinearSpring", [&] {
447 requireKind(Kind::Weld, "Joint3D.setWeldLinearSpring");
448 nonNegative(hertz, "Joint3D.setWeldLinearSpring", "hertz");
449 nonNegative(dampingRatio, "Joint3D.setWeldLinearSpring", "dampingRatio");
450 b3WeldJoint_SetLinearHertz(jointId_, hertz);
451 b3WeldJoint_SetLinearDampingRatio(jointId_, dampingRatio);
452
454 });
455 }
456 eve::Result<void> setWeldAngularSpring(float hertz, float dampingRatio) override {
457 return checked("physics.joint3d.setWeldAngularSpring", [&] {
458 requireKind(Kind::Weld, "Joint3D.setWeldAngularSpring");
459 nonNegative(hertz, "Joint3D.setWeldAngularSpring", "hertz");
460 nonNegative(dampingRatio, "Joint3D.setWeldAngularSpring", "dampingRatio");
461 b3WeldJoint_SetAngularHertz(jointId_, hertz);
462 b3WeldJoint_SetAngularDampingRatio(jointId_, dampingRatio);
463
465 });
466 }
467 eve::Result<float> getWeldLinearHertz() const override {
468 return checked("physics.joint3d.getWeldLinearHertz", [&] {
469 requireKind(Kind::Weld, "Joint3D.getWeldLinearHertz");
470 return eve::Result<float>::success(b3WeldJoint_GetLinearHertz(jointId_));
471 });
472 }
473 eve::Result<float> getWeldLinearDampingRatio() const override {
474 return checked("physics.joint3d.getWeldLinearDampingRatio", [&] {
475 requireKind(Kind::Weld, "Joint3D.getWeldLinearDampingRatio");
476 return eve::Result<float>::success(b3WeldJoint_GetLinearDampingRatio(jointId_));
477 });
478 }
479 eve::Result<float> getWeldAngularHertz() const override {
480 return checked("physics.joint3d.getWeldAngularHertz", [&] {
481 requireKind(Kind::Weld, "Joint3D.getWeldAngularHertz");
482 return eve::Result<float>::success(b3WeldJoint_GetAngularHertz(jointId_));
483 });
484 }
485 eve::Result<float> getWeldAngularDampingRatio() const override {
486 return checked("physics.joint3d.getWeldAngularDampingRatio", [&] {
487 requireKind(Kind::Weld, "Joint3D.getWeldAngularDampingRatio");
488 return eve::Result<float>::success(b3WeldJoint_GetAngularDampingRatio(jointId_));
489 });
490 }
491 eve::Result<void> setMotorLinearVelocity(float vx, float vy, float vz) override {
492 return checked("physics.joint3d.setMotorLinearVelocity", [&] {
493 requireKind(Kind::Motor, "Joint3D.setMotorLinearVelocity");
494 finite(vx, "Joint3D.setMotorLinearVelocity", "vx");
495 finite(vy, "Joint3D.setMotorLinearVelocity", "vy");
496 finite(vz, "Joint3D.setMotorLinearVelocity", "vz");
497 b3MotorJoint_SetLinearVelocity(jointId_, b3Vec3{vx, vy, vz});
498
500 });
501 }
502 eve::Result<void> setMotorAngularVelocity(float wx, float wy, float wz) override {
503 return checked("physics.joint3d.setMotorAngularVelocity", [&] {
504 requireKind(Kind::Motor, "Joint3D.setMotorAngularVelocity");
505 finite(wx, "Joint3D.setMotorAngularVelocity", "wx");
506 finite(wy, "Joint3D.setMotorAngularVelocity", "wy");
507 finite(wz, "Joint3D.setMotorAngularVelocity", "wz");
508 b3MotorJoint_SetAngularVelocity(jointId_, b3Vec3{wx, wy, wz});
509
511 });
512 }
513 eve::Result<void> setMotorVelocityLimits(float maxForce, float maxTorque) override {
514 return checked("physics.joint3d.setMotorVelocityLimits", [&] {
515 requireKind(Kind::Motor, "Joint3D.setMotorVelocityLimits");
516 nonNegative(maxForce, "Joint3D.setMotorVelocityLimits", "maxForce");
517 nonNegative(maxTorque, "Joint3D.setMotorVelocityLimits", "maxTorque");
518 b3MotorJoint_SetMaxVelocityForce(jointId_, maxForce);
519 b3MotorJoint_SetMaxVelocityTorque(jointId_, maxTorque);
520
522 });
523 }
524 eve::Result<void> setMotorLinearSpring(float hertz, float dampingRatio, float maxForce) override {
525 return checked("physics.joint3d.setMotorLinearSpring", [&] {
526 requireKind(Kind::Motor, "Joint3D.setMotorLinearSpring");
527 nonNegative(hertz, "Joint3D.setMotorLinearSpring", "hertz");
528 nonNegative(dampingRatio, "Joint3D.setMotorLinearSpring", "dampingRatio");
529 nonNegative(maxForce, "Joint3D.setMotorLinearSpring", "maxForce");
530 b3MotorJoint_SetLinearHertz(jointId_, hertz);
531 b3MotorJoint_SetLinearDampingRatio(jointId_, dampingRatio);
532 b3MotorJoint_SetMaxSpringForce(jointId_, maxForce);
533
535 });
536 }
537 eve::Result<void> setMotorAngularSpring(float hertz, float dampingRatio, float maxTorque) override {
538 return checked("physics.joint3d.setMotorAngularSpring", [&] {
539 requireKind(Kind::Motor, "Joint3D.setMotorAngularSpring");
540 nonNegative(hertz, "Joint3D.setMotorAngularSpring", "hertz");
541 nonNegative(dampingRatio, "Joint3D.setMotorAngularSpring", "dampingRatio");
542 nonNegative(maxTorque, "Joint3D.setMotorAngularSpring", "maxTorque");
543 b3MotorJoint_SetAngularHertz(jointId_, hertz);
544 b3MotorJoint_SetAngularDampingRatio(jointId_, dampingRatio);
545 b3MotorJoint_SetMaxSpringTorque(jointId_, maxTorque);
546
548 });
549 }
550 eve::Result<float> getMotorLinearVelocityX() const override {
551 return checked("physics.joint3d.getMotorLinearVelocityX", [&] {
552 requireKind(Kind::Motor, "Joint3D.getMotorLinearVelocityX");
553 return eve::Result<float>::success(b3MotorJoint_GetLinearVelocity(jointId_).x);
554 });
555 }
556 eve::Result<float> getMotorLinearVelocityY() const override {
557 return checked("physics.joint3d.getMotorLinearVelocityY", [&] {
558 requireKind(Kind::Motor, "Joint3D.getMotorLinearVelocityY");
559 return eve::Result<float>::success(b3MotorJoint_GetLinearVelocity(jointId_).y);
560 });
561 }
562 eve::Result<float> getMotorLinearVelocityZ() const override {
563 return checked("physics.joint3d.getMotorLinearVelocityZ", [&] {
564 requireKind(Kind::Motor, "Joint3D.getMotorLinearVelocityZ");
565 return eve::Result<float>::success(b3MotorJoint_GetLinearVelocity(jointId_).z);
566 });
567 }
568 eve::Result<float> getMotorAngularVelocityX() const override {
569 return checked("physics.joint3d.getMotorAngularVelocityX", [&] {
570 requireKind(Kind::Motor, "Joint3D.getMotorAngularVelocityX");
571 return eve::Result<float>::success(b3MotorJoint_GetAngularVelocity(jointId_).x);
572 });
573 }
574 eve::Result<float> getMotorAngularVelocityY() const override {
575 return checked("physics.joint3d.getMotorAngularVelocityY", [&] {
576 requireKind(Kind::Motor, "Joint3D.getMotorAngularVelocityY");
577 return eve::Result<float>::success(b3MotorJoint_GetAngularVelocity(jointId_).y);
578 });
579 }
580 eve::Result<float> getMotorAngularVelocityZ() const override {
581 return checked("physics.joint3d.getMotorAngularVelocityZ", [&] {
582 requireKind(Kind::Motor, "Joint3D.getMotorAngularVelocityZ");
583 return eve::Result<float>::success(b3MotorJoint_GetAngularVelocity(jointId_).z);
584 });
585 }
586 eve::Result<void> setParallelSpring(float hertz, float dampingRatio, float maxTorque) override {
587 return checked("physics.joint3d.setParallelSpring", [&] {
588 requireKind(Kind::Parallel, "Joint3D.setParallelSpring");
589 nonNegative(hertz, "Joint3D.setParallelSpring", "hertz");
590 nonNegative(dampingRatio, "Joint3D.setParallelSpring", "dampingRatio");
591 nonNegative(maxTorque, "Joint3D.setParallelSpring", "maxTorque");
592 b3ParallelJoint_SetSpringHertz(jointId_, hertz);
593 b3ParallelJoint_SetSpringDampingRatio(jointId_, dampingRatio);
594 b3ParallelJoint_SetMaxTorque(jointId_, maxTorque);
595
597 });
598 }
599 eve::Result<float> getParallelHertz() const override {
600 return checked("physics.joint3d.getParallelHertz", [&] {
601 requireKind(Kind::Parallel, "Joint3D.getParallelHertz");
602 return eve::Result<float>::success(b3ParallelJoint_GetSpringHertz(jointId_));
603 });
604 }
605 eve::Result<float> getParallelDampingRatio() const override {
606 return checked("physics.joint3d.getParallelDampingRatio", [&] {
607 requireKind(Kind::Parallel, "Joint3D.getParallelDampingRatio");
608 return eve::Result<float>::success(b3ParallelJoint_GetSpringDampingRatio(jointId_));
609 });
610 }
611 eve::Result<float> getParallelMaxTorque() const override {
612 return checked("physics.joint3d.getParallelMaxTorque", [&] {
613 requireKind(Kind::Parallel, "Joint3D.getParallelMaxTorque");
614 return eve::Result<float>::success(b3ParallelJoint_GetMaxTorque(jointId_));
615 });
616 }
617
618private:
619 void requireKind(Kind expected, const char* operation) const {
620 if (kind_ != expected) throw eve::Exception("%s: incompatible joint kind", operation);
621 }
622 b3JointId jointId_{};
623 Kind kind_;
624};
625} // namespace
626b3JointId Access::jointId(const Joint3D& joint) {
627 auto* native = dynamic_cast<Box3DJoint*>(joint.backend_.get());
628 if (!native) throw eve::Exception("Box3D joint access requires a Box3D backend");
629 return native->jointId_;
630}
631std::unique_ptr<IJointBackend3D> adoptJoint(b3JointId id, JointKind3D kind) {
632 return std::make_unique<Box3DJoint>(id, kind);
633}
634} // namespace eve::physics::box3d
double z
double y
double x
ActionParameterOperation operation
std::string name
Definition ECS.cpp:154
float maxForce
float maximum[3]
float minimum[3]
void(* destroy)(void *)
float dampingRatio
float length
std::string id
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::shared_ptr< OceanGpuSpectrum > owner
bool finite
std::string error
Definition Package.cpp:174
Object::Ptr value
std::string kind
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
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
Script-facing joint owned by a World3D. Legacy scalar/void methods adapt the checked backend contract...
Definition Joint3D.h:27
std::unique_ptr< IJointBackend3D > adoptJoint(b3JointId id, JointKind3D kind)
Adopts a live Box3D constraint; internal native composition boundary, simulation thread only.
Result< ForceVector > checked(ForceVector value)
Definition ForceMath.h:21
void setDistanceSpring(JPH::TwoBodyConstraint &value, bool enabled, float hertz, float damping)
Configures its independent spring after validation; no allocation or callbacks.
void setDistanceLength(JPH::TwoBodyConstraint &value, float length)
Updates an already validated private distance constraint, between simulation steps.
void setDistanceLimits(JPH::TwoBodyConstraint &value, bool enabled, float lo, float hi)
Configures its independent hard limits after validation; no allocation or callbacks.
void setDistanceMotor(JPH::TwoBodyConstraint &value, bool enabled, float speed, float force)
Configures its independent motor after validation; no allocation or callbacks.
JointKind3D
Stable constraint kinds; numeric values are used by stress events.
bool enabled
Private bridge used exclusively by Box3D composition and native query code.
Definition Box3DBody.h:15
static b3JointId jointId(const Joint3D &joint)
Internal checked Box3D joint ID; fails for another backend.