载入中...
搜索中...
未找到
Mechanism2D.cpp
浏览该文件的文档.
2#include <cmath>
3#include "common/Exception.h"
4#include "physics/Body.h"
5#include "physics/Body3D.h"
6#include "physics/Joint2D.h"
7#include "physics/Joint3D.h"
9#include "physics/World.h"
10#include "physics/World3D.h"
11
12// Mechanism2D.cpp
13namespace eve::physics {
14
15Mechanism2D::Mechanism2D(World *world, Kind kind, int id, Joint2D *drive, Joint2D *crankPin, Joint2D *sliderPin,
16 Joint2D *slider, int ratchetDirection, float engagementTorque)
17 : world_(world),
18 kind_(kind),
19 id_(id),
20 drive_(drive),
21 crankPin_(crankPin),
22 sliderPin_(sliderPin),
23 slider_(slider),
24 ratchetDirection_(ratchetDirection),
25 engagementTorque_(engagementTorque) {}
26
27Mechanism2D::~Mechanism2D() { destroy(); }
28
29std::string Mechanism2D::getKind() const {
30 switch (kind_) {
31 case Kind::CrankSlider: return "crankSlider";
32 case Kind::Shaft: return "shaft";
33 case Kind::Ratchet: return "ratchet";
34 }
35 return "shaft";
36}
37
38bool Mechanism2D::isValid() const {
39 if (!world_ || !drive_ || !drive_->isValid()) return false;
40 if (kind_ == Kind::CrankSlider) {
41 return crankPin_ && crankPin_->isValid() && sliderPin_ && sliderPin_->isValid() && slider_ &&
42 slider_->isValid();
43 }
44 return true;
45}
46
47void Mechanism2D::requireKind(Kind expected, const char *operation) const {
48 if (!isValid()) throw eve::Exception("%s: mechanism destroyed", operation);
49 if (kind_ != expected) throw eve::Exception("%s: incompatible mechanism kind", operation);
50}
51
52float Mechanism2D::getDriveAngle() const {
53 if (!isValid()) throw eve::Exception("Mechanism2D.getDriveAngle: mechanism destroyed");
54 return drive_->getRevoluteAngle();
55}
56
57float Mechanism2D::getSliderTranslation() const {
58 requireKind(Kind::CrankSlider, "Mechanism2D.getSliderTranslation");
59 return slider_->getPrismaticTranslation();
60}
61
62float Mechanism2D::axisSpinSpeed() const {
63 if (!isValid() || !drive_) return 0.f;
64 return drive_->getRevoluteSpeed();
65}
66
67float Mechanism2D::getSpinSpeed() const {
68 if (!isValid()) throw eve::Exception("Mechanism2D.getSpinSpeed: mechanism destroyed");
69 return axisSpinSpeed();
70}
71
72void Mechanism2D::setDrive(float speed, float maxTorque) {
73 if (!isValid()) throw eve::Exception("Mechanism2D.setDrive: mechanism destroyed");
74 if (!std::isfinite(speed)) throw eve::Exception("Mechanism2D.setDrive: speed must be finite");
75 if (!std::isfinite(maxTorque) || maxTorque < 0.f)
76 throw eve::Exception("Mechanism2D.setDrive: maxTorque must be finite and >= 0");
77 driveEnabled_ = true;
78 driveSpeed_ = speed;
79 driveMaxTorque_ = maxTorque;
80 if (kind_ != Kind::Ratchet) drive_->setRevoluteMotor(true, speed, maxTorque);
81}
82
83void Mechanism2D::clearDrive() {
84 if (!isValid()) throw eve::Exception("Mechanism2D.clearDrive: mechanism destroyed");
85 driveEnabled_ = false;
86 driveSpeed_ = 0.f;
87 driveMaxTorque_ = 0.f;
88 if (kind_ != Kind::Ratchet) drive_->setRevoluteMotor(false, 0.f, 0.f);
89}
90
91void Mechanism2D::setRatchetDirection(int direction) {
92 requireKind(Kind::Ratchet, "Mechanism2D.setRatchetDirection");
93 if (direction != 1 && direction != -1)
94 throw eve::Exception("Mechanism2D.setRatchetDirection: direction must be +1 or -1");
95 ratchetDirection_ = direction;
96}
97
98void Mechanism2D::setRatchetEngagementTorque(float torque) {
99 requireKind(Kind::Ratchet, "Mechanism2D.setRatchetEngagementTorque");
100 if (!std::isfinite(torque) || torque < 0.f)
101 throw eve::Exception("Mechanism2D.setRatchetEngagementTorque: torque must be finite and >= 0");
102 engagementTorque_ = torque;
103}
104
105void Mechanism2D::syncBeforeStep() {
106 if (kind_ != Kind::Ratchet || !isValid()) return;
107 const float signedSpeed = axisSpinSpeed() * static_cast<float>(ratchetDirection_);
108 if (signedSpeed < -1e-3f) {
109 drive_->setRevoluteMotor(true, 0.f, engagementTorque_);
110 return;
111 }
112 if (driveEnabled_) {
113 const float driveSpeed = std::fabs(driveSpeed_) * static_cast<float>(ratchetDirection_);
114 drive_->setRevoluteMotor(true, driveSpeed, driveMaxTorque_);
115 } else {
116 drive_->setRevoluteMotor(false, 0.f, 0.f);
117 }
118}
119
120void Mechanism2D::invalidate() {
121 if (world_) world_->forgetMechanism(this);
122 world_ = nullptr;
123 drive_ = nullptr;
124 crankPin_ = nullptr;
125 sliderPin_ = nullptr;
126 slider_ = nullptr;
127}
128
129void Mechanism2D::destroy() {
130 if (drive_ && drive_->isValid()) drive_->destroy();
131 if (crankPin_ && crankPin_->isValid()) crankPin_->destroy();
132 if (sliderPin_ && sliderPin_->isValid()) sliderPin_->destroy();
133 if (slider_ && slider_->isValid()) slider_->destroy();
134 if (world_) world_->forgetMechanism(this);
135 invalidate();
136}
137
138} // namespace eve::physics
139
140// Mechanism3D.cpp
141namespace eve::physics {
142
143Mechanism3D::Mechanism3D(World3D *world, Kind kind, int id, Joint3D *drive, Joint3D *crankPin, Joint3D *sliderPin,
144 Joint3D *slider, float axisX, float axisY, float axisZ, int ratchetDirection,
145 float engagementTorque)
146 : world_(world),
147 kind_(kind),
148 id_(id),
149 drive_(drive),
150 crankPin_(crankPin),
151 sliderPin_(sliderPin),
152 slider_(slider),
153 axisX_(axisX),
154 axisY_(axisY),
155 axisZ_(axisZ),
156 ratchetDirection_(ratchetDirection),
157 engagementTorque_(engagementTorque) {}
158
160
161std::string Mechanism3D::getKind() const {
162 switch (kind_) {
163 case Kind::CrankSlider: return "crankSlider";
164 case Kind::Shaft: return "shaft";
165 case Kind::Ratchet: return "ratchet";
166 }
167 return "shaft";
168}
169
171 if (!world_ || !drive_ || !drive_->isValid()) return false;
172 if (kind_ == Kind::CrankSlider) {
173 return crankPin_ && crankPin_->isValid() && sliderPin_ && sliderPin_->isValid() && slider_ &&
174 slider_->isValid();
175 }
176 return true;
177}
178
179void Mechanism3D::requireKind(Kind expected, const char *operation) const {
180 if (!isValid()) throw eve::Exception("%s: mechanism destroyed", operation);
181 if (kind_ != expected) throw eve::Exception("%s: incompatible mechanism kind", operation);
182}
183
185 if (!isValid()) throw eve::Exception("Mechanism3D.getDriveAngle: mechanism destroyed");
186 return drive_->getRevoluteAngle();
187}
188
190 requireKind(Kind::CrankSlider, "Mechanism3D.getSliderTranslation");
191 return slider_->getPrismaticTranslation();
192}
193
194float Mechanism3D::axisSpinSpeed() const {
195 if (!isValid() || !drive_) return 0.f;
196 Body3D *bodyA = world_ ? world_->findBodyById(drive_->getBodyAId()) : nullptr;
197 Body3D *bodyB = world_ ? world_->findBodyById(drive_->getBodyBId()) : nullptr;
198 if (!bodyA || !bodyB || !bodyA->isValid() || !bodyB->isValid()) return 0.f;
199 const float wx = bodyB->getAngularVelocityX() - bodyA->getAngularVelocityX();
200 const float wy = bodyB->getAngularVelocityY() - bodyA->getAngularVelocityY();
201 const float wz = bodyB->getAngularVelocityZ() - bodyA->getAngularVelocityZ();
202 return wx * axisX_ + wy * axisY_ + wz * axisZ_;
203}
204
206 if (!isValid()) throw eve::Exception("Mechanism3D.getSpinSpeed: mechanism destroyed");
207 return axisSpinSpeed();
208}
209
210void Mechanism3D::setDrive(float speed, float maxTorque) {
211 if (!isValid()) throw eve::Exception("Mechanism3D.setDrive: mechanism destroyed");
212 if (!std::isfinite(speed)) throw eve::Exception("Mechanism3D.setDrive: speed must be finite");
213 if (!std::isfinite(maxTorque) || maxTorque < 0.f)
214 throw eve::Exception("Mechanism3D.setDrive: maxTorque must be finite and >= 0");
215 driveEnabled_ = true;
216 driveSpeed_ = speed;
217 driveMaxTorque_ = maxTorque;
218 if (kind_ != Kind::Ratchet) {
219 drive_->setRevoluteMotor(true, speed, maxTorque);
220 }
221}
222
224 if (!isValid()) throw eve::Exception("Mechanism3D.clearDrive: mechanism destroyed");
225 driveEnabled_ = false;
226 driveSpeed_ = 0.f;
227 driveMaxTorque_ = 0.f;
228 if (kind_ != Kind::Ratchet) {
229 drive_->setRevoluteMotor(false, 0.f, 0.f);
230 }
231}
232
234 requireKind(Kind::Ratchet, "Mechanism3D.setRatchetDirection");
235 if (direction != 1 && direction != -1)
236 throw eve::Exception("Mechanism3D.setRatchetDirection: direction must be +1 or -1");
237 ratchetDirection_ = direction;
238}
239
241 requireKind(Kind::Ratchet, "Mechanism3D.setRatchetEngagementTorque");
242 if (!std::isfinite(torque) || torque < 0.f)
243 throw eve::Exception("Mechanism3D.setRatchetEngagementTorque: torque must be finite and >= 0");
244 engagementTorque_ = torque;
245}
246
248 if (kind_ != Kind::Ratchet || !isValid()) return;
249 const float signedSpeed = axisSpinSpeed() * static_cast<float>(ratchetDirection_);
250 if (signedSpeed < -1e-3f) {
251 drive_->setRevoluteMotor(true, 0.f, engagementTorque_);
252 return;
253 }
254 if (driveEnabled_) {
255 const float driveSpeed = std::fabs(driveSpeed_) * static_cast<float>(ratchetDirection_);
256 drive_->setRevoluteMotor(true, driveSpeed, driveMaxTorque_);
257 } else {
258 drive_->setRevoluteMotor(false, 0.f, 0.f);
259 }
260}
261
263 if (world_) world_->forgetMechanism(this);
264 world_ = nullptr;
265 drive_ = nullptr;
266 crankPin_ = nullptr;
267 sliderPin_ = nullptr;
268 slider_ = nullptr;
269}
270
272 if (drive_ && drive_->isValid()) drive_->destroy();
273 if (crankPin_ && crankPin_->isValid()) crankPin_->destroy();
274 if (sliderPin_ && sliderPin_->isValid()) sliderPin_->destroy();
275 if (slider_ && slider_->isValid()) slider_->destroy();
276 if (world_) world_->forgetMechanism(this);
277 invalidate();
278}
279
280} // namespace eve::physics
ActionParameterOperation operation
ForceVector torque
void(* destroy)(void *)
std::string id
RoadLaneDirection direction
float axisZ
Definition RockMesh.cpp:22
float axisY
Definition RockMesh.cpp:22
float axisX
Definition RockMesh.cpp:22
std::string kind
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
bool isValid() const
True while the underlying Box3D body is still alive.
Definition Body3D.cpp:57
float getAngularVelocityZ() const
Returns the angular velocity z.
Definition Body3D.cpp:161
float getAngularVelocityX() const
Returns the angular velocity x.
Definition Body3D.cpp:153
float getAngularVelocityY() const
Returns the angular velocity y.
Definition Body3D.cpp:157
void destroy()
Destroys the backend joint and invalidates this wrapper.
Definition Joint3D.cpp:39
float getPrismaticTranslation() const
Current prismatic translation in metres.
Definition Joint3D.cpp:158
float getRevoluteAngle() const
Current revolute angle in radians.
Definition Joint3D.cpp:146
bool isValid() const
Whether the backend joint still exists.
Definition Joint3D.cpp:30
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
int getBodyBId() const
Stable ID of attached body B, or -1 after invalidation.
Definition Joint3D.cpp:59
Mechanism2D(const Mechanism2D &)=delete
Mechanism3D(const Mechanism3D &)=delete
float getSliderTranslation() const
Current slider translation in metres (crank-slider only).
void setRatchetEngagementTorque(float torque)
Sets reverse-lock torque for ratchet engagement.
void destroy()
Destroys owned joints and invalidates this wrapper.
void setRatchetDirection(int direction)
Sets ratchet freewheel direction.
float getDriveAngle() const
Current drive revolute angle in radians (shaft/crank/ratchet).
void setDrive(float speed, float maxTorque)
Enables a continuous drive motor on the primary revolute.
void syncBeforeStep()
Internal: applies ratchet lock / drive before a solver step.
void invalidate()
Internal invalidation used by world teardown.
bool isValid() const
Whether the assembly and its primary joints are still live.
std::string getKind() const
Kind name: "crankSlider", "shaft", or "ratchet".
void clearDrive()
Disables the continuous drive motor.
float getSpinSpeed() const
Relative spin speed along the drive axis in radians/second.
void forgetMechanism(Mechanism3D *mechanism)
Internal: removes a mechanism wrapper from ownership bookkeeping.
Body3D * findBodyById(int bodyId) const
Borrows a live body by stable event/query id; never delete lookup results.
Definition World3D.cpp:439
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
ControlEdit slider(const char *label, float &value, float minimum, float maximum, const char *format="%.3f")
Shared immediate renderer for scalar sliders.