Problem with prismatic joint

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...

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;
    }
}
Oh yea, sorry... the version I'm using is in a jar called:
jbox2d-2.0.1-library-only.jar
I didn't get it to work, but worked around it with static bodies around the door body. Works very well.

Please do tell if you find whats wrong. 🙂
Are you basically wanting to pull on shape into another? If so, turn off the collisions between the two shapes connected by the joint (collideConnected = false on the jointdef).
Quote:
Are you basically wanting to pull on shape into another? If so, turn off the collisions between the two shapes connected by the joint (collideConnected = false on the jointdef).


This isn't the problem (did you run my code?). The problem is that the joint cause the simulation to become unstable.
Didn't run it. Looks like it is connecting two dynamic circles of the same size/weight together with a joint. I could see why that be unstable. One of them needs to be fixed (aka static) by setting the shapedef's density to zero and calling setMassFromShapes() on the body after adding the shape.

I was mainly going off what I thought you were wanting to do from the description. I'd make the wall a static object, with the door being dynamic. Can then set the prismatic joint so that it pulls the door into the wall. Need the collide setting to be false or the door won't go into the wall.

I don't have time now, but if you can't get it working I'll try to write up later tonight.
Quote:
I don't have time now, but if you can't get it working I'll try to write up later tonight.


That would be nice, please do. 🙂

Note however, the hinge body in my example is not given any mass (see the boolean parameter "mass" passed to createBody). So, the hinge is static as far as I can tell.
A shapes density is by default 0. It is no difference if both bodies have mass.

I don't understand why the system would be unstable if both bodies were non-static. I see them like two balls connected by a coil inside a tube, so it can't bend, only expand and contract. Hitting them with a third ball should only cause them to rotate away slowly together, not swinging back and forth aproaching lightspeed. Am I wrong?
Sorry for the delay. Will try to work on when I get back from vacation later this week.

Original thread: https://box2d.org/forum/viewtopic.php?t=5160