jBullet Camera "Holder" Is Not Moving

203 views Asked by At

I am using jBullet with OpenGL to create a basic game engine. I created two classes named ObjectSurface and ObjectEntity. Basically ObjectSurface's have no mass so they don't move and ObjectEntity's can move by collision or gravity.

I created a Camera with an ObjectEntity object named cameraHolder so that it can fall and have collision and all.

The problem is that the cameraHolder is supposed to move when the specific WASD key is pressed but it only moves one unit and doesn't move anymore. When the button is released, it moves one unit to the opposite direction so that you are at the same location. And after the cameraHolder moves the Camera's XYZ are set to the XYZ coordinates of the cameraHolder.

However, the movement works for the movement caused by jBullet which is falling down because of the gravity.

And when I don't use the jBullet updates, the cameraHolder moves normally. So the problem should be with the jBullet updates. I will be showing that in the code.

Question: Why is the cameraHolder only moving 1 unit and then back when the specific WASD buttons are pressed?

Here is the code for the movement in the Camera.java class:

public void move(float amount, float direction) {

if(!still){
    if (direction == MOVE_BACKWARD) {
        //MOVE BACKWARDS (CODE NOT NEEDED, WORKS WITHOUT CAMERA CARRIER)
        }

        if (direction == MOVE_FORWARD) {
        //MOVE FORWARD
        }

        if (direction == MOVE_LEFT) {
        //MOVE LEFT
        }

        if (direction == MOVE_RIGHT) {
        //MOVE RIGHT
        }
    }
}

public void carryCamera() {
    if(!still){
    x = cameraHolder.pos.x;
    y = cameraHolder.pos.y;
    z = cameraHolder.pos.z;
    }
}

Here is how an ObjectEntity is created:

private void createShape() {
    DefaultMotionState fallMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(pos.x, pos.y, pos.z), 1.0f)));
    Vector3f fallInertia = new Vector3f(0,0,0); 
    shape.calculateLocalInertia(mass,fallInertia); 
    RigidBodyConstructionInfo fallRigidBodyCI = new RigidBodyConstructionInfo(mass,fallMotionState,shape,fallInertia); 
    body = new RigidBody(fallRigidBodyCI); 
    Engine.getDynamicsWorld().addRigidBody(body); 
}

Here is how the Physics World is created:

private static void initPhysics() {
    BroadphaseInterface broadphase = new DbvtBroadphase();
    DefaultCollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
    CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);
    SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver();
    dynamicsWorld = new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
    dynamicsWorld.setGravity(Game.gravity);
}

How the input is gotten and physics are updated (SUSPICIOUS CODE IS IN THIS PART):

public static Vector3f gravity = new Vector3f(0, -10, 0); //GRAVITY
private ArrayList<Object> objects; //ALL THE OBJECTS IN THE GAME (CAMERAHOLDER IS IN HERE)

        //GETTING INPUT, USING move() METHOD IN CAMERA CLASS TO MOVE HOLDER.
public void getInput() {

    if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL))
        speedMultiplier = 3;

    if (Keyboard.isKeyDown(Keyboard.KEY_W))
        cam.move(cam.getMoveSpeed() * speedMultiplier, Camera.MOVE_FORWARD);

    if (Keyboard.isKeyDown(Keyboard.KEY_S))
        cam.move(cam.getMoveSpeed() * speedMultiplier, Camera.MOVE_BACKWARD);

    if (Keyboard.isKeyDown(Keyboard.KEY_A))
        cam.move(cam.getMoveSpeed() * speedMultiplier, Camera.MOVE_LEFT);

    if (Keyboard.isKeyDown(Keyboard.KEY_D))
        cam.move(cam.getMoveSpeed() * speedMultiplier, Camera.MOVE_RIGHT);

    cam.carryCamera(); //MOVES THE ACTUAL CAMERA TO THE HOLDER.
    speedMultiplier = 1;

}

public void update() {
    applyJBullet();
}

        //AND JBULLET PHYSICS ARE APPLIED HERE (SUSPICIOUS CODE!!!)
public void applyJBullet(){
Engine.getDynamicsWorld().stepSimulation(1/60.f, 10); //SIMULATES NEXT FRAME

    for (Object o : objects) { 
        if(o.getMass() >= 1){ //ALL THE OBJECTS WITH MASS ARE FOUND

    Transform trans = new Transform();   //CREATING A NEW TRANSFORM
    o.getBody().getMotionState().getWorldTransform(trans); //GET THE SIMULATED LOCATION

//It still doesn't work when I do: "trans.origin.x+=2" here. The same problem occurs.
    o.setPos(trans.origin); //AND SET IT AS THE OBJECTS POSITION
        }   
    }
}
1

There are 1 answers

0
Jaspreet On

I think a better why than just using a box shape is to have a kinematic body. To turn the camera body into a 'kinematic body' (preferable one-way collision, moved by user), call the following:

body.setCollisionFlags(body.ge­tCollisionFlags() | CollisionFlags.KINEMATIC_OBJEC­T);
body.setActivationState(Collis­ionObject.DISABLE_DEACTIVATION­);