载入中...
搜索中...
未找到
Joint3D.cpp
浏览该文件的文档.
1#include "physics/Joint3D.h"
2#include "common/Assert.h"
3#include "common/Exception.h"
4#include "physics/Body3D.h"
5#include "physics/World3D.h"
6namespace eve::physics {
7namespace {
8template <class T>
9T legacyValue(eve::Result<T> result) {
10 if (!result.ok()) throw eve::Exception("%s", result.error()->message().c_str());
11 return std::move(result).value();
12}
13void legacyValue(eve::Result<void> result) {
14 if (!result.ok()) throw eve::Exception("%s", result.error()->message().c_str());
15}
16} // namespace
17Joint3D::Joint3D(World3D* world, Body3D* bodyA, Body3D* bodyB, std::unique_ptr<IJointBackend3D> backend,
18 PhysicsJointHandle runtimeHandle, Kind kind, int id)
19 : world_(world),
20 bodyA_(bodyA),
21 bodyB_(bodyB),
22 backend_(std::move(backend)),
23 runtimeHandle_(runtimeHandle),
24 kind_(kind),
25 id_(id) {
26 EV_PARAM_CHECK(backend_ != nullptr, "Joint3D requires a backend");
27 backend_->bindOwner(*this);
28}
30bool Joint3D::isValid() const { return backend_ && backend_->isValid(); }
32 if (world_) world_->forgetJoint(this);
33 if (backend_) backend_->invalidate();
34 world_ = nullptr;
35 bodyA_ = nullptr;
36 bodyB_ = nullptr;
37 runtimeHandle_ = PhysicsJointHandle::invalid();
38}
40 if (backend_) backend_->destroy();
41 invalidate();
42}
43std::string Joint3D::getKind() const {
44 switch (kind_) {
45 case Kind::Distance: return "distance";
46 case Kind::Revolute: return "revolute";
47 case Kind::Prismatic: return "prismatic";
48 case Kind::Spherical: return "spherical";
49 case Kind::Wheel: return "wheel";
50 case Kind::Weld: return "weld";
51 case Kind::Motor: return "motor";
52 case Kind::Parallel: return "parallel";
53 case Kind::Filter: return "filter";
54 }
55 return "distance";
56}
57
58int Joint3D::getBodyAId() const { return bodyA_ ? bodyA_->getId() : -1; }
59int Joint3D::getBodyBId() const { return bodyB_ ? bodyB_->getId() : -1; }
60
62 if (!backend_)
64 eve::DiagnosticCode::StaleHandle, "Joint has no backend", "physics.joint3d.localFrames"));
65 return backend_->localFrames();
66}
67void Joint3D::setCollideConnected(bool collide) {
68 if (!isValid()) return;
69 legacyValue(backend_->setCollideConnected(collide));
70}
72 if (!isValid()) return false;
73 return legacyValue(backend_->getCollideConnected());
74}
75void Joint3D::setForceThreshold(float threshold) {
76 if (!isValid()) return;
77 legacyValue(backend_->setForceThreshold(threshold));
78}
80 if (!isValid()) return 0.f;
81 return legacyValue(backend_->getForceThreshold());
82}
83void Joint3D::setTorqueThreshold(float threshold) {
84 if (!isValid()) return;
85 legacyValue(backend_->setTorqueThreshold(threshold));
86}
88 if (!isValid()) return 0.f;
89 return legacyValue(backend_->getTorqueThreshold());
90}
91
93 if (!isValid()) return 0.f;
94 return legacyValue(backend_->getConstraintForceX());
95}
97 if (!isValid()) return 0.f;
98 return legacyValue(backend_->getConstraintForceY());
99}
101 if (!isValid()) return 0.f;
102 return legacyValue(backend_->getConstraintForceZ());
103}
105 if (!isValid()) return 0.f;
106 return legacyValue(backend_->getConstraintTorqueX());
107}
109 if (!isValid()) return 0.f;
110 return legacyValue(backend_->getConstraintTorqueY());
111}
113 if (!isValid()) return 0.f;
114 return legacyValue(backend_->getConstraintTorqueZ());
115}
117 if (!isValid()) return 0.f;
118 return legacyValue(backend_->getLinearSeparation());
119}
121 if (!isValid()) return 0.f;
122 return legacyValue(backend_->getAngularSeparation());
123}
124void Joint3D::setDistanceLength(float length) { legacyValue(backend_->setDistanceLength(length)); }
125float Joint3D::getDistanceLength() const { return legacyValue(backend_->getDistanceLength()); }
126float Joint3D::getDistanceCurrentLength() const { return legacyValue(backend_->getDistanceCurrentLength()); }
127void Joint3D::setDistanceSpring(bool enabled, float hertz, float dampingRatio) {
128 legacyValue(backend_->setDistanceSpring(enabled, hertz, dampingRatio));
129}
131 legacyValue(backend_->setDistanceLimits(enabled, minimum, maximum));
132}
133void Joint3D::setDistanceMotor(bool enabled, float speed, float maxForce) {
134 legacyValue(backend_->setDistanceMotor(enabled, speed, maxForce));
135}
136void Joint3D::setRevoluteSpring(bool enabled, float targetAngle, float hertz,
137 float dampingRatio) {
138 legacyValue(backend_->setRevoluteSpring(enabled, targetAngle, hertz, dampingRatio));
139}
140void Joint3D::setRevoluteLimits(bool enabled, float lower, float upper) {
141 legacyValue(backend_->setRevoluteLimits(enabled, lower, upper));
142}
143void Joint3D::setRevoluteMotor(bool enabled, float speed, float maxTorque) {
144 legacyValue(backend_->setRevoluteMotor(enabled, speed, maxTorque));
145}
146float Joint3D::getRevoluteAngle() const { return legacyValue(backend_->getRevoluteAngle()); }
147float Joint3D::getRevoluteMotorTorque() const { return legacyValue(backend_->getRevoluteMotorTorque()); }
148void Joint3D::setPrismaticSpring(bool enabled, float targetTranslation, float hertz,
149 float dampingRatio) {
150 legacyValue(backend_->setPrismaticSpring(enabled, targetTranslation, hertz, dampingRatio));
151}
152void Joint3D::setPrismaticLimits(bool enabled, float lower, float upper) {
153 legacyValue(backend_->setPrismaticLimits(enabled, lower, upper));
154}
155void Joint3D::setPrismaticMotor(bool enabled, float speed, float maxForce) {
156 legacyValue(backend_->setPrismaticMotor(enabled, speed, maxForce));
157}
158float Joint3D::getPrismaticTranslation() const { return legacyValue(backend_->getPrismaticTranslation()); }
159float Joint3D::getPrismaticSpeed() const { return legacyValue(backend_->getPrismaticSpeed()); }
160float Joint3D::getPrismaticMotorForce() const { return legacyValue(backend_->getPrismaticMotorForce()); }
162 legacyValue(backend_->setSphericalConeLimit(enabled, angle));
163}
164void Joint3D::setSphericalTwistLimits(bool enabled, float lower, float upper) {
165 legacyValue(backend_->setSphericalTwistLimits(enabled, lower, upper));
166}
167void Joint3D::setSphericalMotor(bool enabled, float velocityX, float velocityY,
168 float velocityZ, float maxTorque) {
169 legacyValue(backend_->setSphericalMotor(enabled, velocityX, velocityY, velocityZ, maxTorque));
170}
171float Joint3D::getSphericalConeAngle() const { return legacyValue(backend_->getSphericalConeAngle()); }
172float Joint3D::getSphericalTwistAngle() const { return legacyValue(backend_->getSphericalTwistAngle()); }
173void Joint3D::setWheelSuspension(bool enabled, float hertz, float dampingRatio) {
174 legacyValue(backend_->setWheelSuspension(enabled, hertz, dampingRatio));
175}
176void Joint3D::setWheelSuspensionLimits(bool enabled, float lower, float upper) {
177 legacyValue(backend_->setWheelSuspensionLimits(enabled, lower, upper));
178}
179void Joint3D::setWheelSpinMotor(bool enabled, float speed, float maxTorque) {
180 legacyValue(backend_->setWheelSpinMotor(enabled, speed, maxTorque));
181}
182void Joint3D::setWheelSteering(bool enabled, float targetAngle, float hertz,
183 float dampingRatio, float maxTorque) {
184 legacyValue(backend_->setWheelSteering(enabled, targetAngle, hertz, dampingRatio, maxTorque));
185}
186void Joint3D::setWheelSteeringLimits(bool enabled, float lower, float upper) {
187 legacyValue(backend_->setWheelSteeringLimits(enabled, lower, upper));
188}
189float Joint3D::getWheelSpinSpeed() const { return legacyValue(backend_->getWheelSpinSpeed()); }
190float Joint3D::getWheelSpinTorque() const { return legacyValue(backend_->getWheelSpinTorque()); }
191float Joint3D::getWheelSteeringAngle() const { return legacyValue(backend_->getWheelSteeringAngle()); }
192float Joint3D::getWheelSteeringTorque() const { return legacyValue(backend_->getWheelSteeringTorque()); }
194 legacyValue(backend_->setWeldLinearSpring(hertz, dampingRatio));
195}
197 legacyValue(backend_->setWeldAngularSpring(hertz, dampingRatio));
198}
199float Joint3D::getWeldLinearHertz() const { return legacyValue(backend_->getWeldLinearHertz()); }
200float Joint3D::getWeldLinearDampingRatio() const { return legacyValue(backend_->getWeldLinearDampingRatio()); }
201float Joint3D::getWeldAngularHertz() const { return legacyValue(backend_->getWeldAngularHertz()); }
202float Joint3D::getWeldAngularDampingRatio() const { return legacyValue(backend_->getWeldAngularDampingRatio()); }
203void Joint3D::setMotorLinearVelocity(float vx, float vy, float vz) {
204 legacyValue(backend_->setMotorLinearVelocity(vx, vy, vz));
205}
206void Joint3D::setMotorAngularVelocity(float wx, float wy, float wz) {
207 legacyValue(backend_->setMotorAngularVelocity(wx, wy, wz));
208}
209void Joint3D::setMotorVelocityLimits(float maxForce, float maxTorque) {
210 legacyValue(backend_->setMotorVelocityLimits(maxForce, maxTorque));
211}
212void Joint3D::setMotorLinearSpring(float hertz, float dampingRatio, float maxForce) {
213 legacyValue(backend_->setMotorLinearSpring(hertz, dampingRatio, maxForce));
214}
215void Joint3D::setMotorAngularSpring(float hertz, float dampingRatio, float maxTorque) {
216 legacyValue(backend_->setMotorAngularSpring(hertz, dampingRatio, maxTorque));
217}
218float Joint3D::getMotorLinearVelocityX() const { return legacyValue(backend_->getMotorLinearVelocityX()); }
219float Joint3D::getMotorLinearVelocityY() const { return legacyValue(backend_->getMotorLinearVelocityY()); }
220float Joint3D::getMotorLinearVelocityZ() const { return legacyValue(backend_->getMotorLinearVelocityZ()); }
221float Joint3D::getMotorAngularVelocityX() const { return legacyValue(backend_->getMotorAngularVelocityX()); }
222float Joint3D::getMotorAngularVelocityY() const { return legacyValue(backend_->getMotorAngularVelocityY()); }
223float Joint3D::getMotorAngularVelocityZ() const { return legacyValue(backend_->getMotorAngularVelocityZ()); }
224void Joint3D::setParallelSpring(float hertz, float dampingRatio, float maxTorque) {
225 legacyValue(backend_->setParallelSpring(hertz, dampingRatio, maxTorque));
226}
227float Joint3D::getParallelHertz() const { return legacyValue(backend_->getParallelHertz()); }
228float Joint3D::getParallelDampingRatio() const { return legacyValue(backend_->getParallelDampingRatio()); }
229float Joint3D::getParallelMaxTorque() const { return legacyValue(backend_->getParallelMaxTorque()); }
230} // namespace eve::physics
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
float maxForce
float maximum[3]
float minimum[3]
float dampingRatio
float length
std::string id
Poco::JSON::Object::Ptr result
OceanRenderBackend backend
std::string kind
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static constexpr RuntimeHandle invalid() noexcept
Returns the canonical invalid handle.
Backend-neutral 3D rigid body in meter-space coordinates (+Y up by convention). Owned by a World3D; c...
Definition Body3D.h:31
int getId() const
Stable id used by collision events.
Definition Body3D.h:42
float getConstraintForceX() const
Current world-space constraint force X in newtons.
Definition Joint3D.cpp:92
void setDistanceLength(float length)
Changes distance-joint rest length in metres.
Definition Joint3D.cpp:124
float getConstraintForceY() const
Current world-space constraint force Y in newtons.
Definition Joint3D.cpp:96
float getParallelHertz() const
Parallel spring stiffness in Hertz.
Definition Joint3D.cpp:227
void destroy()
Destroys the backend joint and invalidates this wrapper.
Definition Joint3D.cpp:39
void setForceThreshold(float threshold)
Sets the force threshold that emits a joint-stress event, in newtons.
Definition Joint3D.cpp:75
float getDistanceLength() const
Distance-joint rest length in metres.
Definition Joint3D.cpp:125
void setWheelSteering(bool enabled, float targetAngle, float hertz, float dampingRatio, float maxTorque)
Configures steering target, stiffness, damping and maximum torque.
Definition Joint3D.cpp:182
void setMotorVelocityLimits(float maxForce, float maxTorque)
Sets motor velocity force/torque limits.
Definition Joint3D.cpp:209
void setWheelSpinMotor(bool enabled, float speed, float maxTorque)
Configures wheel spin motor speed and maximum torque.
Definition Joint3D.cpp:179
void invalidate()
Internal invalidation used by body/world destruction.
Definition Joint3D.cpp:31
void setSphericalTwistLimits(bool enabled, float lower, float upper)
Enables spherical twist limits in radians.
Definition Joint3D.cpp:164
float getTorqueThreshold() const
Current torque event threshold in newton-metres.
Definition Joint3D.cpp:87
float getParallelMaxTorque() const
Parallel maximum spring torque in newton-metres.
Definition Joint3D.cpp:229
float getWheelSpinTorque() const
Current wheel spin motor torque in newton-metres.
Definition Joint3D.cpp:190
void setRevoluteLimits(bool enabled, float lower, float upper)
Configures revolute angular limits in radians.
Definition Joint3D.cpp:140
void setDistanceMotor(bool enabled, float speed, float maxForce)
Configures the distance motor in metres/second and newtons.
Definition Joint3D.cpp:133
float getRevoluteMotorTorque() const
Current revolute motor torque in newton-metres.
Definition Joint3D.cpp:147
void setMotorAngularVelocity(float wx, float wy, float wz)
Sets motor-joint relative angular velocity in radians/second.
Definition Joint3D.cpp:206
float getForceThreshold() const
Current force event threshold in newtons.
Definition Joint3D.cpp:79
float getParallelDampingRatio() const
Parallel spring damping ratio.
Definition Joint3D.cpp:228
void setSphericalMotor(bool enabled, float velocityX, float velocityY, float velocityZ, float maxTorque)
Configures spherical motor velocity and maximum torque.
Definition Joint3D.cpp:167
float getWeldAngularHertz() const
Weld angular stiffness in Hertz.
Definition Joint3D.cpp:201
float getWeldLinearDampingRatio() const
Weld linear damping ratio.
Definition Joint3D.cpp:200
void setTorqueThreshold(float threshold)
Sets the torque threshold that emits a joint-stress event, in newton-metres.
Definition Joint3D.cpp:83
void setWeldLinearSpring(float hertz, float dampingRatio)
Configures weld linear spring stiffness and damping; 0 hertz is rigid.
Definition Joint3D.cpp:193
float getDistanceCurrentLength() const
Current anchor-to-anchor distance in metres.
Definition Joint3D.cpp:126
float getPrismaticTranslation() const
Current prismatic translation in metres.
Definition Joint3D.cpp:158
float getRevoluteAngle() const
Current revolute angle in radians.
Definition Joint3D.cpp:146
void setDistanceLimits(bool enabled, float minimum, float maximum)
Configures optional distance limits in metres.
Definition Joint3D.cpp:130
float getConstraintTorqueZ() const
Current world-space constraint torque Z in newton-metres.
Definition Joint3D.cpp:112
float getWheelSpinSpeed() const
Current wheel spin speed in radians/second.
Definition Joint3D.cpp:189
eve::Result< JointFrames3D > localFrames() const
Reads both body-local constraint frames without exposing native IDs.
Definition Joint3D.cpp:61
void setWheelSteeringLimits(bool enabled, float lower, float upper)
Configures steering angular limits in radians.
Definition Joint3D.cpp:186
float getPrismaticMotorForce() const
Current prismatic motor force in newtons.
Definition Joint3D.cpp:160
void setRevoluteSpring(bool enabled, float targetAngle, float hertz, float dampingRatio)
Configures a revolute angular spring in radians.
Definition Joint3D.cpp:136
void setPrismaticLimits(bool enabled, float lower, float upper)
Configures prismatic translation limits in metres.
Definition Joint3D.cpp:152
std::string getKind() const
Joint kind string for script/debug ("distance", "revolute", …, "filter").
Definition Joint3D.cpp:43
float getMotorLinearVelocityX() const
Desired motor linear velocity X in metres/second.
Definition Joint3D.cpp:218
void setParallelSpring(float hertz, float dampingRatio, float maxTorque)
Configures parallel-joint spring stiffness, damping and torque cap.
Definition Joint3D.cpp:224
void setMotorLinearVelocity(float vx, float vy, float vz)
Sets motor-joint relative linear velocity in metres/second.
Definition Joint3D.cpp:203
float getMotorAngularVelocityY() const
Desired motor angular velocity Y in radians/second.
Definition Joint3D.cpp:222
float getLinearSeparation() const
Current inadmissible linear separation error in metres.
Definition Joint3D.cpp:116
bool isValid() const
Whether the backend joint still exists.
Definition Joint3D.cpp:30
float getMotorAngularVelocityX() const
Desired motor angular velocity X in radians/second.
Definition Joint3D.cpp:221
float getWheelSteeringTorque() const
Current steering torque in newton-metres.
Definition Joint3D.cpp:192
float getMotorAngularVelocityZ() const
Desired motor angular velocity Z in radians/second.
Definition Joint3D.cpp:223
float getWeldLinearHertz() const
Weld linear stiffness in Hertz.
Definition Joint3D.cpp:199
float getWeldAngularDampingRatio() const
Weld angular damping ratio.
Definition Joint3D.cpp:202
float getConstraintTorqueX() const
Current world-space constraint torque X in newton-metres.
Definition Joint3D.cpp:104
float getConstraintForceZ() const
Current world-space constraint force Z in newtons.
Definition Joint3D.cpp:100
float getMotorLinearVelocityY() const
Desired motor linear velocity Y in metres/second.
Definition Joint3D.cpp:219
float getAngularSeparation() const
Current inadmissible angular separation error in radians.
Definition Joint3D.cpp:120
float getMotorLinearVelocityZ() const
Desired motor linear velocity Z in metres/second.
Definition Joint3D.cpp:220
float getWheelSteeringAngle() const
Current wheel steering angle in radians.
Definition Joint3D.cpp:191
void setDistanceSpring(bool enabled, float hertz, float dampingRatio)
Configures a distance spring; hertz and damping must be non-negative.
Definition Joint3D.cpp:127
void setRevoluteMotor(bool enabled, float speed, float maxTorque)
Configures the revolute motor in radians/second and newton-metres.
Definition Joint3D.cpp:143
int getBodyAId() const
Stable ID of attached body A, or -1 after invalidation.
Definition Joint3D.cpp:58
void setWheelSuspensionLimits(bool enabled, float lower, float upper)
Configures wheel suspension travel limits in metres.
Definition Joint3D.cpp:176
float getPrismaticSpeed() const
Current prismatic translation speed in metres/second.
Definition Joint3D.cpp:159
bool getCollideConnected() const
Whether attached bodies may collide.
Definition Joint3D.cpp:71
void setMotorAngularSpring(float hertz, float dampingRatio, float maxTorque)
Configures motor angular position spring.
Definition Joint3D.cpp:215
float getSphericalConeAngle() const
Current spherical cone angle in radians.
Definition Joint3D.cpp:171
void setPrismaticMotor(bool enabled, float speed, float maxForce)
Configures a prismatic motor in metres/second and newtons.
Definition Joint3D.cpp:155
void setSphericalConeLimit(bool enabled, float angle)
Enables a spherical cone limit in radians, within [0, pi].
Definition Joint3D.cpp:161
void setWeldAngularSpring(float hertz, float dampingRatio)
Configures weld angular spring stiffness and damping; 0 hertz is rigid.
Definition Joint3D.cpp:196
int getBodyBId() const
Stable ID of attached body B, or -1 after invalidation.
Definition Joint3D.cpp:59
void setWheelSuspension(bool enabled, float hertz, float dampingRatio)
Configures wheel suspension spring stiffness and damping.
Definition Joint3D.cpp:173
void setPrismaticSpring(bool enabled, float targetTranslation, float hertz, float dampingRatio)
Configures a prismatic linear spring and target translation.
Definition Joint3D.cpp:148
float getConstraintTorqueY() const
Current world-space constraint torque Y in newton-metres.
Definition Joint3D.cpp:108
float getSphericalTwistAngle() const
Current spherical twist angle in radians.
Definition Joint3D.cpp:172
void setCollideConnected(bool collide)
Enables or disables collision between the attached bodies.
Definition Joint3D.cpp:67
Joint3D(World3D *world, Body3D *bodyA, Body3D *bodyB, std::unique_ptr< IJointBackend3D > backend, PhysicsJointHandle runtimeHandle, Kind kind, int id)
Internal wrapper constructor; use World3D::new*Joint.
Definition Joint3D.cpp:17
void setMotorLinearSpring(float hertz, float dampingRatio, float maxForce)
Configures motor linear position spring.
Definition Joint3D.cpp:212
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
Definition World3D.h:47
void forgetJoint(Joint3D *joint)
Internal: removes a joint wrapper from ownership bookkeeping.
Definition World3D.cpp:305
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
JointKind3D
Stable constraint kinds; numeric values are used by stress events.
bool enabled