7#include <Box2D/Box2D.h>
35 case Kind::Distance:
return "distance";
36 case Kind::Revolute:
return "revolute";
37 case Kind::Prismatic:
return "prismatic";
38 case Kind::Weld:
return "weld";
39 case Kind::Wheel:
return "wheel";
40 case Kind::Motor:
return "motor";
41 case Kind::Gear:
return "gear";
42 case Kind::Pulley:
return "pulley";
48 return isValid() ?
static_cast<Body *
>(joint_->GetBodyA()->GetUserData())->
getId() : -1;
51 return isValid() ?
static_cast<Body *
>(joint_->GetBodyB()->GetUserData())->
getId() : -1;
58 if (collide != joint_->GetCollideConnected())
60 "Joint2D.setCollideConnected: collideConnected is fixed at creation for Box2D joints");
64 return isValid() ? joint_->GetCollideConnected() :
false;
67void Joint2D::requireKind(Kind expected,
const char *
operation)
const {
73 requireKind(Kind::Distance,
"Joint2D.setDistanceLength");
74 nonNegative(lengthPixels,
"Joint2D.setDistanceLength",
"length");
75 static_cast<b2DistanceJoint *
>(joint_)->SetLength(world_->
toMeters(lengthPixels));
78 requireKind(Kind::Distance,
"Joint2D.getDistanceLength");
79 return world_->
toPixels(
static_cast<b2DistanceJoint *
>(joint_)->GetLength());
82 requireKind(Kind::Distance,
"Joint2D.setDistanceSpring");
83 nonNegative(frequencyHz,
"Joint2D.setDistanceSpring",
"frequencyHz");
84 nonNegative(
dampingRatio,
"Joint2D.setDistanceSpring",
"dampingRatio");
85 auto *
distance =
static_cast<b2DistanceJoint *
>(joint_);
90 requireKind(Kind::Distance,
"Joint2D.getDistanceFrequency");
91 return static_cast<b2DistanceJoint *
>(joint_)->GetFrequency();
94 requireKind(Kind::Distance,
"Joint2D.getDistanceDampingRatio");
95 return static_cast<b2DistanceJoint *
>(joint_)->GetDampingRatio();
99 requireKind(Kind::Revolute,
"Joint2D.setRevoluteLimits");
100 finite(lower,
"Joint2D.setRevoluteLimits",
"lower");
101 finite(upper,
"Joint2D.setRevoluteLimits",
"upper");
103 throw eve::Exception(
"Joint2D.setRevoluteLimits: lower must be <= upper");
104 auto *revolute =
static_cast<b2RevoluteJoint *
>(joint_);
105 revolute->SetLimits(lower, upper);
106 revolute->EnableLimit(
enabled);
109 requireKind(Kind::Revolute,
"Joint2D.setRevoluteMotor");
110 finite(speed,
"Joint2D.setRevoluteMotor",
"speed");
111 nonNegative(maxTorque,
"Joint2D.setRevoluteMotor",
"maxTorque");
112 auto *revolute =
static_cast<b2RevoluteJoint *
>(joint_);
113 revolute->SetMotorSpeed(speed);
114 revolute->SetMaxMotorTorque(maxTorque);
115 revolute->EnableMotor(
enabled);
118 requireKind(Kind::Revolute,
"Joint2D.getRevoluteAngle");
119 return static_cast<b2RevoluteJoint *
>(joint_)->GetJointAngle();
122 requireKind(Kind::Revolute,
"Joint2D.getRevoluteSpeed");
123 return static_cast<b2RevoluteJoint *
>(joint_)->GetJointSpeed();
126 requireKind(Kind::Revolute,
"Joint2D.getRevoluteMotorTorque");
127 return static_cast<b2RevoluteJoint *
>(joint_)->GetMotorTorque(1.f / 60.f);
131 requireKind(Kind::Prismatic,
"Joint2D.setPrismaticLimits");
132 finite(lowerPixels,
"Joint2D.setPrismaticLimits",
"lower");
133 finite(upperPixels,
"Joint2D.setPrismaticLimits",
"upper");
134 if (lowerPixels > upperPixels)
135 throw eve::Exception(
"Joint2D.setPrismaticLimits: lower must be <= upper");
136 auto *prismatic =
static_cast<b2PrismaticJoint *
>(joint_);
137 prismatic->SetLimits(world_->
toMeters(lowerPixels), world_->
toMeters(upperPixels));
138 prismatic->EnableLimit(
enabled);
141 requireKind(Kind::Prismatic,
"Joint2D.setPrismaticMotor");
142 finite(speedPixels,
"Joint2D.setPrismaticMotor",
"speed");
143 nonNegative(maxForcePixels,
"Joint2D.setPrismaticMotor",
"maxForce");
144 auto *prismatic =
static_cast<b2PrismaticJoint *
>(joint_);
145 prismatic->SetMotorSpeed(world_->
toMeters(speedPixels));
146 prismatic->SetMaxMotorForce(world_->
toMeters(maxForcePixels));
147 prismatic->EnableMotor(
enabled);
150 requireKind(Kind::Prismatic,
"Joint2D.getPrismaticTranslation");
151 return world_->
toPixels(
static_cast<b2PrismaticJoint *
>(joint_)->GetJointTranslation());
154 requireKind(Kind::Prismatic,
"Joint2D.getPrismaticSpeed");
155 return world_->
toPixels(
static_cast<b2PrismaticJoint *
>(joint_)->GetJointSpeed());
159 requireKind(Kind::Weld,
"Joint2D.setWeldSpring");
160 nonNegative(frequencyHz,
"Joint2D.setWeldSpring",
"frequencyHz");
161 nonNegative(
dampingRatio,
"Joint2D.setWeldSpring",
"dampingRatio");
162 auto *weld =
static_cast<b2WeldJoint *
>(joint_);
163 weld->SetFrequency(frequencyHz);
167 requireKind(Kind::Weld,
"Joint2D.getWeldFrequency");
168 return static_cast<b2WeldJoint *
>(joint_)->GetFrequency();
171 requireKind(Kind::Weld,
"Joint2D.getWeldDampingRatio");
172 return static_cast<b2WeldJoint *
>(joint_)->GetDampingRatio();
176 requireKind(Kind::Wheel,
"Joint2D.setWheelSpring");
177 nonNegative(frequencyHz,
"Joint2D.setWheelSpring",
"frequencyHz");
178 nonNegative(
dampingRatio,
"Joint2D.setWheelSpring",
"dampingRatio");
179 auto *wheel =
static_cast<b2WheelJoint *
>(joint_);
180 wheel->SetSpringFrequencyHz(frequencyHz);
184 requireKind(Kind::Wheel,
"Joint2D.setWheelMotor");
185 finite(speed,
"Joint2D.setWheelMotor",
"speed");
186 nonNegative(maxTorque,
"Joint2D.setWheelMotor",
"maxTorque");
187 auto *wheel =
static_cast<b2WheelJoint *
>(joint_);
188 wheel->SetMotorSpeed(speed);
189 wheel->SetMaxMotorTorque(maxTorque);
193 requireKind(Kind::Wheel,
"Joint2D.getWheelTranslation");
194 return world_->
toPixels(
static_cast<b2WheelJoint *
>(joint_)->GetJointTranslation());
197 requireKind(Kind::Wheel,
"Joint2D.getWheelSpeed");
198 return static_cast<b2WheelJoint *
>(joint_)->GetJointSpeed();
202 requireKind(Kind::Motor,
"Joint2D.setMotorLinearOffset");
203 finite(xPixels,
"Joint2D.setMotorLinearOffset",
"x");
204 finite(yPixels,
"Joint2D.setMotorLinearOffset",
"y");
205 static_cast<b2MotorJoint *
>(joint_)->SetLinearOffset(
209 requireKind(Kind::Motor,
"Joint2D.setMotorAngularOffset");
210 finite(radians,
"Joint2D.setMotorAngularOffset",
"radians");
211 static_cast<b2MotorJoint *
>(joint_)->SetAngularOffset(radians);
214 requireKind(Kind::Motor,
"Joint2D.setMotorLimits");
215 nonNegative(maxForcePixels,
"Joint2D.setMotorLimits",
"maxForce");
216 nonNegative(maxTorque,
"Joint2D.setMotorLimits",
"maxTorque");
217 auto *motor =
static_cast<b2MotorJoint *
>(joint_);
218 motor->SetMaxForce(world_->
toMeters(maxForcePixels));
219 motor->SetMaxTorque(maxTorque);
222 requireKind(Kind::Motor,
"Joint2D.getMotorLinearOffsetX");
223 return world_->
toPixels(
static_cast<b2MotorJoint *
>(joint_)->GetLinearOffset().
x);
226 requireKind(Kind::Motor,
"Joint2D.getMotorLinearOffsetY");
227 return world_->
toPixels(
static_cast<b2MotorJoint *
>(joint_)->GetLinearOffset().
y);
230 requireKind(Kind::Motor,
"Joint2D.getMotorAngularOffset");
231 return static_cast<b2MotorJoint *
>(joint_)->GetAngularOffset();
235 requireKind(Kind::Gear,
"Joint2D.setGearRatio");
236 if (!std::isfinite(
ratio) || std::fabs(
ratio) < 1e-8f)
237 throw eve::Exception(
"Joint2D.setGearRatio: ratio must be finite and non-zero");
238 static_cast<b2GearJoint *
>(joint_)->SetRatio(
ratio);
241 requireKind(Kind::Gear,
"Joint2D.getGearRatio");
242 return static_cast<b2GearJoint *
>(joint_)->GetRatio();
ActionParameterOperation operation
EVENGINE_API_FOUNDATION public API.
2D rigid body (Box2D) in pixel-space coordinates. Owned by a World; create shapes with newRectangleFi...
Owning planar joint wrapper observing World-owned backend state. @ownership Caller/VM owns factory wr...
bool isValid() const noexcept
True until wrapper release or a body/world/source-joint is destroyed.
void setWheelSpring(float frequencyHz, float dampingRatio)
Configures wheel suspension spring.
int getId() const noexcept
World-local persistent topology id, retained after invalidation; not a runtime handle.
void setGearRatio(float ratio)
Changes gear ratio.
float getMotorAngularOffset() const
Motor angular offset in radians.
Joint2D * getGearJoint1() const
First joint linked by a gear, or null.
std::string getKind() const
Joint kind string for script/debug.
float getRevoluteSpeed() const
Current revolute speed in radians/second.
void setPrismaticMotor(bool enabled, float speedPixels, float maxForcePixels)
Configures a prismatic motor in pixels/second and pixel-force units.
float getWeldFrequency() const
Weld spring frequency in Hertz.
void setMotorAngularOffset(float radians)
Sets motor-joint angular offset in radians.
float getDistanceLength() const
Distance-joint rest length in pixels.
void destroy()
Legacy throwing destruction; delegates to destroyChecked().
Joint2D * getGearJoint2() const
Second joint linked by a gear, or null.
void setWeldSpring(float frequencyHz, float dampingRatio)
Configures weld rotational spring frequency and damping.
float getMotorLinearOffsetX() const
Motor linear offset X in pixels.
void setPrismaticLimits(bool enabled, float lowerPixels, float upperPixels)
Configures prismatic translation limits in pixels.
void setCollideConnected(bool collide)
Enables or disables collision between the attached bodies.
void setMotorLimits(float maxForcePixels, float maxTorque)
Sets motor max force (pixel-force) and torque (N·m).
void setWheelMotor(bool enabled, float speed, float maxTorque)
Configures wheel spin motor in radians/second and newton-metres.
float getPrismaticSpeed() const
Current prismatic speed in pixels/second.
int getBodyBId() const
Stable ID of attached body B, or -1 after invalidation.
void setDistanceSpring(float frequencyHz, float dampingRatio)
Configures distance spring frequency (Hz) and damping ratio.
bool getCollideConnected() const
Whether attached bodies may collide.
float getDistanceFrequency() const
Distance spring frequency in Hertz.
Result< void > destroyChecked()
Remove this constraint and dependent gears; repeat destruction is successful.
float getWheelTranslation() const
Current wheel translation along the suspension axis in pixels.
int getBodyAId() const
Stable ID of attached body A, or -1 after invalidation.
void setRevoluteMotor(bool enabled, float speed, float maxTorque)
Configures the revolute motor in radians/second and newton-metres.
float getRevoluteAngle() const
Current revolute angle in radians.
float getMotorLinearOffsetY() const
Motor linear offset Y in pixels.
void setRevoluteLimits(bool enabled, float lower, float upper)
Configures revolute angular limits in radians.
float getGearRatio() const
Current gear ratio.
float getWeldDampingRatio() const
Weld spring damping ratio.
float getPrismaticTranslation() const
Current prismatic translation in pixels.
void setDistanceLength(float lengthPixels)
Changes distance-joint rest length in pixels.
float getWheelSpeed() const
Current wheel spin speed in radians/second.
float getDistanceDampingRatio() const
Distance spring damping ratio.
void setMotorLinearOffset(float xPixels, float yPixels)
Sets motor-joint linear offset in pixels (bodyA frame).
float getRevoluteMotorTorque() const
Current revolute motor torque in newton-metres.
Joint2D * findJoint(PhysicsJointHandle handle) const
Resolves a live joint handle; returns null when stale or foreign.
float toPixels(float meters) const
Converts a meter-space length to pixels.
float toMeters(float pixels) const
Converts a pixel-space length to meters.
Optional physics backend for vehicle mobility and body attach.