载入中...
搜索中...
未找到
JoltWorldJoints.cpp
浏览该文件的文档.
1// eve-hot-path
2// Jolt requires its platform header before any other native header.
3#include <Jolt/Jolt.h>
4
5// Provider implementation dependencies.
8namespace eve::physics::jolt {
9namespace {
11JointResult createJoint(WorldBackend& world, Body3D& a, Body3D& b, JointKind3D kind, JPH::RVec3Arg anchorA,
12 JPH::RVec3Arg anchorB, JPH::Vec3Arg axis, JPH::Vec3Arg wheelAxis, float length, bool collide) {
13 if (!world.isValid())
14 return rejected<std::unique_ptr<IJointBackend3D>>("World expired", eve::DiagnosticCode::StaleHandle);
15 if (&a == &b || !a.isValid() || !b.isValid() || a.getWorld() != world.owner || b.getWorld() != world.owner ||
16 !isFinite(float(anchorA.GetX()), float(anchorA.GetY()), float(anchorA.GetZ())) ||
17 !isFinite(float(anchorB.GetX()), float(anchorB.GetY()), float(anchorB.GetZ())) ||
18 !std::isfinite(axis.LengthSq()) || axis.LengthSq() < 1.e-12f || !std::isfinite(length) || length < 0 ||
19 !std::isfinite(wheelAxis.LengthSq()) || wheelAxis.LengthSq() < 1.e-12f)
20 return rejected<std::unique_ptr<IJointBackend3D>>("Invalid joint definition",
22 auto record = std::make_shared<JointState>();
23 for (const auto& [id, body] : world.state->bodies) {
24 if (body->owner == &a) record->a = body;
25 if (body->owner == &b) record->b = body;
26 }
27 auto ba = record->a.lock(), bb = record->b.lock();
28 if (!ba || !bb)
29 return rejected<std::unique_ptr<IJointBackend3D>>("Bodies are not owned by this provider",
31 auto& na = *world.state->system.GetBodyLockInterfaceNoLock().TryGetBody(ba->id);
32 auto& nb = *world.state->system.GetBodyLockInterfaceNoLock().TryGetBody(bb->id);
33 record->world = world.state;
34 record->kind = kind;
35 record->length = length;
36 record->collide = true;
37 const auto direction = axis.Normalized();
38 // Portable frames keep the existing axis conventions used by snapshot reconstruction.
39 auto portableFrame = [&](const JPH::Body& body, JPH::RVec3Arg anchor, JPH::Vec3Arg localAxis,
40 JPH::Vec3Arg worldAxis) {
41 auto p = body.GetWorldTransform().InversedRotationTranslation() * anchor;
42 auto q = JPH::Quat::sFromTo(localAxis, body.GetRotation().Conjugated() * worldAxis);
43 if (kind == JointKind3D::Weld) q = body.GetRotation().Conjugated();
45 q = JPH::Quat::sIdentity();
46 return std::array<float, 7>{float(p.GetX()), float(p.GetY()), float(p.GetZ()), q.GetX(),
47 q.GetY(), q.GetZ(), q.GetW()};
48 };
49 record->frames.bodyA = portableFrame(
50 na, anchorA,
51 kind == JointKind3D::Prismatic || kind == JointKind3D::Wheel ? JPH::Vec3::sAxisX() : JPH::Vec3::sAxisZ(),
52 direction);
53 record->frames.bodyB =
54 portableFrame(nb, anchorB, kind == JointKind3D::Prismatic ? JPH::Vec3::sAxisX() : JPH::Vec3::sAxisZ(),
55 kind == JointKind3D::Wheel ? wheelAxis.Normalized() : direction);
57 JPH::DistanceConstraintSettings settings;
58 settings.mPoint1 = anchorA;
59 settings.mPoint2 = anchorB;
60 settings.mMinDistance = length;
61 settings.mMaxDistance = length;
62 record->native = createDistanceConstraint(na, nb, settings);
63 } else if (kind != JointKind3D::Filter) {
64 JPH::SixDOFConstraintSettings settings;
65 using A = JPH::SixDOFConstraintSettings::EAxis;
66 for (auto& motor : settings.mMotorSettings) {
67 motor.mSpringSettings = {JPH::ESpringMode::FrequencyAndDamping, 0, 0};
68 if (kind == JointKind3D::Motor) {
69 motor.SetForceLimit(0);
70 motor.SetTorqueLimit(0);
71 }
72 }
73 settings.mPosition1 = anchorA;
74 settings.mPosition2 = anchorB;
75 settings.mAxisX1 = settings.mAxisX2 = direction;
76 settings.mAxisY1 = settings.mAxisY2 = direction.GetNormalizedPerpendicular();
78 settings.mAxisX1 = settings.mAxisX2 = JPH::Vec3::sAxisX();
79 settings.mAxisY1 = settings.mAxisY2 = JPH::Vec3::sAxisY();
80 }
81 if (kind == JointKind3D::Wheel) {
82 auto spin = wheelAxis.Normalized();
83 if (std::abs(spin.Dot(direction)) > .999f)
84 return rejected<std::unique_ptr<IJointBackend3D>>("Wheel axes are parallel",
86 settings.mAxisX1 = settings.mAxisX2 = (spin - direction * spin.Dot(direction)).Normalized();
87 settings.mAxisY1 = settings.mAxisY2 = direction;
88 settings.mSwingType = JPH::ESwingType::Pyramid;
89 }
90 for (int i = 0; i < 6; ++i) settings.MakeFixedAxis(static_cast<A>(i));
91 if (kind == JointKind3D::Revolute) settings.MakeFreeAxis(A::RotationX);
92 if (kind == JointKind3D::Prismatic) settings.MakeFreeAxis(A::TranslationX);
94 for (int i = 3; i < 6; ++i) settings.MakeFreeAxis(static_cast<A>(i));
96 for (int i = 0; i < 6; ++i) settings.MakeFreeAxis(static_cast<A>(i));
98 for (int i = 0; i < 3; ++i) settings.MakeFreeAxis(static_cast<A>(i));
99 settings.MakeFreeAxis(A::RotationX);
100 }
101 if (kind == JointKind3D::Wheel) {
102 settings.MakeFreeAxis(A::TranslationY);
103 settings.MakeFreeAxis(A::RotationX);
104 settings.MakeFreeAxis(A::RotationY);
105 }
106 record->native = settings.Create(na, nb);
108 for (int i = 0; i < 6; ++i) settings.MakeFreeAxis(static_cast<A>(i));
109 record->velocity = static_cast<JPH::SixDOFConstraint*>(settings.Create(na, nb));
110 }
111 }
112 if (record->native) world.state->system.AddConstraint(record->native);
113 if (record->velocity) world.state->system.AddConstraint(record->velocity);
114 auto provider = std::make_unique<JointBackend>(record);
115 auto configured = provider->setCollideConnected(collide);
116 if (!configured) return JointResult::failure(configured.status());
117 std::erase_if(world.state->joints, [](const auto& weak) { return weak.expired(); });
118 world.state->joints.push_back(record);
119 if (kind == JointKind3D::Wheel) {
120 auto defaults = provider->setWheelSuspension(true, 1, .7f);
121 if (!defaults) return JointResult::failure(defaults.status());
122 }
124 auto defaults = provider->setParallelSpring(1, 1, FLT_MAX);
125 if (!defaults) return JointResult::failure(defaults.status());
126 }
127 return JointResult::success(std::move(provider));
128}
129} // namespace
131 float az, float bx, float by, float bz,
132 float length, bool collide) {
133 return createJoint(*this, a, b, JointKind3D::Distance, {ax, ay, az}, {bx, by, bz}, JPH::Vec3::sAxisX(),
134 JPH::Vec3::sAxisZ(), length, collide);
135}
137 float z, float ux, float uy, float uz,
138 float sx, float sy, float sz, bool collide) {
139 return createJoint(*this, a, b, JointKind3D::Wheel, {x, y, z}, {x, y, z}, {ux, uy, uz}, {sx, sy, sz}, 0, collide);
140}
142 float z, bool collide) {
143 return createJoint(*this, a, b, JointKind3D::Weld, {x, y, z}, {x, y, z}, JPH::Vec3::sAxisX(), JPH::Vec3::sAxisZ(),
144 0, collide);
145}
147 return createJoint(*this, a, b, JointKind3D::Motor, {a.getX(), a.getY(), a.getZ()}, {b.getX(), b.getY(), b.getZ()},
148 JPH::Vec3::sAxisX(), JPH::Vec3::sAxisZ(), 0, collide);
149}
151 return createJoint(*this, a, b, JointKind3D::Filter, {0, 0, 0}, {0, 0, 0}, JPH::Vec3::sAxisX(), JPH::Vec3::sAxisZ(),
152 0, false);
153}
155 float z, bool collide) {
156 return createJoint(*this, a, b, JointKind3D::Parallel, {a.getX(), a.getY(), a.getZ()},
157 {b.getX(), b.getY(), b.getZ()}, {x, y, z}, JPH::Vec3::sAxisZ(), 0, collide);
158}
160 float z, float ux, float uy, float uz,
161 bool collide) {
162 return createJoint(*this, a, b, JointKind3D::Revolute, {x, y, z}, {x, y, z}, {ux, uy, uz}, JPH::Vec3::sAxisZ(), 0,
163 collide);
164}
166 float z, float ux, float uy, float uz,
167 bool collide) {
168 return createJoint(*this, a, b, JointKind3D::Prismatic, {x, y, z}, {x, y, z}, {ux, uy, uz}, JPH::Vec3::sAxisZ(), 0,
169 collide);
170}
172 float z, float ux, float uy, float uz,
173 bool collide) {
174 return createJoint(*this, a, b, JointKind3D::Spherical, {x, y, z}, {x, y, z}, {ux, uy, uz}, JPH::Vec3::sAxisZ(), 0,
175 collide);
176}
177} // namespace eve::physics::jolt
double z
double y
double x
glm::vec4 p[6]
std::vector< Value > defaults
Definition ECS.cpp:49
ScriptComponentRecord record
Definition ECS.cpp:228
std::array< double, 10 > q
JointPoint2D anchorB
float length
JointPoint2D axis
JointPoint2D anchorA
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanWakeSettings settings
std::string body
RoadLaneDirection direction
std::string kind
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Backend-neutral 3D rigid body in meter-space coordinates (+Y up by convention). Owned by a World3D; c...
Definition Body3D.h:31
eve::Result< std::unique_ptr< IJointBackend3D > > newDistanceJoint(Body3D &bodyA, Body3D &bodyB, float anchorAX, float anchorAY, float anchorAZ, float anchorBX, float anchorBY, float anchorBZ, float length, bool collideConnected) override
Creates an owning Distance constraint; bodies are synchronous non-owning inputs. @cost One native con...
eve::Result< std::unique_ptr< IJointBackend3D > > newWeldJoint(Body3D &bodyA, Body3D &bodyB, float anchorX, float anchorY, float anchorZ, bool collideConnected) override
Creates an owning Weld constraint; bodies are synchronous non-owning inputs. @cost One native constra...
eve::Result< std::unique_ptr< IJointBackend3D > > newSphericalJoint(Body3D &bodyA, Body3D &bodyB, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, bool collideConnected) override
Creates an owning Spherical constraint; bodies are synchronous non-owning inputs. @cost One native co...
eve::Result< std::unique_ptr< IJointBackend3D > > newParallelJoint(Body3D &bodyA, Body3D &bodyB, float axisX, float axisY, float axisZ, bool collideConnected) override
Creates an owning Parallel constraint; bodies are synchronous non-owning inputs. @cost One native con...
eve::Result< std::unique_ptr< IJointBackend3D > > newPrismaticJoint(Body3D &bodyA, Body3D &bodyB, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, bool collideConnected) override
Creates an owning Prismatic constraint; bodies are synchronous non-owning inputs. @cost One native co...
std::shared_ptr< State > state
Definition JoltWorld.h:17
eve::Result< std::unique_ptr< IJointBackend3D > > newFilterJoint(Body3D &bodyA, Body3D &bodyB) override
Creates an owning Filter constraint; bodies are synchronous non-owning inputs. @cost One native const...
eve::Result< std::unique_ptr< IJointBackend3D > > newWheelJoint(Body3D &bodyA, Body3D &bodyB, float anchorX, float anchorY, float anchorZ, float suspensionAxisX, float suspensionAxisY, float suspensionAxisZ, float wheelAxisX, float wheelAxisY, float wheelAxisZ, bool collideConnected) override
Creates an owning Wheel constraint; bodies are synchronous non-owning inputs. @cost One native constr...
eve::Result< std::unique_ptr< IJointBackend3D > > newMotorJoint(Body3D &bodyA, Body3D &bodyB, bool collideConnected) override
Creates an owning Motor constraint; bodies are synchronous non-owning inputs. @cost One native constr...
bool isValid() const noexcept override
Native world liveness.
Definition JoltWorld.cpp:90
eve::Result< std::unique_ptr< IJointBackend3D > > newRevoluteJoint(Body3D &bodyA, Body3D &bodyB, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, bool collideConnected) override
Creates an owning Revolute constraint; bodies are synchronous non-owning inputs. @cost One native con...
eve::Result< T > rejected(const char *operation, eve::DiagnosticCode code=eve::DiagnosticCode::Unsupported)
bool isFinite(float x, float y, float z)
JPH::Ref< JPH::TwoBodyConstraint > createDistanceConstraint(JPH::Body &a, JPH::Body &b, const JPH::DistanceConstraintSettings &settings)
Native Jolt solver composition for independently configurable distance spring, limits and motor....
JPH::SixDOFConstraint::EAxis A
JointKind3D
Stable constraint kinds; numeric values are used by stress events.