载入中...
搜索中...
未找到
JoltJoint.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 {
8JPH::uint64 pairKey(JPH::BodyID a, JPH::BodyID b) {
9 auto x = a.GetIndexAndSequenceNumber(), y = b.GetIndexAndSequenceNumber();
10 return (JPH::uint64(std::min(x, y)) << 32) | std::max(x, y);
11}
13void JointBackend::bindOwner(Joint3D& owner) noexcept { joint->owner = &owner; }
14bool JointBackend::isValid() const noexcept {
15 auto world = joint->world.lock();
16 auto a = joint->a.lock();
17 auto b = joint->b.lock();
18 return joint->alive && world && world->alive && a && b && a->valid() && b->valid();
19}
20void JointBackend::invalidate() noexcept { destroy(); }
21void JointBackend::destroy() noexcept {
22 if (joint->alive) {
23 auto world = joint->world.lock();
24 auto a = joint->a.lock();
25 auto b = joint->b.lock();
26 if (world && world->alive) {
27 if (joint->native) world->system.RemoveConstraint(joint->native);
28 if (joint->velocity) world->system.RemoveConstraint(joint->velocity);
29 if (!joint->collide && a && b) {
30 auto i = world->suppressedPairs.find(pairKey(a->id, b->id));
31 if (i != world->suppressedPairs.end() && --i->second == 0) world->suppressedPairs.erase(i);
32 if (a->valid()) a->api().InvalidateContactCache(a->id);
33 if (b->valid()) b->api().InvalidateContactCache(b->id);
34 }
35 }
36 }
37 joint->alive = false;
38 joint->owner = nullptr;
39 joint->native = nullptr;
40 joint->velocity = nullptr;
41}
42eve::Result<void> JointBackend::wakeBodies() const {
43 auto a = joint->a.lock(), b = joint->b.lock();
44 if (a && a->active) a->api().ActivateBody(a->id);
45 if (b && b->active) b->api().ActivateBody(b->id);
46 return success();
47}
48eve::Result<void> JointBackend::requireKind(JointKind3D kind) const {
49 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
50 if (joint->kind != kind) return rejected<void>("Wrong joint kind", eve::DiagnosticCode::InvalidArgument);
51 return success();
52}
53JPH::SixDOFConstraint& JointBackend::six() const { return static_cast<JPH::SixDOFConstraint&>(*joint->native); }
54JPH::SixDOFConstraint& JointBackend::drive() const { return *joint->velocity; }
55JPH::RMat44 JointBackend::frame(bool second) const {
56 auto body = second ? joint->b.lock() : joint->a.lock();
57 return body->api().GetCenterOfMassTransform(body->id) *
58 (second ? joint->native->GetConstraintToBody2Matrix() : joint->native->GetConstraintToBody1Matrix());
59}
61 if (!isValid()) return rejected<JointFrames3D>("Joint expired", eve::DiagnosticCode::StaleHandle);
63}
65 if (!isValid()) return rejected<void>("Joint expired", eve::DiagnosticCode::StaleHandle);
66 if (collide == joint->collide) return success();
67 auto world = joint->world.lock();
68 auto a = joint->a.lock();
69 auto b = joint->b.lock();
70 auto key = pairKey(a->id, b->id);
71 if (!collide)
72 ++world->suppressedPairs[key];
73 else {
74 auto i = world->suppressedPairs.find(key);
75 if (i != world->suppressedPairs.end() && --i->second == 0) world->suppressedPairs.erase(i);
76 }
77 joint->collide = collide;
78 a->api().InvalidateContactCache(a->id);
79 b->api().InvalidateContactCache(b->id);
80 if (a->active) a->api().ActivateBody(a->id);
81 if (b->active) b->api().ActivateBody(b->id);
82 return success();
83}
85 if (!isValid()) return rejected<bool>("Joint expired", eve::DiagnosticCode::StaleHandle);
86 return eve::Result<bool>::success(joint->collide);
87}
88JPH::Vec3 JointBackend::force(bool torque) const {
89 if (!joint->native) return JPH::Vec3::sZero();
90 const float inverse = joint->world.lock()->inverseStep;
91 if (joint->kind == JointKind3D::Distance) {
92 if (torque) return JPH::Vec3::sZero();
93 const auto axis =
94 JPH::Vec3(frame(true).GetTranslation() - frame(false).GetTranslation()).NormalizedOr(JPH::Vec3::sAxisX());
95 return axis * distanceImpulse(*joint->native) * inverse;
96 }
97 auto value = torque ? six().GetTotalLambdaRotation() + six().GetTotalLambdaMotorRotation()
98 : six().GetTotalLambdaPosition() + six().GetTotalLambdaMotorTranslation();
99 // Fully locked SixDOF point/rotation parts report world-space impulses; individual axes report constraint space.
100 const bool locked = torque ? six().IsFixedAxis(JPH::SixDOFConstraint::EAxis::RotationX) &&
101 six().IsFixedAxis(JPH::SixDOFConstraint::EAxis::RotationY) &&
102 six().IsFixedAxis(JPH::SixDOFConstraint::EAxis::RotationZ)
103 : six().IsFixedAxis(JPH::SixDOFConstraint::EAxis::TranslationX) &&
104 six().IsFixedAxis(JPH::SixDOFConstraint::EAxis::TranslationY) &&
105 six().IsFixedAxis(JPH::SixDOFConstraint::EAxis::TranslationZ);
106 auto world = locked ? value : frame(false).Multiply3x3(value);
107 if (joint->velocity)
108 world += frame(false).Multiply3x3(torque ? drive().GetTotalLambdaMotorRotation()
109 : drive().GetTotalLambdaMotorTranslation());
110 return world * inverse;
111}
112void notifyJointShapeChanged(State& state, JPH::BodyID id, JPH::Vec3Arg delta) {
113 for (auto& weak : state.joints)
114 if (auto joint = weak.lock(); joint && joint->alive && joint->native) {
115 auto a = joint->a.lock(), b = joint->b.lock();
116 if ((a && a->id == id) || (b && b->id == id)) {
117 joint->native->NotifyShapeChanged(id, delta);
118 if (joint->velocity) joint->velocity->NotifyShapeChanged(id, delta);
119 }
120 }
121}
122} // namespace eve::physics::jolt
double y
double x
bool locked
SQObjectPtr key
Definition ECS.cpp:155
ForceVector torque
std::int32_t second
JointPoint2D axis
std::string id
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::shared_ptr< OceanGpuSpectrum > owner
ssq::Object frame
std::string body
Object::Ptr value
std::string kind
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
eve::Result< bool > getCollideConnected() const override
getCollideConnected; see the class lifetime, validation and failure contract.
Definition JoltJoint.cpp:84
std::shared_ptr< JointState > joint
Definition JoltJoint.h:27
void destroy() noexcept override
Idempotently destroys the constraint; no failure or callbacks.
Definition JoltJoint.cpp:21
eve::Result< JointFrames3D > localFrames() const override
localFrames; see the class lifetime, validation and failure contract.
Definition JoltJoint.cpp:60
void invalidate() noexcept override
Detaches before the parent destroys its constraints; no failure or callbacks.
Definition JoltJoint.cpp:20
eve::Result< void > setCollideConnected(bool collide) override
setCollideConnected; see the class lifetime, validation and failure contract.
Definition JoltJoint.cpp:64
bool isValid() const noexcept override
Whether the native constraint is still alive.
Definition JoltJoint.cpp:14
void bindOwner(Joint3D &) noexcept override
Binds its exclusive facade owner for internal attribution, on the simulation thread....
Definition JoltJoint.cpp:13
void notifyJointShapeChanged(State &state, JPH::BodyID id, JPH::Vec3Arg delta)
Recomputes constraint COM offsets after a successful native shape replacement.
eve::Result< void > success()
JPH::uint64 pairKey(JPH::BodyID a, JPH::BodyID b)
Counts suppression references for one native pair; sequence numbers prevent reuse aliases.
Definition JoltJoint.cpp:8
float distanceImpulse(const JPH::TwoBodyConstraint &value)
Returns the owning scalar sum of distance solver impulses after an update.
JointKind3D
Stable constraint kinds; numeric values are used by stress events.