载入中...
搜索中...
未找到
JoltBodyOperations.cpp
浏览该文件的文档.
1// Jolt requires its platform header before any other native header.
2#include <Jolt/Jolt.h>
3
4// Provider implementation dependencies.
5#include <Jolt/Physics/Body/MotionProperties.h>
6#include <Jolt/Physics/Collision/Shape/OffsetCenterOfMassShape.h>
10namespace eve::physics::jolt {
12 using R [[maybe_unused]] = eve::Result<void>;
13 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
14 if (!isFinite(x, y, z)) return rejected<void>("Invalid position", eve::DiagnosticCode::InvalidArgument);
15 body->api().SetPosition(body->id, {x, y, z}, JPH::EActivation::Activate);
16 return success();
17}
19 using R [[maybe_unused]] = eve::Result<float>;
20 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
21 return R::success(static_cast<float>(body->api().GetPosition(body->id).GetX()));
22}
24 using R [[maybe_unused]] = eve::Result<float>;
25 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
26 return R::success(static_cast<float>(body->api().GetPosition(body->id).GetY()));
27}
29 using R [[maybe_unused]] = eve::Result<float>;
30 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
31 return R::success(static_cast<float>(body->api().GetPosition(body->id).GetZ()));
32}
33eve::Result<void> BodyBackend::setRotation(float qx, float qy, float qz, float qw) {
34 using R [[maybe_unused]] = eve::Result<void>;
35 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
36 JPH::Quat q(qx, qy, qz, qw);
37 if (!isFinite(qx, qy, qz) || !std::isfinite(qw) || !std::isfinite(q.LengthSq()) || q.LengthSq() < 1.e-12f)
38 return rejected<void>("Invalid rotation", eve::DiagnosticCode::InvalidArgument);
39 body->api().SetRotation(body->id, q.Normalized(), JPH::EActivation::Activate);
40 return success();
41}
43 using R [[maybe_unused]] = eve::Result<float>;
44 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
45 return R::success(body->api().GetRotation(body->id).GetX());
46}
48 using R [[maybe_unused]] = eve::Result<float>;
49 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
50 return R::success(body->api().GetRotation(body->id).GetY());
51}
53 using R [[maybe_unused]] = eve::Result<float>;
54 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
55 return R::success(body->api().GetRotation(body->id).GetZ());
56}
58 using R [[maybe_unused]] = eve::Result<float>;
59 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
60 return R::success(body->api().GetRotation(body->id).GetW());
61}
63 using R [[maybe_unused]] = eve::Result<void>;
64 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
65 if (!isFinite(vx, vy, vz)) return rejected<void>("Invalid velocity", eve::DiagnosticCode::InvalidArgument);
66 body->api().SetLinearVelocity(body->id, {vx, vy, vz});
67 return success();
68}
70 using R [[maybe_unused]] = eve::Result<float>;
71 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
72 return R::success(body->api().GetLinearVelocity(body->id).GetX());
73}
75 using R [[maybe_unused]] = eve::Result<float>;
76 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
77 return R::success(body->api().GetLinearVelocity(body->id).GetY());
78}
80 using R [[maybe_unused]] = eve::Result<float>;
81 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
82 return R::success(body->api().GetLinearVelocity(body->id).GetZ());
83}
85 using R [[maybe_unused]] = eve::Result<float>;
86 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
87 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
88 return R::success(native->IsDynamic() ? body->mass.mMass : 0.f);
89}
91 using R [[maybe_unused]] = eve::Result<void>;
92 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
93 if (!isFinite(wx, wy, wz)) return rejected<void>("Invalid velocity", eve::DiagnosticCode::InvalidArgument);
94 body->api().SetAngularVelocity(body->id, {wx, wy, wz});
95 return success();
96}
98 using R [[maybe_unused]] = eve::Result<float>;
99 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
100 return R::success(body->api().GetAngularVelocity(body->id).GetX());
101}
103 using R [[maybe_unused]] = eve::Result<float>;
104 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
105 return R::success(body->api().GetAngularVelocity(body->id).GetY());
106}
108 using R [[maybe_unused]] = eve::Result<float>;
109 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
110 return R::success(body->api().GetAngularVelocity(body->id).GetZ());
111}
113 using R [[maybe_unused]] = eve::Result<std::vector<float>>;
114 if (!isValid()) return rejected<std::vector<float>>("Object expired", eve::DiagnosticCode::StaleHandle);
115 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
116 if (!isFinite(x, y, z)) return rejected<std::vector<float>>("Invalid vector", eve::DiagnosticCode::InvalidArgument);
117 auto v = native->GetWorldTransform() * JPH::Vec3(x, y, z);
118 return R::success({static_cast<float>(v.GetX()), static_cast<float>(v.GetY()), static_cast<float>(v.GetZ())});
119}
121 using R [[maybe_unused]] = eve::Result<PhysicsVector3D>;
122 if (!isValid()) return rejected<PhysicsVector3D>("Object expired", eve::DiagnosticCode::StaleHandle);
123 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
124 if (!isFinite(x, y, z)) return rejected<PhysicsVector3D>("Invalid vector", eve::DiagnosticCode::InvalidArgument);
125 auto v = native->GetWorldTransform() * JPH::Vec3(x, y, z);
126 return R::success({static_cast<float>(v.GetX()), static_cast<float>(v.GetY()), static_cast<float>(v.GetZ())});
127}
129 using R [[maybe_unused]] = eve::Result<std::vector<float>>;
130 if (!isValid()) return rejected<std::vector<float>>("Object expired", eve::DiagnosticCode::StaleHandle);
131 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
132 if (!isFinite(x, y, z)) return rejected<std::vector<float>>("Invalid vector", eve::DiagnosticCode::InvalidArgument);
133 auto v = native->GetWorldTransform().Inversed() * JPH::RVec3(x, y, z);
134 return R::success({static_cast<float>(v.GetX()), static_cast<float>(v.GetY()), static_cast<float>(v.GetZ())});
135}
137 using R [[maybe_unused]] = eve::Result<PhysicsVector3D>;
138 if (!isValid()) return rejected<PhysicsVector3D>("Object expired", eve::DiagnosticCode::StaleHandle);
139 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
140 if (!isFinite(x, y, z)) return rejected<PhysicsVector3D>("Invalid vector", eve::DiagnosticCode::InvalidArgument);
141 auto v = native->GetWorldTransform().Inversed() * JPH::RVec3(x, y, z);
142 return R::success({static_cast<float>(v.GetX()), static_cast<float>(v.GetY()), static_cast<float>(v.GetZ())});
143}
145 using R [[maybe_unused]] = eve::Result<std::vector<float>>;
146 if (!isValid()) return rejected<std::vector<float>>("Object expired", eve::DiagnosticCode::StaleHandle);
147 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
148 if (!isFinite(x, y, z)) return rejected<std::vector<float>>("Invalid vector", eve::DiagnosticCode::InvalidArgument);
149 auto v = native->GetRotation() * JPH::Vec3(x, y, z);
150 return R::success({static_cast<float>(v.GetX()), static_cast<float>(v.GetY()), static_cast<float>(v.GetZ())});
151}
153 using R [[maybe_unused]] = eve::Result<PhysicsVector3D>;
154 if (!isValid()) return rejected<PhysicsVector3D>("Object expired", eve::DiagnosticCode::StaleHandle);
155 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
156 if (!isFinite(x, y, z)) return rejected<PhysicsVector3D>("Invalid vector", eve::DiagnosticCode::InvalidArgument);
157 auto v = native->GetRotation() * JPH::Vec3(x, y, z);
158 return R::success({static_cast<float>(v.GetX()), static_cast<float>(v.GetY()), static_cast<float>(v.GetZ())});
159}
161 using R [[maybe_unused]] = eve::Result<std::vector<float>>;
162 if (!isValid()) return rejected<std::vector<float>>("Object expired", eve::DiagnosticCode::StaleHandle);
163 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
164 if (!isFinite(x, y, z)) return rejected<std::vector<float>>("Invalid vector", eve::DiagnosticCode::InvalidArgument);
165 auto v = native->GetRotation().Conjugated() * JPH::Vec3(x, y, z);
166 return R::success({static_cast<float>(v.GetX()), static_cast<float>(v.GetY()), static_cast<float>(v.GetZ())});
167}
169 using R [[maybe_unused]] = eve::Result<std::vector<float>>;
170 if (!isValid()) return rejected<std::vector<float>>("Object expired", eve::DiagnosticCode::StaleHandle);
171 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
172 if (!isFinite(x, y, z)) return rejected<std::vector<float>>("Invalid vector", eve::DiagnosticCode::InvalidArgument);
173 auto v = native->GetPointVelocity(native->GetWorldTransform() * JPH::Vec3(x, y, z));
174 return R::success({static_cast<float>(v.GetX()), static_cast<float>(v.GetY()), static_cast<float>(v.GetZ())});
175}
177 using R [[maybe_unused]] = eve::Result<PhysicsVector3D>;
178 if (!isValid()) return rejected<PhysicsVector3D>("Object expired", eve::DiagnosticCode::StaleHandle);
179 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
180 if (!isFinite(x, y, z)) return rejected<PhysicsVector3D>("Invalid vector", eve::DiagnosticCode::InvalidArgument);
181 auto v = native->GetPointVelocity(native->GetWorldTransform() * JPH::Vec3(x, y, z));
182 return R::success({static_cast<float>(v.GetX()), static_cast<float>(v.GetY()), static_cast<float>(v.GetZ())});
183}
185 using R [[maybe_unused]] = eve::Result<std::vector<float>>;
186 if (!isValid()) return rejected<std::vector<float>>("Object expired", eve::DiagnosticCode::StaleHandle);
187 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
188 if (!isFinite(x, y, z)) return rejected<std::vector<float>>("Invalid vector", eve::DiagnosticCode::InvalidArgument);
189 auto v = native->GetPointVelocity(JPH::RVec3(x, y, z));
190 return R::success({static_cast<float>(v.GetX()), static_cast<float>(v.GetY()), static_cast<float>(v.GetZ())});
191}
192eve::Result<void> BodyBackend::applyForce(float fx, float fy, float fz) {
193 using R [[maybe_unused]] = eve::Result<void>;
194 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
195 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
196 if (!isFinite(fx, fy, fz)) return rejected<void>("Invalid force or impulse", eve::DiagnosticCode::InvalidArgument);
197 if (native->IsDynamic()) body->api().AddForce(body->id, {fx, fy, fz});
198 return success();
199}
200eve::Result<void> BodyBackend::applyForceAt(float fx, float fy, float fz, float x, float y, float z) {
201 using R [[maybe_unused]] = eve::Result<void>;
202 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
203 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
204 if (!isFinite(fx, fy, fz) || !isFinite(x, y, z))
205 return rejected<void>("Invalid force or impulse", eve::DiagnosticCode::InvalidArgument);
206 if (native->IsDynamic()) body->api().AddForce(body->id, {fx, fy, fz}, JPH::RVec3(x, y, z));
207 return success();
208}
209eve::Result<void> BodyBackend::applyTorque(float tx, float ty, float tz) {
210 using R [[maybe_unused]] = eve::Result<void>;
211 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
212 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
213 if (!isFinite(tx, ty, tz)) return rejected<void>("Invalid force or impulse", eve::DiagnosticCode::InvalidArgument);
214 if (native->IsDynamic()) body->api().AddTorque(body->id, {tx, ty, tz});
215 return success();
216}
218 using R [[maybe_unused]] = eve::Result<void>;
219 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
220 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
221 if (!isFinite(ix, iy, iz)) return rejected<void>("Invalid force or impulse", eve::DiagnosticCode::InvalidArgument);
222 if (native->IsDynamic()) body->api().AddImpulse(body->id, {ix, iy, iz});
223 return success();
224}
225eve::Result<void> BodyBackend::applyLinearImpulseAt(float ix, float iy, float iz, float x, float y, float z) {
226 using R [[maybe_unused]] = eve::Result<void>;
227 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
228 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
229 if (!isFinite(ix, iy, iz) || !isFinite(x, y, z))
230 return rejected<void>("Invalid force or impulse", eve::DiagnosticCode::InvalidArgument);
231 if (native->IsDynamic()) body->api().AddImpulse(body->id, {ix, iy, iz}, JPH::RVec3(x, y, z));
232 return success();
233}
235 using R [[maybe_unused]] = eve::Result<void>;
236 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
237 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
238 if (!isFinite(ix, iy, iz)) return rejected<void>("Invalid force or impulse", eve::DiagnosticCode::InvalidArgument);
239 if (native->IsDynamic()) body->api().AddAngularImpulse(body->id, {ix, iy, iz});
240 return success();
241}
242eve::Result<void> BodyBackend::applyLocalForce(float vx, float vy, float vz, float x, float y, float z) {
243 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
244 if (!isFinite(vx, vy, vz) || !isFinite(x, y, z))
245 return rejected<void>("Invalid local force", eve::DiagnosticCode::InvalidArgument);
246 const auto v = body->api().GetRotation(body->id) * JPH::Vec3(vx, vy, vz);
247 const auto p = body->api().GetWorldTransform(body->id) * JPH::Vec3(x, y, z);
248 return applyForceAt(v.GetX(), v.GetY(), v.GetZ(), float(p.GetX()), float(p.GetY()), float(p.GetZ()));
249}
251 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
252 if (!isFinite(vx, vy, vz)) return rejected<void>("Invalid local force", eve::DiagnosticCode::InvalidArgument);
253 const auto v = body->api().GetRotation(body->id) * JPH::Vec3(vx, vy, vz);
254 return applyForce(v.GetX(), v.GetY(), v.GetZ());
255}
257 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
258 if (!isFinite(vx, vy, vz)) return rejected<void>("Invalid local force", eve::DiagnosticCode::InvalidArgument);
259 const auto v = body->api().GetRotation(body->id) * JPH::Vec3(vx, vy, vz);
260 return applyTorque(v.GetX(), v.GetY(), v.GetZ());
261}
262eve::Result<void> BodyBackend::applyLocalLinearImpulse(float vx, float vy, float vz, float x, float y, float z) {
263 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
264 if (!isFinite(vx, vy, vz) || !isFinite(x, y, z))
265 return rejected<void>("Invalid local force", eve::DiagnosticCode::InvalidArgument);
266 const auto v = body->api().GetRotation(body->id) * JPH::Vec3(vx, vy, vz);
267 const auto p = body->api().GetWorldTransform(body->id) * JPH::Vec3(x, y, z);
268 return applyLinearImpulseAt(v.GetX(), v.GetY(), v.GetZ(), float(p.GetX()), float(p.GetY()), float(p.GetZ()));
269}
271 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
272 if (!isFinite(vx, vy, vz)) return rejected<void>("Invalid local force", eve::DiagnosticCode::InvalidArgument);
273 const auto v = body->api().GetRotation(body->id) * JPH::Vec3(vx, vy, vz);
274 return applyLinearImpulse(v.GetX(), v.GetY(), v.GetZ());
275}
277 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
278 if (!isFinite(vx, vy, vz)) return rejected<void>("Invalid local force", eve::DiagnosticCode::InvalidArgument);
279 const auto v = body->api().GetRotation(body->id) * JPH::Vec3(vx, vy, vz);
280 return applyAngularImpulse(v.GetX(), v.GetY(), v.GetZ());
281}
282eve::Result<void> BodyBackend::setTargetTransform(float x, float y, float z, float qx, float qy, float qz, float qw,
283 float timeStep) {
284 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
285 JPH::Quat q(qx, qy, qz, qw);
286 if (!isFinite(x, y, z) || !std::isfinite(timeStep) || timeStep <= 0 || !std::isfinite(q.LengthSq()) ||
287 q.LengthSq() < 1.e-12f)
288 return rejected<void>("Invalid target transform", eve::DiagnosticCode::InvalidArgument);
289 if (body->api().GetMotionType(body->id) != JPH::EMotionType::Kinematic)
290 return rejected<void>("Target transform requires a kinematic body", eve::DiagnosticCode::InvalidArgument);
291 body->api().MoveKinematic(body->id, {x, y, z}, q.Normalized(), timeStep);
292 return success();
293}
295 using R [[maybe_unused]] = eve::Result<void>;
296 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
297 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
298 if (!std::isfinite(damping) || damping < 0)
299 return rejected<void>("Invalid damping", eve::DiagnosticCode::InvalidArgument);
300 native->GetMotionPropertiesUnchecked()->SetLinearDamping(damping);
301 return success();
302}
304 using R [[maybe_unused]] = eve::Result<float>;
305 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
306 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
307 return R::success(native->GetMotionPropertiesUnchecked()->GetLinearDamping());
308}
310 using R [[maybe_unused]] = eve::Result<void>;
311 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
312 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
313 if (!std::isfinite(damping) || damping < 0)
314 return rejected<void>("Invalid damping", eve::DiagnosticCode::InvalidArgument);
315 native->GetMotionPropertiesUnchecked()->SetAngularDamping(damping);
316 return success();
317}
319 using R [[maybe_unused]] = eve::Result<float>;
320 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
321 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
322 return R::success(native->GetMotionPropertiesUnchecked()->GetAngularDamping());
323}
325 using R [[maybe_unused]] = eve::Result<void>;
326 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
327 if (!std::isfinite(scale)) return rejected<void>("Invalid gravity scale", eve::DiagnosticCode::InvalidArgument);
328 body->api().SetGravityFactor(body->id, scale);
329 return success();
330}
332 using R [[maybe_unused]] = eve::Result<float>;
333 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
334 return R::success(body->api().GetGravityFactor(body->id));
335}
337 using R [[maybe_unused]] = eve::Result<void>;
338 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
339 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
340 body->sleepEnabled = enabled;
341 body->quietSeconds = 0;
342 native->SetAllowSleeping(enabled && !body->customSleepThreshold);
343 return success();
344}
346 using R [[maybe_unused]] = eve::Result<bool>;
347 if (!isValid()) return rejected<bool>("Object expired", eve::DiagnosticCode::StaleHandle);
348 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
349 return R::success(body->customSleepThreshold ? body->sleepEnabled : native->GetAllowSleeping());
350}
352 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
353 if (!std::isfinite(threshold) || threshold < 0)
354 return rejected<void>("Invalid sleep threshold", eve::DiagnosticCode::InvalidArgument);
355 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
356 if (!body->customSleepThreshold) body->sleepEnabled = native->GetAllowSleeping();
357 body->customSleepThreshold = true;
358 body->sleepThreshold = threshold;
359 body->quietSeconds = 0;
360 native->SetAllowSleeping(false);
361 return success();
362}
364 if (!isValid()) return rejected<float>("Body expired", eve::DiagnosticCode::StaleHandle);
366 body->customSleepThreshold ? body->sleepThreshold
367 : body->world.lock()->system.GetPhysicsSettings().mPointVelocitySleepThreshold);
368}
369
370eve::Result<void> BodyBackend::setMotionLocks(bool linearX, bool linearY, bool linearZ, bool angularX, bool angularY,
371 bool angularZ) {
372 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
373 int bits = (!linearX ? 1 : 0) | (!linearY ? 2 : 0) | (!linearZ ? 4 : 0) | (!angularX ? 8 : 0) |
374 (!angularY ? 16 : 0) | (!angularZ ? 32 : 0);
375 if (!bits) return rejected<void>("Jolt requires at least one movable degree of freedom; use a static body");
376 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
377 body->dofs = static_cast<JPH::EAllowedDOFs>(bits);
378 if (!native->GetShape()->MustBeStatic())
379 native->GetMotionPropertiesUnchecked()->SetMassProperties(body->dofs, body->mass);
380 return success();
381}
383 if (!isValid()) return rejected<bool>("Body expired", eve::DiagnosticCode::StaleHandle);
384 return eve::Result<bool>::success((static_cast<int>(body->dofs) & 1) == 0);
385}
387 if (!isValid()) return rejected<bool>("Body expired", eve::DiagnosticCode::StaleHandle);
388 return eve::Result<bool>::success((static_cast<int>(body->dofs) & 2) == 0);
389}
391 if (!isValid()) return rejected<bool>("Body expired", eve::DiagnosticCode::StaleHandle);
392 return eve::Result<bool>::success((static_cast<int>(body->dofs) & 4) == 0);
393}
395 if (!isValid()) return rejected<bool>("Body expired", eve::DiagnosticCode::StaleHandle);
396 return eve::Result<bool>::success((static_cast<int>(body->dofs) & 8) == 0);
397}
399 if (!isValid()) return rejected<bool>("Body expired", eve::DiagnosticCode::StaleHandle);
400 return eve::Result<bool>::success((static_cast<int>(body->dofs) & 16) == 0);
401}
403 if (!isValid()) return rejected<bool>("Body expired", eve::DiagnosticCode::StaleHandle);
404 return eve::Result<bool>::success((static_cast<int>(body->dofs) & 32) == 0);
405}
406eve::Result<void> BodyBackend::setMassProperties(float mass, float cx, float cy, float cz, float xx, float yy, float zz,
407 float xy, float xz, float yz) {
408 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
409 if (body->api().GetMotionType(body->id) != JPH::EMotionType::Dynamic || !std::isfinite(mass) || mass <= 0 ||
410 !isFinite(cx, cy, cz) || !isFinite(xx, yy, zz) || !isFinite(xy, xz, yz))
411 return rejected<void>("Invalid mass properties", eve::DiagnosticCode::InvalidArgument);
412 JPH::MassProperties properties;
413 properties.mMass = mass;
414 properties.mInertia =
415 JPH::Mat44(JPH::Vec4(xx, xy, xz, 0), JPH::Vec4(xy, yy, yz, 0), JPH::Vec4(xz, yz, zz, 0), JPH::Vec4(0, 0, 0, 1));
416 JPH::Mat44 rotation;
417 JPH::Vec3 diagonal;
418 if (!properties.DecomposePrincipalMomentsOfInertia(rotation, diagonal) ||
419 !isFinite(diagonal.GetX(), diagonal.GetY(), diagonal.GetZ()) || diagonal.ReduceMin() <= 0)
420 return rejected<void>("Inertia must be positive definite", eve::DiagnosticCode::InvalidArgument);
421 auto original = body->api().GetShape(body->id);
422 const auto previous = original->GetCenterOfMass();
423 if (original->GetSubType() == JPH::EShapeSubType::OffsetCenterOfMass)
424 original = static_cast<const JPH::OffsetCenterOfMassShape*>(original.GetPtr())->GetInnerShape();
425 JPH::RefConst<JPH::Shape> adjusted =
426 new JPH::OffsetCenterOfMassShape(original, JPH::Vec3(cx, cy, cz) - original->GetCenterOfMass());
427 body->api().SetShape(body->id, adjusted, false, JPH::EActivation::Activate);
428 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
429 native->GetMotionPropertiesUnchecked()->SetMassProperties(body->dofs, properties);
430 body->mass = properties;
431 notifyJointShapeChanged(*body->world.lock(), body->id, adjusted->GetCenterOfMass() - previous);
432 return success();
433}
435 if (!isValid()) return rejected<void>("Body expired", eve::DiagnosticCode::StaleHandle);
436 return replaceShapes(*body, body->shapes);
437}
439 if (!isValid()) return rejected<float>("Body expired", eve::DiagnosticCode::StaleHandle);
441 body->api().GetMotionType(body->id) == JPH::EMotionType::Dynamic ? body->mass.mInertia(0, 0) : 0.f);
442}
444 if (!isValid()) return rejected<float>("Body expired", eve::DiagnosticCode::StaleHandle);
446 body->api().GetMotionType(body->id) == JPH::EMotionType::Dynamic ? body->mass.mInertia(1, 1) : 0.f);
447}
449 if (!isValid()) return rejected<float>("Body expired", eve::DiagnosticCode::StaleHandle);
451 body->api().GetMotionType(body->id) == JPH::EMotionType::Dynamic ? body->mass.mInertia(2, 2) : 0.f);
452}
454 if (!isValid()) return rejected<float>("Body expired", eve::DiagnosticCode::StaleHandle);
456 body->api().GetMotionType(body->id) == JPH::EMotionType::Dynamic ? body->mass.mInertia(0, 1) : 0.f);
457}
459 if (!isValid()) return rejected<float>("Body expired", eve::DiagnosticCode::StaleHandle);
461 body->api().GetMotionType(body->id) == JPH::EMotionType::Dynamic ? body->mass.mInertia(0, 2) : 0.f);
462}
464 if (!isValid()) return rejected<float>("Body expired", eve::DiagnosticCode::StaleHandle);
466 body->api().GetMotionType(body->id) == JPH::EMotionType::Dynamic ? body->mass.mInertia(1, 2) : 0.f);
467}
469 using R [[maybe_unused]] = eve::Result<float>;
470 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
471 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
472 return R::success(native->GetShape()->GetCenterOfMass().GetX());
473}
475 using R [[maybe_unused]] = eve::Result<float>;
476 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
477 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
478 return R::success(native->GetShape()->GetCenterOfMass().GetY());
479}
481 using R [[maybe_unused]] = eve::Result<float>;
482 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
483 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
484 return R::success(native->GetShape()->GetCenterOfMass().GetZ());
485}
487 using R [[maybe_unused]] = eve::Result<float>;
488 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
489 return R::success(static_cast<float>(body->api().GetCenterOfMassPosition(body->id).GetX()));
490}
492 using R [[maybe_unused]] = eve::Result<float>;
493 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
494 return R::success(static_cast<float>(body->api().GetCenterOfMassPosition(body->id).GetY()));
495}
497 using R [[maybe_unused]] = eve::Result<float>;
498 if (!isValid()) return rejected<float>("Object expired", eve::DiagnosticCode::StaleHandle);
499 return R::success(static_cast<float>(body->api().GetCenterOfMassPosition(body->id).GetZ()));
500}
501eve::Result<void> BodyBackend::setType(const std::string& bodyType) {
502 using R [[maybe_unused]] = eve::Result<void>;
503 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
504 if (bodyType != "static" && bodyType != "dynamic" && bodyType != "kinematic")
505 return rejected<void>("Invalid body type", eve::DiagnosticCode::InvalidArgument);
506 auto type = bodyType == "static" ? JPH::EMotionType::Static
507 : bodyType == "dynamic" ? JPH::EMotionType::Dynamic
508 : JPH::EMotionType::Kinematic;
509 if (type != JPH::EMotionType::Static)
510 for (const auto& s : body->shapes)
511 if (s->native->MustBeStatic())
512 return rejected<void>("Concave shapes require a static body", eve::DiagnosticCode::InvalidArgument);
513 if (body->api().GetMotionType(body->id) == type) return success();
514 // Box3D recomputes mass when changing type, including any deferred density edits.
515 auto rebuilt = replaceShapes(*body, body->shapes);
516 if (!rebuilt) return rebuilt;
517 body->api().SetMotionType(body->id, type, JPH::EActivation::Activate);
518 body->api().SetObjectLayer(body->id, type == JPH::EMotionType::Static ? 0 : 1);
519 return success();
520}
522 using R [[maybe_unused]] = eve::Result<std::string>;
523 if (!isValid()) return rejected<std::string>("Object expired", eve::DiagnosticCode::StaleHandle);
524 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
525 return R::success(native->IsStatic() ? "static" : native->IsKinematic() ? "kinematic" : "dynamic");
526}
528 using R [[maybe_unused]] = eve::Result<void>;
529 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
530 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
531 const int next = fixed ? (static_cast<int>(body->dofs) & 7) : (static_cast<int>(body->dofs) | 56);
532 if (next == 0) return rejected<void>("Jolt requires at least one movable degree of freedom; use a static body");
533 body->dofs = static_cast<JPH::EAllowedDOFs>(next);
534 if (!native->GetShape()->MustBeStatic())
535 native->GetMotionPropertiesUnchecked()->SetMassProperties(body->dofs, body->mass);
536 return success();
537}
539 using R [[maybe_unused]] = eve::Result<bool>;
540 if (!isValid()) return rejected<bool>("Object expired", eve::DiagnosticCode::StaleHandle);
541 return R::success((static_cast<int>(body->dofs) & 56) == 0);
542}
544 using R [[maybe_unused]] = eve::Result<void>;
545 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
546 if (active != body->active) {
547 if (active)
548 body->api().AddBody(body->id, JPH::EActivation::Activate);
549 else
550 body->api().RemoveBody(body->id);
551 body->active = active;
552 }
553 return success();
554}
556 using R [[maybe_unused]] = eve::Result<bool>;
557 if (!isValid()) return rejected<bool>("Object expired", eve::DiagnosticCode::StaleHandle);
558 return R::success(body->active);
559}
561 using R [[maybe_unused]] = eve::Result<void>;
562 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
563 body->bullet = bullet;
564 body->api().SetMotionQuality(body->id, bullet && body->world.lock()->continuousCollision
565 ? JPH::EMotionQuality::LinearCast
566 : JPH::EMotionQuality::Discrete);
567 return success();
568}
570 using R [[maybe_unused]] = eve::Result<bool>;
571 if (!isValid()) return rejected<bool>("Object expired", eve::DiagnosticCode::StaleHandle);
572 return R::success(body->bullet);
573}
575 using R [[maybe_unused]] = eve::Result<void>;
576 if (!isValid()) return rejected<void>("Object expired", eve::DiagnosticCode::StaleHandle);
577 auto* native = body->world.lock()->system.GetBodyLockInterfaceNoLock().TryGetBody(body->id);
578 if (body->active && !native->IsStatic()) {
579 if (awake)
580 body->api().ActivateBody(body->id);
581 else
582 body->api().DeactivateBody(body->id);
583 }
584 return success();
585}
587 using R [[maybe_unused]] = eve::Result<bool>;
588 if (!isValid()) return rejected<bool>("Object expired", eve::DiagnosticCode::StaleHandle);
589 return R::success(body->api().IsActive(body->id));
590}
591} // namespace eve::physics::jolt
double z
double y
double x
bool & active
const std::string & s
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
bool awake
glm::vec4 p[6]
std::array< double, 10 > q
float v
std::array< float, 4 > rotation
std::array< float, 3 > scale
std::map< std::string, PropertyRule > properties
std::shared_ptr< const OceanSprayFrame > previous
bool diagonal
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
eve::Result< float > getZ() const override
getZ; inherits the class lifetime and failure contract.
eve::Result< float > getSleepThreshold() const override
getSleepThreshold; inherits the class lifetime and failure contract.
eve::Result< float > getLocalCenterY() const override
getLocalCenterY; inherits the class lifetime and failure contract.
eve::Result< void > setActive(bool active) override
setActive; inherits the class lifetime and failure contract.
eve::Result< void > setMassProperties(float mass, float centerX, float centerY, float centerZ, float inertiaXX, float inertiaYY, float inertiaZZ, float inertiaXY, float inertiaXZ, float inertiaYZ) override
setMassProperties; inherits the class lifetime and failure contract.
eve::Result< float > getY() const override
getY; inherits the class lifetime and failure contract.
eve::Result< float > getInertiaXX() const override
getInertiaXX; inherits the class lifetime and failure contract.
eve::Result< float > getWorldCenterX() const override
getWorldCenterX; inherits the class lifetime and failure contract.
eve::Result< void > applyLocalTorque(float tx, float ty, float tz) override
applyLocalTorque; inherits the class lifetime and failure contract.
eve::Result< std::vector< float > > localToWorldPoint(float x, float y, float z) const override
localToWorldPoint; inherits the class lifetime and failure contract. @cost Three scalar values copied...
eve::Result< bool > isAngularZLocked() const override
isAngularZLocked; inherits the class lifetime and failure contract.
eve::Result< void > applyAngularImpulse(float ix, float iy, float iz) override
applyAngularImpulse; inherits the class lifetime and failure contract.
eve::Result< void > setGravityScale(float scale) override
setGravityScale; inherits the class lifetime and failure contract.
eve::Result< float > getAngularVelocityY() const override
getAngularVelocityY; inherits the class lifetime and failure contract.
eve::Result< float > getRotY() const override
getRotY; inherits the class lifetime and failure contract.
eve::Result< PhysicsVector3D > worldToLocalPointOwned(float x, float y, float z) const override
worldToLocalPointOwned; inherits the class lifetime and failure contract.
eve::Result< PhysicsVector3D > getLocalPointVelocityOwned(float x, float y, float z) const override
getLocalPointVelocityOwned; inherits the class lifetime and failure contract.
eve::Result< void > applyLinearImpulse(float ix, float iy, float iz) override
applyLinearImpulse; inherits the class lifetime and failure contract.
eve::Result< float > getMass() const override
getMass; inherits the class lifetime and failure contract.
eve::Result< float > getGravityScale() const override
getGravityScale; inherits the class lifetime and failure contract.
eve::Result< float > getAngularVelocityX() const override
getAngularVelocityX; inherits the class lifetime and failure contract.
eve::Result< float > getLinearVelocityX() const override
getLinearVelocityX; inherits the class lifetime and failure contract.
eve::Result< void > setAngularDamping(float damping) override
setAngularDamping; inherits the class lifetime and failure contract.
eve::Result< bool > isSleepEnabled() const override
isSleepEnabled; inherits the class lifetime and failure contract.
eve::Result< float > getInertiaYY() const override
getInertiaYY; inherits the class lifetime and failure contract.
eve::Result< void > applyLocalLinearImpulseToCenter(float ix, float iy, float iz) override
applyLocalLinearImpulseToCenter; inherits the class lifetime and failure contract.
eve::Result< void > setPosition(float x, float y, float z) override
setPosition; inherits the class lifetime and failure contract.
eve::Result< PhysicsVector3D > localToWorldVectorOwned(float x, float y, float z) const override
localToWorldVectorOwned; inherits the class lifetime and failure contract.
eve::Result< bool > isAwake() const override
isAwake; inherits the class lifetime and failure contract.
eve::Result< void > setRotation(float qx, float qy, float qz, float qw) override
setRotation; inherits the class lifetime and failure contract.
eve::Result< bool > isLinearYLocked() const override
isLinearYLocked; inherits the class lifetime and failure contract.
eve::Result< void > setTargetTransform(float x, float y, float z, float qx, float qy, float qz, float qw, float timeStep) override
setTargetTransform; inherits the class lifetime and failure contract.
eve::Result< void > applyForceAt(float fx, float fy, float fz, float x, float y, float z) override
applyForceAt; inherits the class lifetime and failure contract.
eve::Result< void > setLinearVelocity(float vx, float vy, float vz) override
setLinearVelocity; inherits the class lifetime and failure contract.
eve::Result< void > setBullet(bool bullet) override
setBullet; inherits the class lifetime and failure contract.
eve::Result< void > setAngularVelocity(float wx, float wy, float wz) override
setAngularVelocity; inherits the class lifetime and failure contract.
eve::Result< bool > isActive() const override
isActive; inherits the class lifetime and failure contract.
eve::Result< float > getLinearVelocityZ() const override
getLinearVelocityZ; inherits the class lifetime and failure contract.
eve::Result< float > getX() const override
getX; inherits the class lifetime and failure contract.
eve::Result< float > getRotX() const override
getRotX; inherits the class lifetime and failure contract.
eve::Result< float > getInertiaZZ() const override
getInertiaZZ; inherits the class lifetime and failure contract.
eve::Result< void > setType(const std::string &bodyType) override
setType; inherits the class lifetime and failure contract.
eve::Result< float > getInertiaXY() const override
getInertiaXY; inherits the class lifetime and failure contract.
eve::Result< float > getWorldCenterZ() const override
getWorldCenterZ; inherits the class lifetime and failure contract.
eve::Result< void > applyTorque(float tx, float ty, float tz) override
applyTorque; inherits the class lifetime and failure contract.
eve::Result< bool > isLinearZLocked() const override
isLinearZLocked; inherits the class lifetime and failure contract.
eve::Result< float > getAngularDamping() const override
getAngularDamping; inherits the class lifetime and failure contract.
eve::Result< void > applyLocalAngularImpulse(float ix, float iy, float iz) override
applyLocalAngularImpulse; inherits the class lifetime and failure contract.
eve::Result< float > getLocalCenterX() const override
getLocalCenterX; inherits the class lifetime and failure contract.
eve::Result< float > getRotW() const override
getRotW; inherits the class lifetime and failure contract.
eve::Result< void > setLinearDamping(float damping) override
setLinearDamping; inherits the class lifetime and failure contract.
eve::Result< float > getInertiaYZ() const override
getInertiaYZ; inherits the class lifetime and failure contract.
eve::Result< float > getInertiaXZ() const override
getInertiaXZ; inherits the class lifetime and failure contract.
eve::Result< std::vector< float > > getWorldPointVelocity(float x, float y, float z) const override
getWorldPointVelocity; inherits the class lifetime and failure contract. @cost Three scalar values co...
eve::Result< bool > isLinearXLocked() const override
isLinearXLocked; inherits the class lifetime and failure contract.
eve::Result< float > getAngularVelocityZ() const override
getAngularVelocityZ; inherits the class lifetime and failure contract.
eve::Result< void > setAwake(bool awake) override
setAwake; inherits the class lifetime and failure contract.
eve::Result< PhysicsVector3D > localToWorldPointOwned(float x, float y, float z) const override
localToWorldPointOwned; inherits the class lifetime and failure contract.
eve::Result< float > getLinearDamping() const override
getLinearDamping; inherits the class lifetime and failure contract.
eve::Result< bool > isAngularXLocked() const override
isAngularXLocked; inherits the class lifetime and failure contract.
std::shared_ptr< BodyState > body
Definition JoltBody.h:10
eve::Result< float > getLocalCenterZ() const override
getLocalCenterZ; inherits the class lifetime and failure contract.
eve::Result< std::vector< float > > worldToLocalVector(float x, float y, float z) const override
worldToLocalVector; inherits the class lifetime and failure contract. @cost Three scalar values copie...
eve::Result< bool > isBullet() const override
isBullet; inherits the class lifetime and failure contract.
eve::Result< bool > isAngularYLocked() const override
isAngularYLocked; inherits the class lifetime and failure contract.
eve::Result< void > applyLocalLinearImpulse(float ix, float iy, float iz, float x, float y, float z) override
applyLocalLinearImpulse; inherits the class lifetime and failure contract.
eve::Result< void > applyLocalForceToCenter(float fx, float fy, float fz) override
applyLocalForceToCenter; inherits the class lifetime and failure contract.
eve::Result< void > applyLinearImpulseAt(float ix, float iy, float iz, float x, float y, float z) override
applyLinearImpulseAt; inherits the class lifetime and failure contract.
bool isValid() const noexcept override
Whether the native body remains alive.
Definition JoltBody.cpp:9
eve::Result< float > getWorldCenterY() const override
getWorldCenterY; inherits the class lifetime and failure contract.
eve::Result< void > setSleepThreshold(float threshold) override
setSleepThreshold; inherits the class lifetime and failure contract.
eve::Result< std::string > getType() const override
getType; inherits the class lifetime and failure contract.
eve::Result< void > resetMassProperties() override
resetMassProperties; inherits the class lifetime and failure contract.
eve::Result< float > getLinearVelocityY() const override
getLinearVelocityY; inherits the class lifetime and failure contract.
eve::Result< std::vector< float > > localToWorldVector(float x, float y, float z) const override
localToWorldVector; inherits the class lifetime and failure contract. @cost Three scalar values copie...
eve::Result< bool > isFixedRotation() const override
isFixedRotation; inherits the class lifetime and failure contract.
eve::Result< void > setMotionLocks(bool linearX, bool linearY, bool linearZ, bool angularX, bool angularY, bool angularZ) override
setMotionLocks; inherits the class lifetime and failure contract.
eve::Result< float > getRotZ() const override
getRotZ; inherits the class lifetime and failure contract.
eve::Result< std::vector< float > > worldToLocalPoint(float x, float y, float z) const override
worldToLocalPoint; inherits the class lifetime and failure contract. @cost Three scalar values copied...
eve::Result< void > setSleepEnabled(bool enabled) override
setSleepEnabled; inherits the class lifetime and failure contract.
eve::Result< void > applyForce(float fx, float fy, float fz) override
applyForce; inherits the class lifetime and failure contract.
eve::Result< void > setFixedRotation(bool fixed) override
setFixedRotation; inherits the class lifetime and failure contract.
eve::Result< void > applyLocalForce(float fx, float fy, float fz, float x, float y, float z) override
applyLocalForce; inherits the class lifetime and failure contract.
eve::Result< std::vector< float > > getLocalPointVelocity(float x, float y, float z) const override
getLocalPointVelocity; inherits the class lifetime and failure contract. @cost Three scalar values co...
eve::Result< T > rejected(const char *operation, eve::DiagnosticCode code=eve::DiagnosticCode::Unsupported)
void notifyJointShapeChanged(State &state, JPH::BodyID id, JPH::Vec3Arg delta)
Recomputes constraint COM offsets after a successful native shape replacement.
eve::Result< void > success()
eve::Result< void > replaceShapes(BodyState &, const std::vector< std::shared_ptr< ShapeState > > &)
Definition JoltShape.cpp:77
bool isFinite(float x, float y, float z)
bool enabled