载入中...
搜索中...
未找到
World3DJoints.cpp
浏览该文件的文档.
1#include "physics/World3D.h"
2
3#include "physics/Body3D.h"
4#include "physics/Joint3D.h"
6#include "common/Exception.h"
7
9
10#include <cmath>
11
12namespace eve::physics {
13using namespace world3d_detail;
14namespace {
15
16void requireFiniteJointParams(const float *values, int count, const char *operation) {
17 for (int i = 0; i < count; ++i) {
18 if (!std::isfinite(values[i]))
19 throw eve::Exception("%s: parameters must be finite", operation);
20 }
21}
22
23void requireDistinctBodies(World3D *world, Body3D *bodyA, Body3D *bodyB, const char *operation) {
24 if (!world || !world->isValid() || !bodyA || !bodyB || !bodyA->isValid() || !bodyB->isValid() ||
25 bodyA->getWorld() != world || bodyB->getWorld() != world || bodyA == bodyB)
26 throw eve::Exception("%s: bodies must be distinct and belong to this world", operation);
27}
28
29PhysicsVector3D normalizedAxis(float x, float y, float z, const char *operation) {
30 const float length = std::sqrt(x * x + y * y + z * z);
31 if (length <= 1e-8f) throw eve::Exception("%s: axis length must be > 0", operation);
32 return PhysicsVector3D{x / length, y / length, z / length};
33}
34
35} // namespace
36
37Joint3D *World3D::newWeldJoint(Body3D *bodyA, Body3D *bodyB, float anchorX, float anchorY, float anchorZ,
38 bool collideConnected) {
39 requireDistinctBodies(this, bodyA, bodyB, "World3D.newWeldJoint");
40 const auto handle = nextJointRuntimeHandle();
41 auto native = worldValue(rigidBackend_->newWeldJoint(*bodyA, *bodyB, anchorX, anchorY, anchorZ, collideConnected));
42 auto joint =
43 std::make_unique<Joint3D>(this, bodyA, bodyB, std::move(native), handle, Joint3D::Kind::Weld, nextJointId());
44 joints_.insert(joint.get());
45 jointHandles_[handle] = joint.get();
46 return joint.release();
47}
48
50 requireDistinctBodies(this, bodyA, bodyB, "World3D.newMotorJoint");
51 const auto handle = nextJointRuntimeHandle();
52 auto native = worldValue(rigidBackend_->newMotorJoint(*bodyA, *bodyB, collideConnected));
53 auto joint =
54 std::make_unique<Joint3D>(this, bodyA, bodyB, std::move(native), handle, Joint3D::Kind::Motor, nextJointId());
55 joints_.insert(joint.get());
56 jointHandles_[handle] = joint.get();
57 return joint.release();
58}
59
60Joint3D *World3D::newParallelJoint(Body3D *bodyA, Body3D *bodyB, float axisX, float axisY, float axisZ,
61 bool collideConnected) {
62 requireDistinctBodies(this, bodyA, bodyB, "World3D.newParallelJoint");
63 const auto handle = nextJointRuntimeHandle();
64 auto native = worldValue(rigidBackend_->newParallelJoint(*bodyA, *bodyB, axisX, axisY, axisZ, collideConnected));
65 auto joint = std::make_unique<Joint3D>(this, bodyA, bodyB, std::move(native), handle, Joint3D::Kind::Parallel,
66 nextJointId());
67 joints_.insert(joint.get());
68 jointHandles_[handle] = joint.get();
69 return joint.release();
70}
71
73 requireDistinctBodies(this, bodyA, bodyB, "World3D.newFilterJoint");
74 const auto handle = nextJointRuntimeHandle();
75 auto native = worldValue(rigidBackend_->newFilterJoint(*bodyA, *bodyB));
76 auto joint =
77 std::make_unique<Joint3D>(this, bodyA, bodyB, std::move(native), handle, Joint3D::Kind::Filter, nextJointId());
78 joints_.insert(joint.get());
79 jointHandles_[handle] = joint.get();
80 return joint.release();
81}
82
84 if (!mechanism) return;
85 mechanisms_.erase(mechanism);
86}
87
88int World3D::nextMechanismId() { return nextMechanismId_++; }
89
90Mechanism3D *World3D::newShaft(Body3D *support, Body3D *rotor, float anchorX, float anchorY,
91 float anchorZ, float axisX, float axisY, float axisZ,
92 bool collideConnected) {
93 requireDistinctBodies(this, support, rotor, "World3D.newShaft");
94 const PhysicsVector3D axis = normalizedAxis(axisX, axisY, axisZ, "World3D.newShaft");
95 Joint3D *drive =
96 newRevoluteJoint(support, rotor, anchorX, anchorY, anchorZ, axis.x, axis.y, axis.z,
98 auto *mechanism = new Mechanism3D(this, Mechanism3D::Kind::Shaft, nextMechanismId(), drive,
99 nullptr, nullptr, nullptr, axis.x, axis.y, axis.z, 1, 0.f);
100 mechanisms_.insert(mechanism);
101 return mechanism;
102}
103
104Mechanism3D *World3D::newRatchet(Body3D *frame, Body3D *wheel, float anchorX, float anchorY,
105 float anchorZ, float axisX, float axisY, float axisZ,
106 int direction, float engagementTorque, bool collideConnected) {
107 requireDistinctBodies(this, frame, wheel, "World3D.newRatchet");
108 if (direction != 1 && direction != -1)
109 throw eve::Exception("World3D.newRatchet: direction must be +1 or -1");
110 if (!std::isfinite(engagementTorque) || engagementTorque < 0.f)
111 throw eve::Exception("World3D.newRatchet: engagementTorque must be finite and >= 0");
112 const PhysicsVector3D axis = normalizedAxis(axisX, axisY, axisZ, "World3D.newRatchet");
113 Joint3D *drive =
114 newRevoluteJoint(frame, wheel, anchorX, anchorY, anchorZ, axis.x, axis.y, axis.z,
116 auto *mechanism =
117 new Mechanism3D(this, Mechanism3D::Kind::Ratchet, nextMechanismId(), drive, nullptr,
118 nullptr, nullptr, axis.x, axis.y, axis.z, direction, engagementTorque);
119 mechanisms_.insert(mechanism);
120 return mechanism;
121}
122
124 float crankAnchorX, float crankAnchorY, float crankAnchorZ,
125 float axisX, float axisY, float axisZ, float crankPinX,
126 float crankPinY, float crankPinZ, float sliderPinX,
127 float sliderPinY, float sliderPinZ, float slideAxisX,
128 float slideAxisY, float slideAxisZ, bool collideConnected) {
129 requireDistinctBodies(this, frame, crank, "World3D.newCrankSlider");
130 requireDistinctBodies(this, crank, rod, "World3D.newCrankSlider");
131 requireDistinctBodies(this, rod, slider, "World3D.newCrankSlider");
132 requireDistinctBodies(this, frame, slider, "World3D.newCrankSlider");
133 if (frame == rod || crank == slider)
134 throw eve::Exception("World3D.newCrankSlider: bodies must form a four-bar chain");
135 const float values[] = {crankAnchorX, crankAnchorY, crankAnchorZ, axisX, axisY,
136 axisZ, crankPinX, crankPinY, crankPinZ, sliderPinX,
137 sliderPinY, sliderPinZ, slideAxisX, slideAxisY, slideAxisZ};
138 requireFiniteJointParams(values, 15, "World3D.newCrankSlider");
139 const PhysicsVector3D hingeAxis = normalizedAxis(axisX, axisY, axisZ, "World3D.newCrankSlider");
140 const PhysicsVector3D slideAxis = normalizedAxis(slideAxisX, slideAxisY, slideAxisZ, "World3D.newCrankSlider");
141 if (std::fabs((hingeAxis.x * slideAxis.x + hingeAxis.y * slideAxis.y + hingeAxis.z * slideAxis.z)) > 0.999f)
142 throw eve::Exception("World3D.newCrankSlider: hinge and slide axes must not be parallel");
143
144 Joint3D *drive =
145 newRevoluteJoint(frame, crank, crankAnchorX, crankAnchorY, crankAnchorZ, hingeAxis.x,
146 hingeAxis.y, hingeAxis.z, collideConnected);
147 Joint3D *crankPin =
148 newRevoluteJoint(crank, rod, crankPinX, crankPinY, crankPinZ, hingeAxis.x, hingeAxis.y,
149 hingeAxis.z, collideConnected);
150 Joint3D *sliderPin =
151 newRevoluteJoint(rod, slider, sliderPinX, sliderPinY, sliderPinZ, hingeAxis.x, hingeAxis.y,
152 hingeAxis.z, collideConnected);
153 Joint3D *prismatic =
154 newPrismaticJoint(frame, slider, sliderPinX, sliderPinY, sliderPinZ, slideAxis.x,
155 slideAxis.y, slideAxis.z, collideConnected);
156 auto *mechanism =
157 new Mechanism3D(this, Mechanism3D::Kind::CrankSlider, nextMechanismId(), drive, crankPin,
158 sliderPin, prismatic, hingeAxis.x, hingeAxis.y, hingeAxis.z, 1, 0.f);
159 mechanisms_.insert(mechanism);
160 return mechanism;
161}
162
163Joint3D *World3D::newDistanceJoint(Body3D *bodyA, Body3D *bodyB, float anchorAX, float anchorAY, float anchorAZ,
164 float anchorBX, float anchorBY, float anchorBZ, float length,
165 bool collideConnected) {
166 requireDistinctBodies(this, bodyA, bodyB, "World3D.newDistanceJoint");
167 const auto handle = nextJointRuntimeHandle();
168 auto native = worldValue(rigidBackend_->newDistanceJoint(*bodyA, *bodyB, anchorAX, anchorAY, anchorAZ, anchorBX,
169 anchorBY, anchorBZ, length, collideConnected));
170 auto joint = std::make_unique<Joint3D>(this, bodyA, bodyB, std::move(native), handle, Joint3D::Kind::Distance,
171 nextJointId());
172 joints_.insert(joint.get());
173 jointHandles_[handle] = joint.get();
174 return joint.release();
175}
176
177Joint3D *World3D::newRevoluteJoint(Body3D *bodyA, Body3D *bodyB, float anchorX, float anchorY, float anchorZ,
178 float axisX, float axisY, float axisZ, bool collideConnected) {
179 requireDistinctBodies(this, bodyA, bodyB, "World3D.newRevoluteJoint");
180 const auto handle = nextJointRuntimeHandle();
181 auto native = worldValue(rigidBackend_->newRevoluteJoint(*bodyA, *bodyB, anchorX, anchorY, anchorZ, axisX, axisY,
183 auto joint = std::make_unique<Joint3D>(this, bodyA, bodyB, std::move(native), handle, Joint3D::Kind::Revolute,
184 nextJointId());
185 joints_.insert(joint.get());
186 jointHandles_[handle] = joint.get();
187 return joint.release();
188}
189
190Joint3D *World3D::newPrismaticJoint(Body3D *bodyA, Body3D *bodyB, float anchorX, float anchorY, float anchorZ,
191 float axisX, float axisY, float axisZ, bool collideConnected) {
192 requireDistinctBodies(this, bodyA, bodyB, "World3D.newPrismaticJoint");
193 const auto handle = nextJointRuntimeHandle();
194 auto native = worldValue(rigidBackend_->newPrismaticJoint(*bodyA, *bodyB, anchorX, anchorY, anchorZ, axisX, axisY,
196 auto joint = std::make_unique<Joint3D>(this, bodyA, bodyB, std::move(native), handle, Joint3D::Kind::Prismatic,
197 nextJointId());
198 joints_.insert(joint.get());
199 jointHandles_[handle] = joint.get();
200 return joint.release();
201}
202
203Joint3D *World3D::newSphericalJoint(Body3D *bodyA, Body3D *bodyB, float anchorX, float anchorY, float anchorZ,
204 float axisX, float axisY, float axisZ, bool collideConnected) {
205 requireDistinctBodies(this, bodyA, bodyB, "World3D.newSphericalJoint");
206 const auto handle = nextJointRuntimeHandle();
207 auto native = worldValue(rigidBackend_->newSphericalJoint(*bodyA, *bodyB, anchorX, anchorY, anchorZ, axisX, axisY,
209 auto joint = std::make_unique<Joint3D>(this, bodyA, bodyB, std::move(native), handle, Joint3D::Kind::Spherical,
210 nextJointId());
211 joints_.insert(joint.get());
212 jointHandles_[handle] = joint.get();
213 return joint.release();
214}
215
216Joint3D *World3D::newWheelJoint(Body3D *bodyA, Body3D *bodyB, float anchorX, float anchorY, float anchorZ,
217 float suspensionAxisX, float suspensionAxisY, float suspensionAxisZ, float wheelAxisX,
218 float wheelAxisY, float wheelAxisZ, bool collideConnected) {
219 requireDistinctBodies(this, bodyA, bodyB, "World3D.newWheelJoint");
220 const auto handle = nextJointRuntimeHandle();
221 auto native = worldValue(rigidBackend_->newWheelJoint(*bodyA, *bodyB, anchorX, anchorY, anchorZ, suspensionAxisX,
222 suspensionAxisY, suspensionAxisZ, wheelAxisX, wheelAxisY,
223 wheelAxisZ, collideConnected));
224 auto joint =
225 std::make_unique<Joint3D>(this, bodyA, bodyB, std::move(native), handle, Joint3D::Kind::Wheel, nextJointId());
226 joints_.insert(joint.get());
227 jointHandles_[handle] = joint.get();
228 return joint.release();
229}
230
231} // namespace eve::physics
double z
double y
double x
ActionParameterOperation operation
bool collideConnected
float length
JointPoint2D axis
ssq::Object frame
PrimitiveHandle handle
std::set< std::string > values
RoadLaneDirection direction
float axisZ
Definition RockMesh.cpp:22
float axisY
Definition RockMesh.cpp:22
float axisX
Definition RockMesh.cpp:22
int count
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Backend-neutral 3D rigid body in meter-space coordinates (+Y up by convention). Owned by a World3D; c...
Definition Body3D.h:31
Script-facing joint owned by a World3D. Legacy scalar/void methods adapt the checked backend contract...
Definition Joint3D.h:27
Composed mechanical assembly owned by a World3D.
Definition Mechanism3D.h:25
PhysicsJointHandle nextJointRuntimeHandle()
Internal: next generation-qualified joint handle.
Definition World3D.cpp:416
Joint3D * newMotorJoint(Body3D *bodyA, Body3D *bodyB, bool collideConnected=false)
Creates a motor joint that drives relative linear/angular velocity between bodies.
Joint3D * newWeldJoint(Body3D *bodyA, Body3D *bodyB, float anchorX, float anchorY, float anchorZ, bool collideConnected=false)
Welds two bodies at a shared world-space anchor, preserving current relative pose.
Joint3D * newParallelJoint(Body3D *bodyA, Body3D *bodyB, float axisX, float axisY, float axisZ, bool collideConnected=false)
Creates a parallel joint that spring-aligns body local Z axes to a world axis.
Joint3D * newSphericalJoint(Body3D *bodyA, Body3D *bodyB, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, bool collideConnected=false)
Creates a ball-and-socket joint with a world-space twist/cone axis.
Joint3D * newRevoluteJoint(Body3D *bodyA, Body3D *bodyB, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, bool collideConnected=false)
Creates a hinge around a normalized world-space axis at a shared anchor.
int nextJointId()
Internal: next stable joint id.
Definition World3D.h:1038
Mechanism3D * newCrankSlider(Body3D *frame, Body3D *crank, Body3D *rod, Body3D *slider, float crankAnchorX, float crankAnchorY, float crankAnchorZ, float axisX, float axisY, float axisZ, float crankPinX, float crankPinY, float crankPinZ, float sliderPinX, float sliderPinY, float sliderPinZ, float slideAxisX, float slideAxisY, float slideAxisZ, bool collideConnected=false)
Builds a planar crank-slider from frame, crank, connecting rod and slider bodies.
Mechanism3D * newRatchet(Body3D *frame, Body3D *wheel, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, int direction, float engagementTorque, bool collideConnected=false)
Builds a one-way ratchet around a revolute axis.
Joint3D * newFilterJoint(Body3D *bodyA, Body3D *bodyB)
Disables collision between two specific bodies (filter joint).
Joint3D * newPrismaticJoint(Body3D *bodyA, Body3D *bodyB, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, bool collideConnected=false)
Creates a slider whose permitted world-space translation follows axis.
Mechanism3D * newShaft(Body3D *support, Body3D *rotor, float anchorX, float anchorY, float anchorZ, float axisX, float axisY, float axisZ, bool collideConnected=false)
Builds a shaft (support↔rotor revolute) with optional drive motor helpers.
void forgetMechanism(Mechanism3D *mechanism)
Internal: removes a mechanism wrapper from ownership bookkeeping.
Joint3D * 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=false)
Creates a vehicle wheel joint with independent world suspension and spin axes.
Joint3D * newDistanceJoint(Body3D *bodyA, Body3D *bodyB, float anchorAX, float anchorAY, float anchorAZ, float anchorBX, float anchorBY, float anchorBZ, float length, bool collideConnected=false)
Connects two world-space anchor points with a rigid distance constraint.
friend class Mechanism3D
Definition World3D.h:1060
int nextMechanismId()
Internal: next stable mechanism id.
T worldValue(eve::Result< T > result)
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
Allocation-free owning three-dimensional physics value.
Definition RigidTypes3D.h:4