16 Joint2D *slider,
int ratchetDirection,
float engagementTorque)
22 sliderPin_(sliderPin),
24 ratchetDirection_(ratchetDirection),
25 engagementTorque_(engagementTorque) {}
29std::string Mechanism2D::getKind()
const {
31 case Kind::CrankSlider:
return "crankSlider";
32 case Kind::Shaft:
return "shaft";
33 case Kind::Ratchet:
return "ratchet";
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_ &&
47void Mechanism2D::requireKind(Kind expected,
const char *
operation)
const {
52float Mechanism2D::getDriveAngle()
const {
53 if (!isValid())
throw eve::Exception(
"Mechanism2D.getDriveAngle: mechanism destroyed");
54 return drive_->getRevoluteAngle();
57float Mechanism2D::getSliderTranslation()
const {
58 requireKind(Kind::CrankSlider,
"Mechanism2D.getSliderTranslation");
59 return slider_->getPrismaticTranslation();
62float Mechanism2D::axisSpinSpeed()
const {
63 if (!isValid() || !drive_)
return 0.f;
64 return drive_->getRevoluteSpeed();
67float Mechanism2D::getSpinSpeed()
const {
68 if (!isValid())
throw eve::Exception(
"Mechanism2D.getSpinSpeed: mechanism destroyed");
69 return axisSpinSpeed();
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");
79 driveMaxTorque_ = maxTorque;
80 if (kind_ != Kind::Ratchet) drive_->setRevoluteMotor(
true, speed, maxTorque);
83void Mechanism2D::clearDrive() {
84 if (!isValid())
throw eve::Exception(
"Mechanism2D.clearDrive: mechanism destroyed");
85 driveEnabled_ =
false;
87 driveMaxTorque_ = 0.f;
88 if (kind_ != Kind::Ratchet) drive_->setRevoluteMotor(
false, 0.f, 0.f);
91void Mechanism2D::setRatchetDirection(
int direction) {
92 requireKind(Kind::Ratchet,
"Mechanism2D.setRatchetDirection");
94 throw eve::Exception(
"Mechanism2D.setRatchetDirection: direction must be +1 or -1");
98void Mechanism2D::setRatchetEngagementTorque(
float torque) {
99 requireKind(Kind::Ratchet,
"Mechanism2D.setRatchetEngagementTorque");
101 throw eve::Exception(
"Mechanism2D.setRatchetEngagementTorque: torque must be finite and >= 0");
102 engagementTorque_ =
torque;
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_);
113 const float driveSpeed = std::fabs(driveSpeed_) *
static_cast<float>(ratchetDirection_);
114 drive_->setRevoluteMotor(
true, driveSpeed, driveMaxTorque_);
116 drive_->setRevoluteMotor(
false, 0.f, 0.f);
120void Mechanism2D::invalidate() {
121 if (world_) world_->forgetMechanism(
this);
125 sliderPin_ =
nullptr;
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);
144 Joint3D *slider,
float axisX,
float axisY,
float axisZ,
int ratchetDirection,
145 float engagementTorque)
151 sliderPin_(sliderPin),
156 ratchetDirection_(ratchetDirection),
157 engagementTorque_(engagementTorque) {}
171 if (!world_ || !drive_ || !drive_->
isValid())
return false;
173 return crankPin_ && crankPin_->
isValid() && sliderPin_ && sliderPin_->
isValid() && slider_ &&
179void Mechanism3D::requireKind(Kind expected,
const char *
operation)
const {
194float Mechanism3D::axisSpinSpeed()
const {
195 if (!
isValid() || !drive_)
return 0.f;
198 if (!bodyA || !bodyB || !bodyA->
isValid() || !bodyB->
isValid())
return 0.f;
202 return wx * axisX_ +
wy * axisY_ +
wz * axisZ_;
207 return axisSpinSpeed();
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;
217 driveMaxTorque_ = maxTorque;
225 driveEnabled_ =
false;
227 driveMaxTorque_ = 0.f;
234 requireKind(
Kind::Ratchet,
"Mechanism3D.setRatchetDirection");
236 throw eve::Exception(
"Mechanism3D.setRatchetDirection: direction must be +1 or -1");
241 requireKind(
Kind::Ratchet,
"Mechanism3D.setRatchetEngagementTorque");
243 throw eve::Exception(
"Mechanism3D.setRatchetEngagementTorque: torque must be finite and >= 0");
244 engagementTorque_ =
torque;
249 const float signedSpeed = axisSpinSpeed() *
static_cast<float>(ratchetDirection_);
250 if (signedSpeed < -1e-3f) {
255 const float driveSpeed = std::fabs(driveSpeed_) *
static_cast<float>(ratchetDirection_);
267 sliderPin_ =
nullptr;
ActionParameterOperation operation
RoadLaneDirection direction
EVENGINE_API_FOUNDATION public API.
Backend-neutral 3D rigid body in meter-space coordinates (+Y up by convention). Owned by a World3D; c...
bool isValid() const
True while the underlying Box3D body is still alive.
float getAngularVelocityZ() const
Returns the angular velocity z.
float getAngularVelocityX() const
Returns the angular velocity x.
float getAngularVelocityY() const
Returns the angular velocity y.
void destroy()
Destroys the backend joint and invalidates this wrapper.
float getPrismaticTranslation() const
Current prismatic translation in metres.
float getRevoluteAngle() const
Current revolute angle in radians.
bool isValid() const
Whether the backend joint still exists.
void setRevoluteMotor(bool enabled, float speed, float maxTorque)
Configures the revolute motor in radians/second and newton-metres.
int getBodyAId() const
Stable ID of attached body A, or -1 after invalidation.
int getBodyBId() const
Stable ID of attached body B, or -1 after invalidation.
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.
Optional physics backend for vehicle mobility and body attach.
ControlEdit slider(const char *label, float &value, float minimum, float maximum, const char *format="%.3f")
Shared immediate renderer for scalar sliders.