Hi, I am writing an action rpg with an overhead view.
Now I am trying to do star-trek style doors. They should slide into the walls and disappear instead of swinging open. They should not be able to make any other movement. I guess a prismatic joint would be perfect for this. I create two bodies, one with infinite mass (not setting the mass does this?) that acts as a hinge. The hinge resides inside the wall and will never be visible.
But, when the actor touches the door, the door very fast starts swinging wildly and in a matter of frames reaches escape velocity, leaving the boundaries of floating point precision 🙂
If I leave out the PrismaticJoint, the door behaves normal. So I guess the joint tries to compensate, overcompensates and the system becomes unstable.
I have written a small piece of test code that shows this and would love if someone could point out what I am doing wrong. If you notice something else fishy in my code, please let me know...
Now I am trying to do star-trek style doors. They should slide into the walls and disappear instead of swinging open. They should not be able to make any other movement. I guess a prismatic joint would be perfect for this. I create two bodies, one with infinite mass (not setting the mass does this?) that acts as a hinge. The hinge resides inside the wall and will never be visible.
But, when the actor touches the door, the door very fast starts swinging wildly and in a matter of frames reaches escape velocity, leaving the boundaries of floating point precision 🙂
If I leave out the PrismaticJoint, the door behaves normal. So I guess the joint tries to compensate, overcompensates and the system becomes unstable.
I have written a small piece of test code that shows this and would love if someone could point out what I am doing wrong. If you notice something else fishy in my code, please let me know...
Code:
public class Test {
public static void main(String[] args) {
World world = new World(new AABB(new Vec2(0, 0), new Vec2(10, 10)), new Vec2(0, 0), true);
Body door = createBody(world, 5, 5, true);
Body hinge = createBody(world, 5, 7, false);
PrismaticJointDef jointDef = new PrismaticJointDef();
jointDef.initialize(hinge, door, hinge.getPosition(), new Vec2(0, -2));
jointDef.enableLimit = true;
jointDef.enableMotor = true;
jointDef.lowerTranslation = -5;
jointDef.upperTranslation = 5;
PrismaticJoint joint = (PrismaticJoint) world.createJoint(jointDef);
debug(door, joint);
step(world, door, joint);
createBody(world, 3, 5, true).applyForce(new Vec2(1, 0), new Vec2(3, 5));
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
}
private static void step(World world, Body door, PrismaticJoint joint) {
world.step(0.05f, 1);
debug(door, joint);
}
private static void debug(Body door, PrismaticJoint joint) {
System.out.println("door = " + door.getPosition());
}
private static Body createBody(World world, float x, float y, boolean mass) {
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(x, y);
if (mass) {
bodyDef.massData.mass = 1;
}
Body body = world.createBody(bodyDef);
CircleDef shape = new CircleDef();
shape.radius = 1;
body.createShape(shape);
return body;
}
}
public static void main(String[] args) {
World world = new World(new AABB(new Vec2(0, 0), new Vec2(10, 10)), new Vec2(0, 0), true);
Body door = createBody(world, 5, 5, true);
Body hinge = createBody(world, 5, 7, false);
PrismaticJointDef jointDef = new PrismaticJointDef();
jointDef.initialize(hinge, door, hinge.getPosition(), new Vec2(0, -2));
jointDef.enableLimit = true;
jointDef.enableMotor = true;
jointDef.lowerTranslation = -5;
jointDef.upperTranslation = 5;
PrismaticJoint joint = (PrismaticJoint) world.createJoint(jointDef);
debug(door, joint);
step(world, door, joint);
createBody(world, 3, 5, true).applyForce(new Vec2(1, 0), new Vec2(3, 5));
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
step(world, door, joint);
}
private static void step(World world, Body door, PrismaticJoint joint) {
world.step(0.05f, 1);
debug(door, joint);
}
private static void debug(Body door, PrismaticJoint joint) {
System.out.println("door = " + door.getPosition());
}
private static Body createBody(World world, float x, float y, boolean mass) {
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(x, y);
if (mass) {
bodyDef.massData.mass = 1;
}
Body body = world.createBody(bodyDef);
CircleDef shape = new CircleDef();
shape.radius = 1;
body.createShape(shape);
return body;
}
}