Why Objects automatically start to rotate when I add a HingeJoint to it

I want to create a door. I build a box to represent the door and then add a fixed point on the wall . I want to let the door swing along Y-axis once it is hit by some other objects. But the problem is once I start running the program , the door will swing automatically instead of hitting by something else. And it seems that the swing won’t stop except the door collide with other object. How can I let the door stay closed at the beginning?

It sounds like you have physics enabled on the door hinge thing. And well some small force is swinging it open. But really more details would be great.

i have PhysicsControl on both wall and door

I’d suggest setting some small angular damping or friction on your door (just enough to stop it from spontaneously swinging). If you use damping you can use a physics collision listener and remove the damping as soon as something collides with the door, allowing it to swing freely after the initial collision occurs. On the other hand, adding an appropriate amount of friction to the joint will give you a more realistic physics simulation and you really wouldn’t have any need to remove it.

This is example code for physic door. This code is from JME CookBook. (I hope it is OK to put code from book here :innocent:)

/**
 *
 * @author reden
 */
public class TestDoor extends CharacterInputTest {

    public static void main(String[] args){
        TestDoor testDoor = new TestDoor();
        testDoor.start();
    }
    
//    private BulletAppState bulletAppState;
    private HingeJoint joint;
    
    Material mat;

    @Override
    public void simpleInitApp() {
        super.simpleInitApp();
        
        initMaterial();
        
        bulletAppState.setDebugEnabled(true);
        
        RigidBodyControl attachment = new RigidBodyControl(new BoxCollisionShape(new Vector3f(.1f, .1f, .1f)), 0);
        attachment.setPhysicsLocation(new Vector3f(-5f, 1.52f, 0f));
        bulletAppState.getPhysicsSpace().add(attachment);
        
        Geometry doorGeometry = new Geometry("Door", new Box(0.6f, 1.5f, 0.1f));
        
        doorGeometry.setMaterial(mat);
        RigidBodyControl doorPhysicsBody = new RigidBodyControl(new BoxCollisionShape(new Vector3f(.6f, 1.5f, .1f)), 1);
        bulletAppState.getPhysicsSpace().add(doorPhysicsBody);
        doorGeometry.addControl(doorPhysicsBody);
        
        rootNode.attachChild(doorGeometry);
        
        joint = new HingeJoint(attachment, doorPhysicsBody, new Vector3f(0f, 0f, 0f), new Vector3f(-1f, 0f, 0f), Vector3f.UNIT_Y, Vector3f.UNIT_Y);
        joint.setLimit(-FastMath.HALF_PI - 0.1f, FastMath.HALF_PI + 0.1f);
        bulletAppState.getPhysicsSpace().add(joint);

        DoorCloseControl doorControl = new DoorCloseControl();
        doorControl.setHingeJoint(joint);
        doorGeometry.addControl(doorControl);
    }
    
    public void initMaterial() {
        mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    }

    @Override
    public void update() {
        super.update();
    }

    
}

DoorCloseControl

/**
 *
 * @author reden
 */
public class DoorCloseControl extends AbstractControl{

    private HingeJoint joint;
    
    private float timeOpen;
    
    public DoorCloseControl(){
    }
    
    @Override
    protected void controlUpdate(float tpf) {
        float angle = joint.getHingeAngle();
        if(angle > 0.1f || angle < -0.1f){
            timeOpen += tpf;
        } else {
            timeOpen = 0f;
        }
        
        if(timeOpen > 5){
            float speed = angle > 0 ? -0.9f : 0.9f;
            joint.enableMotor(true, speed, 0.1f);
            spatial.getControl(RigidBodyControl.class).activate();
        } else {
            joint.enableMotor(true, 0, 1);
        }
    }
    
    public void setHingeJoint(HingeJoint joint){
        this.joint = joint;
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }
    
}

tested it and it works fine as you want.