Skip to content

Commit 3a1e7ad

Browse files
committed
feat: added support for box2d prismatic joint
1 parent ceba828 commit 3a1e7ad

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsWorld.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,30 @@ public RopeJoint addRopeJoint(Entity e1, Entity e2, Point2D localAnchor1, Point2
711711
return addJoint(e1, e2, def);
712712
}
713713

714+
/**
715+
* Add a prismatic joint (a slider along a given axis) between two entities.
716+
* More details: https://en.wikipedia.org/wiki/Prismatic_joint
717+
*
718+
* @param e1 entity1
719+
* @param e2 entity2
720+
* @param axis the axis along which to slide
721+
* @param limit max slide distance in pixels
722+
* @return joint
723+
*/
724+
public PrismaticJoint addPrismaticJoint(Entity e1, Entity e2, Point2D axis, double limit) {
725+
checkJointRequirements(e1, e2);
726+
727+
var p1 = e1.getComponent(PhysicsComponent.class);
728+
var p2 = e2.getComponent(PhysicsComponent.class);
729+
730+
var def = new PrismaticJointDef();
731+
def.initialize(p1.getBody(), p2.getBody(), p2.getBody().getWorldCenter(), toVector(axis));
732+
def.enableLimit = true;
733+
def.upperTranslation = toMetersF(limit);
734+
735+
return addJoint(e1, e2, def);
736+
}
737+
714738
/**
715739
* Add a joint constraining two entities with PhysicsComponent.
716740
* The entities must already be in the game world.

fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/dynamics/joints/PrismaticJointDef.java

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,76 +16,61 @@
1616
* coincide in world space. Using local anchors and a local axis helps when saving and loading a
1717
* game.
1818
*
19-
* @warning at least one body should by dynamic with a non-fixed rotation.
20-
* @author Daniel
19+
* Note: at least one body should by dynamic with a non-fixed rotation.
2120
*
21+
* @author Daniel
2222
*/
2323
public class PrismaticJointDef extends JointDef<PrismaticJoint> {
2424

25-
2625
/**
2726
* The local anchor point relative to body1's origin.
2827
*/
29-
public final Vec2 localAnchorA;
28+
public final Vec2 localAnchorA = new Vec2();
3029

3130
/**
3231
* The local anchor point relative to body2's origin.
3332
*/
34-
public final Vec2 localAnchorB;
33+
public final Vec2 localAnchorB = new Vec2();
3534

3635
/**
3736
* The local translation axis in body1.
3837
*/
39-
public final Vec2 localAxisA;
38+
public final Vec2 localAxisA = new Vec2(1.0f, 0.0f);
4039

4140
/**
4241
* The constrained angle between the bodies: body2_angle - body1_angle.
4342
*/
44-
public float referenceAngle;
43+
public float referenceAngle = 0.0f;
4544

4645
/**
4746
* Enable/disable the joint limit.
4847
*/
49-
public boolean enableLimit;
48+
public boolean enableLimit = false;
5049

5150
/**
5251
* The lower translation limit, usually in meters.
5352
*/
54-
public float lowerTranslation;
53+
public float lowerTranslation = 0.0f;
5554

5655
/**
5756
* The upper translation limit, usually in meters.
5857
*/
59-
public float upperTranslation;
58+
public float upperTranslation = 0.0f;
6059

6160
/**
6261
* Enable/disable the joint motor.
6362
*/
64-
public boolean enableMotor;
63+
public boolean enableMotor = false;
6564

6665
/**
6766
* The maximum motor torque, usually in N-m.
6867
*/
69-
public float maxMotorForce;
68+
public float maxMotorForce = 0.0f;
7069

7170
/**
7271
* The desired motor speed in radians per second.
7372
*/
74-
public float motorSpeed;
75-
76-
public PrismaticJointDef() {
77-
localAnchorA = new Vec2();
78-
localAnchorB = new Vec2();
79-
localAxisA = new Vec2(1.0f, 0.0f);
80-
referenceAngle = 0.0f;
81-
enableLimit = false;
82-
lowerTranslation = 0.0f;
83-
upperTranslation = 0.0f;
84-
enableMotor = false;
85-
maxMotorForce = 0.0f;
86-
motorSpeed = 0.0f;
87-
}
88-
73+
public float motorSpeed = 0.0f;
8974

9075
/**
9176
* Initialize the bodies, anchors, axis, and reference angle using the world anchor and world

0 commit comments

Comments
 (0)