载入中...
搜索中...
未找到
JoltJointOperations.cpp
浏览该文件的文档.
1// Jolt requires its platform header before any other native header.
2#include <Jolt/Jolt.h>
3
4// Provider implementation dependencies.
5#include <algorithm>
7namespace eve::physics::jolt {
8using A = JPH::SixDOFConstraint::EAxis;
10 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
11 if (!std::isfinite(threshold) || threshold < 0)
12 return rejected<void>("Invalid threshold", eve::DiagnosticCode::InvalidArgument);
13 joint->forceThreshold = threshold;
14 return wakeBodies();
15}
17 using R = eve::Result<float>;
18 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
19 return R::success(joint->forceThreshold);
20}
22 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
23 if (!std::isfinite(threshold) || threshold < 0)
24 return rejected<void>("Invalid threshold", eve::DiagnosticCode::InvalidArgument);
25 joint->torqueThreshold = threshold;
26 return wakeBodies();
27}
29 using R = eve::Result<float>;
30 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
31 return R::success(joint->torqueThreshold);
32}
34 using R = eve::Result<float>;
35 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
36 return R::success(force(false).GetX());
37}
39 using R = eve::Result<float>;
40 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
41 return R::success(force(false).GetY());
42}
44 using R = eve::Result<float>;
45 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
46 return R::success(force(false).GetZ());
47}
49 using R = eve::Result<float>;
50 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
51 return R::success(force(true).GetX());
52}
54 using R = eve::Result<float>;
55 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
56 return R::success(force(true).GetY());
57}
59 using R = eve::Result<float>;
60 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
61 return R::success(force(true).GetZ());
62}
64 using R = eve::Result<float>;
65 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
66 return R::success(joint->native ? JPH::Vec3(frame(true).GetTranslation() - frame(false).GetTranslation()).Length()
67 : 0.f);
68}
70 using R = eve::Result<float>;
71 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
72 return R::success(joint->native && joint->kind != JointKind3D::Distance
73 ? 2.f * std::acos(std::clamp(std::abs(six().GetRotationInConstraintSpace().GetW()), 0.f, 1.f))
74 : 0.f);
75}
77 using R = eve::Result<void>;
78 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
79 auto checked = requireKind(JointKind3D::Distance);
80 if (!checked) return R::failure(checked.status());
81 if (!std::isfinite(length) || length < 0)
82 return rejected<void>("Invalid length", eve::DiagnosticCode::InvalidArgument);
83 joint->length = length;
85 return wakeBodies();
86}
88 using R = eve::Result<float>;
89 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
90 auto checked = requireKind(JointKind3D::Distance);
91 if (!checked) return R::failure(checked.status());
92 return R::success(joint->length);
93}
95 using R = eve::Result<float>;
96 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
97 auto checked = requireKind(JointKind3D::Distance);
98 if (!checked) return R::failure(checked.status());
99 return R::success(JPH::Vec3(frame(true).GetTranslation() - frame(false).GetTranslation()).Length());
100}
102 using R = eve::Result<void>;
103 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
104 auto checked = requireKind(JointKind3D::Distance);
105 if (!checked) return R::failure(checked.status());
106 if (!isFinite(hertz, dampingRatio, 0) || hertz < 0 || dampingRatio < 0)
107 return rejected<void>("Invalid spring", eve::DiagnosticCode::InvalidArgument);
109 return wakeBodies();
110}
112 using R = eve::Result<void>;
113 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
114 auto checked = requireKind(JointKind3D::Distance);
115 if (!checked) return R::failure(checked.status());
116 if (!isFinite(minimum, maximum, 0) || minimum < 0 || minimum > maximum)
117 return rejected<void>("Invalid limits", eve::DiagnosticCode::InvalidArgument);
119 return wakeBodies();
120}
122 using R = eve::Result<void>;
123 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
124 auto checked = requireKind(JointKind3D::Distance);
125 if (!checked) return R::failure(checked.status());
126 if (!isFinite(speed, maxForce, 0) || maxForce < 0)
127 return rejected<void>("Invalid motor", eve::DiagnosticCode::InvalidArgument);
128 jolt::setDistanceMotor(*joint->native, enabled, speed, maxForce);
129 return wakeBodies();
130}
131eve::Result<void> JointBackend::setRevoluteSpring(bool enabled, float targetAngle, float hertz, float dampingRatio) {
132 using R = eve::Result<void>;
133 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
134 auto checked = requireKind(JointKind3D::Revolute);
135 if (!checked) return R::failure(checked.status());
136 if (!isFinite(targetAngle, hertz, dampingRatio) || hertz < 0 || dampingRatio < 0)
137 return rejected<void>("Invalid spring", eve::DiagnosticCode::InvalidArgument);
138 six().GetMotorSettings(A(3)).mSpringSettings = {JPH::ESpringMode::FrequencyAndDamping, hertz, dampingRatio};
139 six().SetTargetOrientationCS(JPH::Quat::sRotation(JPH::Vec3::sAxisX(), targetAngle));
140 if (enabled || six().GetMotorState(A(3)) == JPH::EMotorState::Position)
141 six().SetMotorState(A(3), enabled ? JPH::EMotorState::Position : JPH::EMotorState::Off);
142 return wakeBodies();
143}
145 using R = eve::Result<void>;
146 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
147 auto checked = requireKind(JointKind3D::Revolute);
148 if (!checked) return R::failure(checked.status());
149 if (!isFinite(lower, upper, 0) || lower > upper || lower < -JPH::JPH_PI || upper > JPH::JPH_PI)
150 return rejected<void>("Invalid limits", eve::DiagnosticCode::InvalidArgument);
151 // Keep the nonzero reference angle: SixDOF interprets an exactly equal range as a zero lock.
152 if (enabled && lower == upper && lower != 0) {
153 if (upper < JPH::JPH_PI)
154 upper = std::nextafter(upper, JPH::JPH_PI);
155 else
156 lower = std::nextafter(lower, -JPH::JPH_PI);
157 }
158 auto lo = six().GetRotationLimitsMin(), hi = six().GetRotationLimitsMax();
159 lo.SetComponent(0, enabled ? lower : -FLT_MAX);
160 hi.SetComponent(0, enabled ? upper : FLT_MAX);
161 six().SetRotationLimits(lo, hi);
162 return wakeBodies();
163}
164eve::Result<void> JointBackend::setRevoluteMotor(bool enabled, float speed, float maxTorque) {
165 using R = eve::Result<void>;
166 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
167 auto checked = requireKind(JointKind3D::Revolute);
168 if (!checked) return R::failure(checked.status());
169 if (!isFinite(speed, maxTorque, 0) || maxTorque < 0)
170 return rejected<void>("Invalid motor", eve::DiagnosticCode::InvalidArgument);
171 auto& settings = drive().GetMotorSettings(A(3));
172 if (enabled || drive().GetMotorState(A(3)) == JPH::EMotorState::Velocity) settings.SetTorqueLimit(maxTorque);
173 auto velocity = drive().GetTargetAngularVelocityCS();
174 velocity.SetX(speed);
175 drive().SetTargetAngularVelocityCS(velocity);
176 if (enabled || drive().GetMotorState(A(3)) == JPH::EMotorState::Velocity)
177 drive().SetMotorState(A(3), enabled ? JPH::EMotorState::Velocity : JPH::EMotorState::Off);
178 return wakeBodies();
179}
181 using R = eve::Result<float>;
182 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
183 auto checked = requireKind(JointKind3D::Revolute);
184 if (!checked) return R::failure(checked.status());
185 const auto q = six().GetRotationInConstraintSpace();
186 return R::success(2.f * std::atan2(q.GetX(), q.GetW()));
187}
189 using R = eve::Result<float>;
190 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
191 auto checked = requireKind(JointKind3D::Revolute);
192 if (!checked) return R::failure(checked.status());
193 return R::success(drive().GetTotalLambdaMotorRotation().GetX() * joint->world.lock()->inverseStep);
194}
195eve::Result<void> JointBackend::setPrismaticSpring(bool enabled, float targetTranslation, float hertz,
196 float dampingRatio) {
197 using R = eve::Result<void>;
198 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
199 auto checked = requireKind(JointKind3D::Prismatic);
200 if (!checked) return R::failure(checked.status());
201 if (!isFinite(targetTranslation, hertz, dampingRatio) || hertz < 0 || dampingRatio < 0)
202 return rejected<void>("Invalid spring", eve::DiagnosticCode::InvalidArgument);
203 six().GetMotorSettings(A(0)).mSpringSettings = {JPH::ESpringMode::FrequencyAndDamping, hertz, dampingRatio};
204 six().SetTargetPositionCS({targetTranslation, 0, 0});
205 if (enabled || six().GetMotorState(A(0)) == JPH::EMotorState::Position)
206 six().SetMotorState(A(0), enabled ? JPH::EMotorState::Position : JPH::EMotorState::Off);
207 return wakeBodies();
208}
210 using R = eve::Result<void>;
211 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
212 auto checked = requireKind(JointKind3D::Prismatic);
213 if (!checked) return R::failure(checked.status());
214 if (!isFinite(lower, upper, 0) || lower > upper)
215 return rejected<void>("Invalid limits", eve::DiagnosticCode::InvalidArgument);
216 // One representable step keeps a nonzero target without triggering SixDOF's zero-lock shortcut.
217 if (enabled && lower == upper && lower != 0) {
218 if (upper < FLT_MAX)
219 upper = std::nextafter(upper, FLT_MAX);
220 else
221 lower = std::nextafter(lower, -FLT_MAX);
222 }
223 auto lo = six().GetTranslationLimitsMin(), hi = six().GetTranslationLimitsMax();
224 lo.SetComponent(0, enabled ? lower : -FLT_MAX);
225 hi.SetComponent(0, enabled ? upper : FLT_MAX);
226 six().SetTranslationLimits(lo, hi);
227 return wakeBodies();
228}
230 using R = eve::Result<void>;
231 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
232 auto checked = requireKind(JointKind3D::Prismatic);
233 if (!checked) return R::failure(checked.status());
234 if (!isFinite(speed, maxForce, 0) || maxForce < 0)
235 return rejected<void>("Invalid motor", eve::DiagnosticCode::InvalidArgument);
236 auto& settings = drive().GetMotorSettings(A(0));
237 if (enabled || drive().GetMotorState(A(0)) == JPH::EMotorState::Velocity) settings.SetForceLimit(maxForce);
238 auto velocity = drive().GetTargetVelocityCS();
239 velocity.SetX(speed);
240 drive().SetTargetVelocityCS(velocity);
241 if (enabled || drive().GetMotorState(A(0)) == JPH::EMotorState::Velocity)
242 drive().SetMotorState(A(0), enabled ? JPH::EMotorState::Velocity : JPH::EMotorState::Off);
243 return wakeBodies();
244}
246 using R = eve::Result<float>;
247 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
248 auto checked = requireKind(JointKind3D::Prismatic);
249 if (!checked) return R::failure(checked.status());
250 return R::success(
251 frame(false)
252 .Multiply3x3Transposed(JPH::Vec3(frame(true).GetTranslation() - frame(false).GetTranslation()))
253 .GetX());
254}
256 using R = eve::Result<float>;
257 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
258 auto checked = requireKind(JointKind3D::Prismatic);
259 if (!checked) return R::failure(checked.status());
260 const auto a = joint->a.lock(), b = joint->b.lock();
261 return R::success(frame(false)
262 .Multiply3x3Transposed(b->api().GetPointVelocity(b->id, frame(true).GetTranslation()) -
263 a->api().GetPointVelocity(a->id, frame(false).GetTranslation()))
264 .GetX());
265}
267 using R = eve::Result<float>;
268 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
269 auto checked = requireKind(JointKind3D::Prismatic);
270 if (!checked) return R::failure(checked.status());
271 return R::success(drive().GetTotalLambdaMotorTranslation().GetX() * joint->world.lock()->inverseStep);
272}
274 using R = eve::Result<void>;
275 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
276 auto checked = requireKind(JointKind3D::Spherical);
277 if (!checked) return R::failure(checked.status());
278 if (!std::isfinite(angle) || angle < 0 || angle > JPH::JPH_PI)
279 return rejected<void>("Invalid cone", eve::DiagnosticCode::InvalidArgument);
280 auto lo = six().GetRotationLimitsMin(), hi = six().GetRotationLimitsMax();
281 const float limit = enabled ? angle : JPH::JPH_PI;
282 lo.SetY(-limit);
283 lo.SetZ(-limit);
284 hi.SetY(limit);
285 hi.SetZ(limit);
286 six().SetRotationLimits(lo, hi);
287 return wakeBodies();
288}
290 using R = eve::Result<void>;
291 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
292 auto checked = requireKind(JointKind3D::Spherical);
293 if (!checked) return R::failure(checked.status());
294 if (!isFinite(lower, upper, 0) || lower > upper || lower < -JPH::JPH_PI || upper > JPH::JPH_PI)
295 return rejected<void>("Invalid limits", eve::DiagnosticCode::InvalidArgument);
296 // Keep the nonzero reference angle: SixDOF interprets an exactly equal range as a zero lock.
297 if (enabled && lower == upper && lower != 0) {
298 if (upper < JPH::JPH_PI)
299 upper = std::nextafter(upper, JPH::JPH_PI);
300 else
301 lower = std::nextafter(lower, -JPH::JPH_PI);
302 }
303 auto lo = six().GetRotationLimitsMin(), hi = six().GetRotationLimitsMax();
304 lo.SetComponent(0, enabled ? lower : -FLT_MAX);
305 hi.SetComponent(0, enabled ? upper : FLT_MAX);
306 six().SetRotationLimits(lo, hi);
307 return wakeBodies();
308}
309eve::Result<void> JointBackend::setSphericalMotor(bool enabled, float velocityX, float velocityY, float velocityZ,
310 float maxTorque) {
311 using R = eve::Result<void>;
312 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
313 auto checked = requireKind(JointKind3D::Spherical);
314 if (!checked) return R::failure(checked.status());
315 if (!isFinite(velocityX, velocityY, velocityZ) || !std::isfinite(maxTorque) || maxTorque < 0)
316 return rejected<void>("Invalid motor", eve::DiagnosticCode::InvalidArgument);
317 joint->worldAngularVelocity = {velocityX, velocityY, velocityZ};
318 six().SetTargetAngularVelocityCS(frame(false).Multiply3x3Transposed(joint->worldAngularVelocity));
319 for (int i = 3; i < 6; ++i) {
320 six().GetMotorSettings(A(i)).SetTorqueLimit(maxTorque);
321 six().SetMotorState(A(i), enabled ? JPH::EMotorState::Velocity : JPH::EMotorState::Off);
322 }
323 return wakeBodies();
324}
326 using R = eve::Result<float>;
327 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
328 auto checked = requireKind(JointKind3D::Spherical);
329 if (!checked) return R::failure(checked.status());
330 return R::success(
331 std::acos(std::clamp((six().GetRotationInConstraintSpace() * JPH::Vec3::sAxisX()).GetX(), -1.f, 1.f)));
332}
334 using R = eve::Result<float>;
335 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
336 auto checked = requireKind(JointKind3D::Spherical);
337 if (!checked) return R::failure(checked.status());
338 const auto q = six().GetRotationInConstraintSpace();
339 return R::success(2.f * std::atan2(q.GetX(), q.GetW()));
340}
342 using R = eve::Result<void>;
343 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
344 auto checked = requireKind(JointKind3D::Wheel);
345 if (!checked) return R::failure(checked.status());
346 if (!isFinite(hertz, dampingRatio, 0) || hertz < 0 || dampingRatio < 0)
347 return rejected<void>("Invalid spring", eve::DiagnosticCode::InvalidArgument);
348 six().GetMotorSettings(A::TranslationY).mSpringSettings = {JPH::ESpringMode::FrequencyAndDamping, hertz,
350 six().SetMotorState(A::TranslationY, enabled ? JPH::EMotorState::Position : JPH::EMotorState::Off);
351 return wakeBodies();
352}
354 using R = eve::Result<void>;
355 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
356 auto checked = requireKind(JointKind3D::Wheel);
357 if (!checked) return R::failure(checked.status());
358 if (!isFinite(lower, upper, 0) || lower > upper)
359 return rejected<void>("Invalid limits", eve::DiagnosticCode::InvalidArgument);
360 // One representable step keeps a nonzero target without triggering SixDOF's zero-lock shortcut.
361 if (enabled && lower == upper && lower != 0) {
362 if (upper < FLT_MAX)
363 upper = std::nextafter(upper, FLT_MAX);
364 else
365 lower = std::nextafter(lower, -FLT_MAX);
366 }
367 auto lo = six().GetTranslationLimitsMin(), hi = six().GetTranslationLimitsMax();
368 lo.SetComponent(1, enabled ? lower : -FLT_MAX);
369 hi.SetComponent(1, enabled ? upper : FLT_MAX);
370 six().SetTranslationLimits(lo, hi);
371 return wakeBodies();
372}
373eve::Result<void> JointBackend::setWheelSpinMotor(bool enabled, float speed, float maxTorque) {
374 using R = eve::Result<void>;
375 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
376 auto checked = requireKind(JointKind3D::Wheel);
377 if (!checked) return R::failure(checked.status());
378 if (!isFinite(speed, maxTorque, 0) || maxTorque < 0)
379 return rejected<void>("Invalid motor", eve::DiagnosticCode::InvalidArgument);
380 auto& settings = six().GetMotorSettings(A(3));
381 if (enabled || six().GetMotorState(A(3)) == JPH::EMotorState::Velocity) settings.SetTorqueLimit(maxTorque);
382 auto velocity = six().GetTargetAngularVelocityCS();
383 velocity.SetX(speed);
384 six().SetTargetAngularVelocityCS(velocity);
385 six().SetMotorState(A(3), enabled ? JPH::EMotorState::Velocity : JPH::EMotorState::Off);
386 return wakeBodies();
387}
388eve::Result<void> JointBackend::setWheelSteering(bool enabled, float targetAngle, float hertz, float dampingRatio,
389 float maxTorque) {
390 using R = eve::Result<void>;
391 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
392 auto checked = requireKind(JointKind3D::Wheel);
393 if (!checked) return R::failure(checked.status());
394 if (!isFinite(targetAngle, hertz, dampingRatio) || !std::isfinite(maxTorque) || hertz < 0 || dampingRatio < 0 ||
395 maxTorque < 0)
396 return rejected<void>("Invalid steering", eve::DiagnosticCode::InvalidArgument);
397 auto& m = six().GetMotorSettings(A::RotationY);
398 m.mSpringSettings = {JPH::ESpringMode::FrequencyAndDamping, hertz, dampingRatio};
399 m.SetTorqueLimit(maxTorque);
400 six().SetTargetOrientationCS(JPH::Quat::sRotation(JPH::Vec3::sAxisY(), targetAngle));
401 six().SetMotorState(A::RotationY, enabled ? JPH::EMotorState::Position : JPH::EMotorState::Off);
402 return wakeBodies();
403}
405 using R = eve::Result<void>;
406 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
407 auto checked = requireKind(JointKind3D::Wheel);
408 if (!checked) return R::failure(checked.status());
409 if (!isFinite(lower, upper, 0) || lower > upper || lower < -JPH::JPH_PI || upper > JPH::JPH_PI)
410 return rejected<void>("Invalid limits", eve::DiagnosticCode::InvalidArgument);
411 // Keep the nonzero reference angle: SixDOF interprets an exactly equal range as a zero lock.
412 if (enabled && lower == upper && lower != 0) {
413 if (upper < JPH::JPH_PI)
414 upper = std::nextafter(upper, JPH::JPH_PI);
415 else
416 lower = std::nextafter(lower, -JPH::JPH_PI);
417 }
418 auto lo = six().GetRotationLimitsMin(), hi = six().GetRotationLimitsMax();
419 lo.SetComponent(1, enabled ? lower : -FLT_MAX);
420 hi.SetComponent(1, enabled ? upper : FLT_MAX);
421 six().SetRotationLimits(lo, hi);
422 return wakeBodies();
423}
425 using R = eve::Result<float>;
426 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
427 auto checked = requireKind(JointKind3D::Wheel);
428 if (!checked) return R::failure(checked.status());
429 auto a = joint->a.lock(), b = joint->b.lock();
430 return R::success(
431 frame(false)
432 .Multiply3x3Transposed(b->api().GetAngularVelocity(b->id) - a->api().GetAngularVelocity(a->id))
433 .GetX());
434}
436 using R = eve::Result<float>;
437 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
438 auto checked = requireKind(JointKind3D::Wheel);
439 if (!checked) return R::failure(checked.status());
440 return R::success(six().GetTotalLambdaMotorRotation().GetX() * joint->world.lock()->inverseStep);
441}
443 using R = eve::Result<float>;
444 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
445 auto checked = requireKind(JointKind3D::Wheel);
446 if (!checked) return R::failure(checked.status());
447 const auto q = six().GetRotationInConstraintSpace();
448 return R::success(2.f * std::atan2(q.GetY(), q.GetW()));
449}
451 using R = eve::Result<float>;
452 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
453 auto checked = requireKind(JointKind3D::Wheel);
454 if (!checked) return R::failure(checked.status());
455 return R::success(six().GetTotalLambdaMotorRotation().GetY() * joint->world.lock()->inverseStep);
456}
458 using R = eve::Result<void>;
459 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
460 auto checked = requireKind(JointKind3D::Weld);
461 if (!checked) return R::failure(checked.status());
462 if (!isFinite(hertz, dampingRatio, 0) || hertz < 0 || dampingRatio < 0)
463 return rejected<void>("Invalid spring", eve::DiagnosticCode::InvalidArgument);
464 six().SetTranslationLimits(JPH::Vec3::sReplicate(hertz > 0 ? -FLT_MAX : FLT_MAX),
465 JPH::Vec3::sReplicate(hertz > 0 ? FLT_MAX : -FLT_MAX));
466 for (int i = 0; i < 3; ++i) {
467 auto& m = six().GetMotorSettings(A(i));
468 m.mSpringSettings = {JPH::ESpringMode::FrequencyAndDamping, hertz, dampingRatio};
469 m.SetForceLimit(FLT_MAX);
470 six().SetMotorState(A(i), hertz > 0 ? JPH::EMotorState::Position : JPH::EMotorState::Off);
471 }
472 return wakeBodies();
473}
475 using R = eve::Result<void>;
476 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
477 auto checked = requireKind(JointKind3D::Weld);
478 if (!checked) return R::failure(checked.status());
479 if (!isFinite(hertz, dampingRatio, 0) || hertz < 0 || dampingRatio < 0)
480 return rejected<void>("Invalid spring", eve::DiagnosticCode::InvalidArgument);
481 six().SetRotationLimits(JPH::Vec3::sReplicate(hertz > 0 ? -FLT_MAX : FLT_MAX),
482 JPH::Vec3::sReplicate(hertz > 0 ? FLT_MAX : -FLT_MAX));
483 for (int i = 3; i < 6; ++i) {
484 auto& m = six().GetMotorSettings(A(i));
485 m.mSpringSettings = {JPH::ESpringMode::FrequencyAndDamping, hertz, dampingRatio};
486 m.SetTorqueLimit(FLT_MAX);
487 six().SetMotorState(A(i), hertz > 0 ? JPH::EMotorState::Position : JPH::EMotorState::Off);
488 }
489 return wakeBodies();
490}
492 using R = eve::Result<float>;
493 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
494 auto checked = requireKind(JointKind3D::Weld);
495 if (!checked) return R::failure(checked.status());
496 return R::success(six().GetMotorSettings(A(0)).mSpringSettings.mFrequency);
497}
499 using R = eve::Result<float>;
500 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
501 auto checked = requireKind(JointKind3D::Weld);
502 if (!checked) return R::failure(checked.status());
503 return R::success(six().GetMotorSettings(A(0)).mSpringSettings.mDamping);
504}
506 using R = eve::Result<float>;
507 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
508 auto checked = requireKind(JointKind3D::Weld);
509 if (!checked) return R::failure(checked.status());
510 return R::success(six().GetMotorSettings(A(3)).mSpringSettings.mFrequency);
511}
513 using R = eve::Result<float>;
514 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
515 auto checked = requireKind(JointKind3D::Weld);
516 if (!checked) return R::failure(checked.status());
517 return R::success(six().GetMotorSettings(A(3)).mSpringSettings.mDamping);
518}
520 using R = eve::Result<void>;
521 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
522 auto checked = requireKind(JointKind3D::Motor);
523 if (!checked) return R::failure(checked.status());
524 if (!isFinite(vx, vy, vz)) return rejected<void>("Invalid velocity", eve::DiagnosticCode::InvalidArgument);
525 joint->worldLinearVelocity = {vx, vy, vz};
526 for (int i = 0; i < 3; ++i) drive().GetMotorSettings(A(i)).SetForceLimit(joint->maxVelocityForce);
527 drive().SetTargetVelocityCS(frame(false).Multiply3x3Transposed(joint->worldLinearVelocity));
528 for (int i = 0; i < 3; ++i) drive().SetMotorState(A(i), JPH::EMotorState::Velocity);
529 return wakeBodies();
530}
532 using R = eve::Result<void>;
533 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
534 auto checked = requireKind(JointKind3D::Motor);
535 if (!checked) return R::failure(checked.status());
536 if (!isFinite(wx, wy, wz)) return rejected<void>("Invalid velocity", eve::DiagnosticCode::InvalidArgument);
537 joint->worldAngularVelocity = {wx, wy, wz};
538 for (int i = 3; i < 6; ++i) drive().GetMotorSettings(A(i)).SetTorqueLimit(joint->maxVelocityTorque);
539 drive().SetTargetAngularVelocityCS(frame(false).Multiply3x3Transposed(joint->worldAngularVelocity));
540 for (int i = 3; i < 6; ++i) drive().SetMotorState(A(i), JPH::EMotorState::Velocity);
541 return wakeBodies();
542}
544 using R = eve::Result<void>;
545 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
546 auto checked = requireKind(JointKind3D::Motor);
547 if (!checked) return R::failure(checked.status());
548 if (!isFinite(maxForce, maxTorque, 0) || maxForce < 0 || maxTorque < 0)
549 return rejected<void>("Invalid motor limits", eve::DiagnosticCode::InvalidArgument);
550 joint->maxVelocityForce = maxForce;
551 joint->maxVelocityTorque = maxTorque;
552 for (int i = 0; i < 6; ++i)
553 if (drive().GetMotorState(A(i)) == JPH::EMotorState::Velocity) {
554 drive().GetMotorSettings(A(i)).SetForceLimit(maxForce);
555 drive().GetMotorSettings(A(i)).SetTorqueLimit(maxTorque);
556 }
557 return wakeBodies();
558}
560 using R = eve::Result<void>;
561 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
562 auto checked = requireKind(JointKind3D::Motor);
563 if (!checked) return R::failure(checked.status());
564 if (!isFinite(hertz, dampingRatio, maxForce) || hertz < 0 || dampingRatio < 0 || maxForce < 0)
565 return rejected<void>("Invalid spring", eve::DiagnosticCode::InvalidArgument);
566
567 for (int i = 0; i < 3; ++i) {
568 auto& m = six().GetMotorSettings(A(i));
569 m.mSpringSettings = {JPH::ESpringMode::FrequencyAndDamping, hertz, dampingRatio};
570 if (hertz > 0 || six().GetMotorState(A(i)) == JPH::EMotorState::Position) m.SetForceLimit(maxForce);
571 if (hertz > 0 || six().GetMotorState(A(i)) == JPH::EMotorState::Position)
572 six().SetMotorState(A(i), hertz > 0 ? JPH::EMotorState::Position : JPH::EMotorState::Off);
573 }
574 return wakeBodies();
575}
577 using R = eve::Result<void>;
578 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
579 auto checked = requireKind(JointKind3D::Motor);
580 if (!checked) return R::failure(checked.status());
581 if (!isFinite(hertz, dampingRatio, maxTorque) || hertz < 0 || dampingRatio < 0 || maxTorque < 0)
582 return rejected<void>("Invalid spring", eve::DiagnosticCode::InvalidArgument);
583
584 for (int i = 3; i < 6; ++i) {
585 auto& m = six().GetMotorSettings(A(i));
586 m.mSpringSettings = {JPH::ESpringMode::FrequencyAndDamping, hertz, dampingRatio};
587 if (hertz > 0 || six().GetMotorState(A(i)) == JPH::EMotorState::Position) m.SetTorqueLimit(maxTorque);
588 if (hertz > 0 || six().GetMotorState(A(i)) == JPH::EMotorState::Position)
589 six().SetMotorState(A(i), hertz > 0 ? JPH::EMotorState::Position : JPH::EMotorState::Off);
590 }
591 return wakeBodies();
592}
594 using R = eve::Result<float>;
595 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
596 auto checked = requireKind(JointKind3D::Motor);
597 if (!checked) return R::failure(checked.status());
598 return R::success(joint->worldLinearVelocity.GetX());
599}
601 using R = eve::Result<float>;
602 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
603 auto checked = requireKind(JointKind3D::Motor);
604 if (!checked) return R::failure(checked.status());
605 return R::success(joint->worldLinearVelocity.GetY());
606}
608 using R = eve::Result<float>;
609 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
610 auto checked = requireKind(JointKind3D::Motor);
611 if (!checked) return R::failure(checked.status());
612 return R::success(joint->worldLinearVelocity.GetZ());
613}
615 using R = eve::Result<float>;
616 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
617 auto checked = requireKind(JointKind3D::Motor);
618 if (!checked) return R::failure(checked.status());
619 return R::success(joint->worldAngularVelocity.GetX());
620}
622 using R = eve::Result<float>;
623 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
624 auto checked = requireKind(JointKind3D::Motor);
625 if (!checked) return R::failure(checked.status());
626 return R::success(joint->worldAngularVelocity.GetY());
627}
629 using R = eve::Result<float>;
630 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
631 auto checked = requireKind(JointKind3D::Motor);
632 if (!checked) return R::failure(checked.status());
633 return R::success(joint->worldAngularVelocity.GetZ());
634}
636 using R = eve::Result<void>;
637 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
638 auto checked = requireKind(JointKind3D::Parallel);
639 if (!checked) return R::failure(checked.status());
640 if (!isFinite(hertz, dampingRatio, maxTorque) || hertz < 0 || dampingRatio < 0 || maxTorque < 0)
641 return rejected<void>("Invalid spring", eve::DiagnosticCode::InvalidArgument);
642 six().SetRotationLimits({-FLT_MAX, hertz > 0 ? -FLT_MAX : FLT_MAX, hertz > 0 ? -FLT_MAX : FLT_MAX},
643 {FLT_MAX, hertz > 0 ? FLT_MAX : -FLT_MAX, hertz > 0 ? FLT_MAX : -FLT_MAX});
644 for (int i = 4; i < 6; ++i) {
645 auto& m = six().GetMotorSettings(A(i));
646 m.mSpringSettings = {JPH::ESpringMode::FrequencyAndDamping, hertz, dampingRatio};
647 m.SetTorqueLimit(maxTorque);
648 six().SetMotorState(A(i), hertz > 0 ? JPH::EMotorState::Position : JPH::EMotorState::Off);
649 }
650 return wakeBodies();
651}
653 using R = eve::Result<float>;
654 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
655 auto checked = requireKind(JointKind3D::Parallel);
656 if (!checked) return R::failure(checked.status());
657 return R::success(six().GetMotorSettings(A::RotationY).mSpringSettings.mFrequency);
658}
660 using R = eve::Result<float>;
661 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
662 auto checked = requireKind(JointKind3D::Parallel);
663 if (!checked) return R::failure(checked.status());
664 return R::success(six().GetMotorSettings(A::RotationY).mSpringSettings.mDamping);
665}
667 using R = eve::Result<float>;
668 if (!isValid()) return rejected<float>("Joint expired", eve::DiagnosticCode::StaleHandle);
669 auto checked = requireKind(JointKind3D::Parallel);
670 if (!checked) return R::failure(checked.status());
671 return R::success(six().GetMotorSettings(A::RotationY).mMaxTorqueLimit);
672}
673} // namespace eve::physics::jolt
double m[16]
ForceVector force
float maxForce
float maximum[3]
float minimum[3]
std::array< double, 10 > q
float dampingRatio
float length
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanWakeSettings settings
ssq::Object frame
glm::dvec3 velocity
int limit
Definition TreeMesh.cpp:166
Move-only operation result carrying either a value or Status.
Definition Result.h:176
eve::Result< float > getWeldLinearDampingRatio() const override
getWeldLinearDampingRatio; see the class lifetime, validation and failure contract.
eve::Result< void > setForceThreshold(float threshold) override
setForceThreshold; see the class lifetime, validation and failure contract.
eve::Result< float > getConstraintTorqueX() const override
getConstraintTorqueX; see the class lifetime, validation and failure contract.
eve::Result< float > getWheelSpinTorque() const override
getWheelSpinTorque; see the class lifetime, validation and failure contract.
eve::Result< float > getTorqueThreshold() const override
getTorqueThreshold; see the class lifetime, validation and failure contract.
std::shared_ptr< JointState > joint
Definition JoltJoint.h:27
eve::Result< void > setSphericalTwistLimits(bool enabled, float lower, float upper) override
setSphericalTwistLimits; see the class lifetime, validation and failure contract.
eve::Result< void > setWheelSpinMotor(bool enabled, float speed, float maxTorque) override
setWheelSpinMotor; see the class lifetime, validation and failure contract.
eve::Result< void > setDistanceLength(float length) override
setDistanceLength; see the class lifetime, validation and failure contract.
eve::Result< void > setRevoluteMotor(bool enabled, float speed, float maxTorque) override
setRevoluteMotor; see the class lifetime, validation and failure contract.
eve::Result< float > getMotorAngularVelocityX() const override
getMotorAngularVelocityX; see the class lifetime, validation and failure contract.
eve::Result< void > setPrismaticMotor(bool enabled, float speed, float maxForce) override
setPrismaticMotor; see the class lifetime, validation and failure contract.
eve::Result< void > setMotorLinearSpring(float hertz, float dampingRatio, float maxForce) override
setMotorLinearSpring; see the class lifetime, validation and failure contract.
eve::Result< float > getConstraintForceZ() const override
getConstraintForceZ; see the class lifetime, validation and failure contract.
eve::Result< void > setRevoluteSpring(bool enabled, float targetAngle, float hertz, float dampingRatio) override
setRevoluteSpring; see the class lifetime, validation and failure contract.
eve::Result< float > getSphericalConeAngle() const override
getSphericalConeAngle; see the class lifetime, validation and failure contract.
eve::Result< float > getAngularSeparation() const override
getAngularSeparation; see the class lifetime, validation and failure contract.
eve::Result< void > setMotorLinearVelocity(float vx, float vy, float vz) override
setMotorLinearVelocity; see the class lifetime, validation and failure contract.
eve::Result< void > setSphericalConeLimit(bool enabled, float angle) override
setSphericalConeLimit; see the class lifetime, validation and failure contract.
eve::Result< float > getMotorAngularVelocityZ() const override
getMotorAngularVelocityZ; see the class lifetime, validation and failure contract.
eve::Result< float > getDistanceLength() const override
getDistanceLength; see the class lifetime, validation and failure contract.
eve::Result< float > getWeldAngularDampingRatio() const override
getWeldAngularDampingRatio; see the class lifetime, validation and failure contract.
eve::Result< void > setTorqueThreshold(float threshold) override
setTorqueThreshold; see the class lifetime, validation and failure contract.
eve::Result< float > getPrismaticTranslation() const override
getPrismaticTranslation; see the class lifetime, validation and failure contract.
eve::Result< float > getConstraintForceX() const override
getConstraintForceX; see the class lifetime, validation and failure contract.
eve::Result< float > getWheelSteeringAngle() const override
getWheelSteeringAngle; see the class lifetime, validation and failure contract.
eve::Result< float > getParallelDampingRatio() const override
getParallelDampingRatio; see the class lifetime, validation and failure contract.
eve::Result< void > setDistanceSpring(bool enabled, float hertz, float dampingRatio) override
setDistanceSpring; see the class lifetime, validation and failure contract.
eve::Result< void > setPrismaticSpring(bool enabled, float targetTranslation, float hertz, float dampingRatio) override
setPrismaticSpring; see the class lifetime, validation and failure contract.
eve::Result< void > setDistanceMotor(bool enabled, float speed, float maxForce) override
setDistanceMotor; see the class lifetime, validation and failure contract.
eve::Result< float > getSphericalTwistAngle() const override
getSphericalTwistAngle; see the class lifetime, validation and failure contract.
eve::Result< float > getPrismaticMotorForce() const override
getPrismaticMotorForce; see the class lifetime, validation and failure contract.
eve::Result< float > getDistanceCurrentLength() const override
getDistanceCurrentLength; see the class lifetime, validation and failure contract.
eve::Result< void > setDistanceLimits(bool enabled, float minimum, float maximum) override
setDistanceLimits; see the class lifetime, validation and failure contract.
bool isValid() const noexcept override
Whether the native constraint is still alive.
Definition JoltJoint.cpp:14
eve::Result< void > setPrismaticLimits(bool enabled, float lower, float upper) override
setPrismaticLimits; see the class lifetime, validation and failure contract.
eve::Result< float > getWheelSteeringTorque() const override
getWheelSteeringTorque; see the class lifetime, validation and failure contract.
eve::Result< void > setWeldAngularSpring(float hertz, float dampingRatio) override
setWeldAngularSpring; see the class lifetime, validation and failure contract.
eve::Result< void > setWheelSteeringLimits(bool enabled, float lower, float upper) override
setWheelSteeringLimits; see the class lifetime, validation and failure contract.
eve::Result< float > getConstraintTorqueY() const override
getConstraintTorqueY; see the class lifetime, validation and failure contract.
eve::Result< float > getParallelMaxTorque() const override
getParallelMaxTorque; see the class lifetime, validation and failure contract.
eve::Result< void > setWheelSuspensionLimits(bool enabled, float lower, float upper) override
setWheelSuspensionLimits; see the class lifetime, validation and failure contract.
eve::Result< void > setParallelSpring(float hertz, float dampingRatio, float maxTorque) override
setParallelSpring; see the class lifetime, validation and failure contract.
eve::Result< float > getMotorLinearVelocityZ() const override
getMotorLinearVelocityZ; see the class lifetime, validation and failure contract.
eve::Result< void > setWheelSuspension(bool enabled, float hertz, float dampingRatio) override
setWheelSuspension; see the class lifetime, validation and failure contract.
eve::Result< float > getRevoluteMotorTorque() const override
getRevoluteMotorTorque; see the class lifetime, validation and failure contract.
eve::Result< float > getWheelSpinSpeed() const override
getWheelSpinSpeed; see the class lifetime, validation and failure contract.
eve::Result< void > setWheelSteering(bool enabled, float targetAngle, float hertz, float dampingRatio, float maxTorque) override
setWheelSteering; see the class lifetime, validation and failure contract.
eve::Result< float > getRevoluteAngle() const override
getRevoluteAngle; see the class lifetime, validation and failure contract.
eve::Result< float > getForceThreshold() const override
getForceThreshold; see the class lifetime, validation and failure contract.
eve::Result< float > getMotorAngularVelocityY() const override
getMotorAngularVelocityY; see the class lifetime, validation and failure contract.
eve::Result< float > getLinearSeparation() const override
getLinearSeparation; see the class lifetime, validation and failure contract.
eve::Result< void > setMotorVelocityLimits(float maxForce, float maxTorque) override
setMotorVelocityLimits; see the class lifetime, validation and failure contract.
eve::Result< float > getMotorLinearVelocityX() const override
getMotorLinearVelocityX; see the class lifetime, validation and failure contract.
eve::Result< void > setRevoluteLimits(bool enabled, float lower, float upper) override
setRevoluteLimits; see the class lifetime, validation and failure contract.
eve::Result< float > getConstraintForceY() const override
getConstraintForceY; see the class lifetime, validation and failure contract.
eve::Result< void > setSphericalMotor(bool enabled, float velocityX, float velocityY, float velocityZ, float maxTorque) override
setSphericalMotor; see the class lifetime, validation and failure contract.
eve::Result< float > getWeldAngularHertz() const override
getWeldAngularHertz; see the class lifetime, validation and failure contract.
eve::Result< float > getMotorLinearVelocityY() const override
getMotorLinearVelocityY; see the class lifetime, validation and failure contract.
eve::Result< float > getConstraintTorqueZ() const override
getConstraintTorqueZ; see the class lifetime, validation and failure contract.
eve::Result< float > getParallelHertz() const override
getParallelHertz; see the class lifetime, validation and failure contract.
eve::Result< void > setMotorAngularVelocity(float wx, float wy, float wz) override
setMotorAngularVelocity; see the class lifetime, validation and failure contract.
eve::Result< void > setMotorAngularSpring(float hertz, float dampingRatio, float maxTorque) override
setMotorAngularSpring; see the class lifetime, validation and failure contract.
eve::Result< float > getPrismaticSpeed() const override
getPrismaticSpeed; see the class lifetime, validation and failure contract.
eve::Result< float > getWeldLinearHertz() const override
getWeldLinearHertz; see the class lifetime, validation and failure contract.
eve::Result< void > setWeldLinearSpring(float hertz, float dampingRatio) override
setWeldLinearSpring; see the class lifetime, validation and failure contract.
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.
bool isFinite(float x, float y, float z)
JPH::SixDOFConstraint::EAxis A
void setDistanceMotor(JPH::TwoBodyConstraint &value, bool enabled, float speed, float force)
Configures its independent motor after validation; no allocation or callbacks.
bool enabled