Get rotation angle of JBullet RigidBody to render it using LWJGL.

I am using the Bullet Physics port to java, JBullet, and am trying to render a vehicle body that i created in Blender on top of the vehicle chassis. Since i cannot effectively find a way to render bullets debug, i have to get all of the rotations and position from the chassis RigidBody. I then set the positions and render a blender model. Is there anyway i could computer a yaw angle of the chassis RigidBody so i could rotate the blender model in lwjgl?

Getting the rigidbody:

glPushMatrix();
    Vector3f pos = vehicle.getRigidBody().getWorldTransform(new Transform()).origin;
    Matrix3f rotation = vehicle.getRigidBody().getWorldTransform(new Transform()).basis;
    glTranslatef(pos.x, pos.y + 1, pos.z);
    glRotatef(Don't know what to put here, 0, 1, 0);
    model.render();
    glPopMatrix();

Code for creating the vehicle:

vehicleChassisShape = new BoxShape(new Vector3f(1.f, 0.5f, 2.f));
    vehicleBody = new CompoundShape();

    localTrans.setIdentity();
    localTrans.transform(new Vector3f(0, 0, 0));
    vehicleBody.addChildShape(localTrans, vehicleChassisShape);

    localTrans.transform(new Vector3f(3, 0.f, 0));
    vehicleMotionState = new DefaultMotionState(localTrans);
    Vector3f vehicleInertia = new Vector3f(0, 0, 0);
    vehicleBody.calculateLocalInertia(vehicleMass, vehicleInertia);
    RigidBodyConstructionInfo vehicleRigidBodyCI = new RigidBodyConstructionInfo(vehicleMass, vehicleMotionState, vehicleBody, vehicleInertia);

    vehicleRigidBody = new RigidBody(vehicleRigidBodyCI);
    dynamicsWorld.addRigidBody(vehicleRigidBody);

    //wheelShape = new CylinderShapeX(new Vector3f(wheelWidth, wheelRadius, wheelRadius));

    vehicleRayCaster = new DefaultVehicleRaycaster(dynamicsWorld);
    vehicle = new RaycastVehicle(vehicleTuning, vehicleRigidBody, vehicleRayCaster);

    // never deactivate vehicle
    vehicleRigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
    dynamicsWorld.addVehicle(vehicle);

    float connectionHeight = 1.2f;
    boolean isFrontWheel = true;
    // rightIndex, upIndex, forwardIndex
    vehicle.setCoordinateSystem(0, 1, 2); // 0, 1, 2

    // add wheels
    // front left
    Vector3f connectionPointCS0 = new Vector3f(CUBE_HALF_EXTENT - (0.3f * wheelWidth), connectionHeight, 2 * CUBE_HALF_EXTENT - wheelRadius);
    vehicle.addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, vehicleTuning, isFrontWheel);
    // front right
    connectionPointCS0 = new Vector3f(-CUBE_HALF_EXTENT + (0.3f * wheelWidth), connectionHeight, 2 * CUBE_HALF_EXTENT - wheelRadius);
    vehicle.addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, vehicleTuning, isFrontWheel);
    isFrontWheel = false;
    // rear right
    connectionPointCS0 = new Vector3f(-CUBE_HALF_EXTENT + (0.3f * wheelWidth), connectionHeight, -2 * CUBE_HALF_EXTENT + wheelRadius);
    vehicle.addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, vehicleTuning, !isFrontWheel);
    // rear left
    connectionPointCS0 = new Vector3f(CUBE_HALF_EXTENT - (0.3f * wheelWidth), connectionHeight, -2 * CUBE_HALF_EXTENT + wheelRadius);
    vehicle.addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, vehicleTuning, !isFrontWheel);

    for (int i = 0; i < vehicle.getNumWheels(); i++) {
        WheelInfo wheel = vehicle.getWheelInfo(i);
        wheel.suspensionStiffness = suspensionStiffness;
        wheel.wheelsDampingRelaxation = suspensionDamping;
        wheel.wheelsDampingCompression = suspensionCompression;
        wheel.frictionSlip = wheelFriction;
        wheel.rollInfluence = rollInfluence;
    }

    fallMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(-1, 10, 0), 0f)));
    float mass = 1f;
    Vector3f fallInertia = new Vector3f(0, 0, 0);
    fallShape = new BoxShape(new Vector3f(1, 1, 1));
    fallShape.calculateLocalInertia(mass, fallInertia);
    RigidBodyConstructionInfo fallRigidBodyCI = new RigidBodyConstructionInfo(mass, fallMotionState, fallShape, fallInertia);
    fallRigidBody = new RigidBody(fallRigidBodyCI);
    dynamicsWorld.addRigidBody(fallRigidBody);

I want to find angle rotation of the RaycastVehicle in this situation.

It will effetivly move the body which is the “model” to the location of the vehicle, but i dont know what to put in the rotate method to set the rotation of the body. Is there any way i could compute an angle from the vehicles RigidBody?

The ‘RigidBody’-class has a method called ‘getOpenGLMatrix’ (or similar). The method returns a model-space matrix for the RigidBody in your scene.
This matrix contains both translation and rotation.


(:pseudocode:)
projectionMatrix = ???; // projection
viewMatrix = ???; // camera
modelMatrix = getOpenGLMatrix(); // model

Have a nice day!

  • Longor1996