Block rigidbody rotation

Hi, i want to block a rigidbody physic rotation, i tryed to set the quaternion but the y location of the rigidbody started to go up…

What does this mean?

Why did you delete the post that actually had the information we’d need to help you?

Hint: if you are calling quaterion.set(x, y, z, w) then you are already doing something wrong. Probably setting total nonsense to the quaternion.

2 Likes

i’m setting the rigidbody.setphysicRotation(quaternion), make quaternion using a matrice like that

private float [][] rotPos = {
            {1.0f, 0.0f ,0.0f},
            {0.0f, 1.0f ,0.0f},
            {0.0f, 0.0f ,1.0f}
        };

(sorry for eng, i’m italian)

Show all the code.

1 Like
private void blockRotation(){
    
    Quaternion qt = this.getControl(RigidBodyControl.class).getPhysicsRotation();
    qt.set(0f, 0f, 0f, 1f);
    this.getControl(RigidBodyControl.class).setPhysicsRotation(qt);
}

this method is called with simpleUpdate (this is extended to Node)

Every update, you zero out all rotation but any rotational acceleration has to go somewhere.

We still don’t see enough code.

Put together a simple test case that fully shows the problem.

public class Player extends Node implements ActionListener, AnalogListener{

RigidBodyControl rb;
InputManager inputManager;

Geometry playerGeom;

private Vector3f startPos;



private float [][] rotPos = {
            {1.0f, 0.0f ,0.0f},
            {0.0f, 1.0f ,0.0f},
            {0.0f, 0.0f ,1.0f}
        };

FpsCam cam;

float playerWalkSpeed = 5.0f;
public Player(AssetManager assetManager, InputManager inputManager, FpsCam cam){
    this.cam = cam;
    startPos = new Vector3f(0f,2f,0f);
    
    this.inputManager = inputManager;
    
    setPlayerInternal(assetManager);

    this.attachChild(playerGeom);
    
    initKeys();
}

private void setPlayerInternal(AssetManager assetManager){
    Material mat = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
    playerGeom = new Geometry("player", new Box(0.5f,1f,0.5f));
    playerGeom.setMaterial(mat);
    
    playerGeom.setLocalTranslation(startPos);
    
    CollisionShape shape = CollisionShapeFactory.createBoxShape(playerGeom);
    rb = new RigidBodyControl(shape,0.001f);
    rb.setGravity(new Vector3f(0f,-1f,0f));
    rb.setPhysicsLocation(startPos);
    rb.setAngularDamping(1f);
    rb.setFriction(1f);
    
    //rb.setPhysicsRotation(new Quaternion(0f,0f,0f,0f));
    this.addControl(rb);
    playerGeom.addControl(rb);
    
    
    
}

private void blockRotation(){
    
    Quaternion qt = this.getControl(RigidBodyControl.class).getPhysicsRotation();
    qt.set(0f, 0f, 0f, 1f);
    this.getControl(RigidBodyControl.class).setPhysicsRotation(qt);
}

public void update(float tpf){
    cam.setLocalPosition(new Vector3f(playerGeom.getLocalTranslation().x -2f,playerGeom.getLocalTranslation().y + 1.3f,playerGeom.getLocalTranslation().z));
    //this.getControl(RigidBodyControl.class).setPhysicsRotation(Quaternion.IDENTITY);
    blockRotation();
    
}

//update methods
@Override
public void onAction(String action, boolean isPresed, float tpf) {
    switch(action){
        case "jump":
            this.getControl(RigidBodyControl.class).setLinearVelocity(new Vector3f(
                    this.getControl(RigidBodyControl.class).getLinearVelocity().x,
                    5f,
                    this.getControl(RigidBodyControl.class).getLinearVelocity().z              
            ));
            break;
    }
    
    //this.getControl(RigidBodyControl.class).applyCentralForce(new Vector3f(cam.getCam().getDirection().x * playerWalkSpeed,0f,cam.getCam().getDirection().z*playerWalkSpeed));
}

private void initKeys(){
    inputManager.addMapping("up", new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping("down", new KeyTrigger(KeyInput.KEY_S));
    inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_D));
    inputManager.addMapping("jump", new KeyTrigger(KeyInput.KEY_SPACE));
    
    
    inputManager.addListener(this, "up");
    inputManager.addListener(this, "down");
    inputManager.addListener(this, "left");
    inputManager.addListener(this, "jump");
}

@Override
public void onAnalog(String action, float arg1, float arg2) {
    switch(action){
        case "up":
        this.getControl(RigidBodyControl.class).setLinearVelocity(new Vector3f(
                    cam.getCam().getDirection().x*playerWalkSpeed,
                    this.getControl(RigidBodyControl.class).getLinearVelocity().y,
                    cam.getCam().getDirection().z*playerWalkSpeed             
        ));
            break;
        case "down":
        this.getControl(RigidBodyControl.class).setLinearVelocity(new Vector3f(
                    -1f * cam.getCam().getDirection().x*playerWalkSpeed,
                    this.getControl(RigidBodyControl.class).getLinearVelocity().y,
                    -1f * cam.getCam().getDirection().z*playerWalkSpeed             
        ));
            break;
    }
    //System.out.println(cam.getCam().getDirection().x * (0.6f - this.getControl(RigidBodyControl.class).getLinearVelocity().x)/playerWalkSpeed);
    System.out.println(this.getControl(RigidBodyControl.class).getPhysicsRotation());
}

}

this is the class

A good way to block the rotation of a rigid body is to set its “angular factors” to zero:

body.setAngularFactor(new Vector3f(0f, 0f, 0f));

For more information see the Minie tutorial on rigid bodies.