1

I'm working on a small FPS (personal project), written in java and using JBullet (port of bullet). I got a basic rendering engine with OpenGL and bullet works well. I've implemented a character using a RigidBody, but my character keeps going through every object, except the ground. here is a demo of what happens

This is my first project involving bullet, but I didn't find anyone having this strange behavior on google (nor StackExchange).

Here is a summary of my code.

Main class:

public class Fps {
    public static void main(String[] args) {
        //Initialization stuff
        // Loading level
        // Creating DiscreteDynamiWorld
        player = new Player(0, -50, 0);
        //Mainloop: 
        while(!Display.isCloseRequested()) {
             //GL stuff
             //InputHandleR.poll(player);
             update();
             render();
             //GL and timing stuff
        }
    }

    public static void update() {
        world.stepSimulation((float) delta / 1000f, 10);
        for(int i = 0; i < objects.size(); i++)
            objects.get(i).update();
    }

    public static void render() {
        // stuff
    }
}

Player:

public class Player implements tk.azertyfun.fps.objects.Object {
    // Attributes...

    public Player(float x, float y, float z) {
        c = new Camera(x, y, z); // Objects that has pos, yaw, pitch, and calls glRotate and glTranslate each frame

        //Creating RigidBody with a CapsuleShape (using BoxShape doesn't change anything)
        shape = new CapsuleShape(1.75f * CHAR_SCALE, 1.75f * CHAR_SCALE);
        Transform trans = new Transform(new Matrix4f(new Quat4f(1, 0, 0, 0), new javax.vecmath.Vector3f(x, y, z), 1f));
        motionState = new DefaultMotionState(trans);
        javax.vecmath.Vector3f inertia = new javax.vecmath.Vector3f(0, 0, 0);
        shape.calculateLocalInertia(1, inertia);
        RigidBodyConstructionInfo bodyRbCi = new RigidBodyConstructionInfo(1, motionState, shape, inertia);
        bodyRb = new RigidBody(bodyRbCi);

        //We wan't our body to stay up
        bodyRb.setSleepingThresholds(0.0f, 0.0f); 
        bodyRb.setAngularFactor(0.0f);

        Fps.btWorld.addRigidBody(bodyRb); //Adding body to the world (already trying adding it in the main class, doesn't change anything).
        bodyRb.setGravity(new javax.vecmath.Vector3f(0, 50, 0)); //Strange behavior that seems linked to my bug ; this body doesn't act like others, and has reversed gravity.
    }

    // This function is called by InputHandler and just uses body.setLinearVelocity(vel);. X and Z velocity is reset each frame (i want my player reactive, not walking with soap instead of foots). Anyway, it doesn't change our bug if not reset.
    public void move(boolean left, boolean right, boolean forward, boolean backwards, boolean up, boolean down) {
        float velX = 0;
        float velY = 0;
        float velZ = 0;

        javax.vecmath.Vector3f vel = new javax.vecmath.Vector3f();
        bodyRb.getLinearVelocity(vel);
        velY = vel.y;

        double delta = Util.getDelta();

        if(forward) {
            velX -= SPEED * (float) (Math.sin(Math.toRadians(c.yaw))) * delta;
            velZ += SPEED * (float) (Math.cos(Math.toRadians(c.yaw))) * delta;
        }

        if(backwards) {
            velX += SPEED * (float) (Math.sin(Math.toRadians(c.yaw))) * delta;
            velZ -= SPEED * (float) (Math.cos(Math.toRadians(c.yaw))) * delta;
        }

        if(left) {
            velX -= SPEED * (float) (Math.sin(Math.toRadians(c.yaw - 90))) * delta;
            velZ += SPEED * (float) (Math.cos(Math.toRadians(c.yaw - 90))) * delta;
        }

        if(right) {
            velX += SPEED * (float) (Math.sin(Math.toRadians(c.yaw - 90))) * delta;
            velZ -= SPEED * (float) (Math.cos(Math.toRadians(c.yaw - 90))) * delta;
        }

        if(up)
            velY += SPEED * (float) delta / 7f;
        if(down)
            velY -= SPEED * (float) delta / 7f;

        javax.vecmath.Vector3f velocity = new javax.vecmath.Vector3f(velX, velY, velZ);
        bodyRb.setLinearVelocity(velocity);
    }

    //Called each frame, sets the camera pos to the body pos.
    @Override
    public void update() {
        Transform trans = new Transform();
        bodyRb.getMotionState().getWorldTransform(trans);
        c.pos.set(trans.origin.x, trans.origin.y, trans.origin.z);
    }
}

Why isn't my body concerned by physics like the other ones? I have to revert the gravity for it and the only colliding object is the ground (which may be as buggy as the player). It looks like I didn't understand something.

Brian
  • 14,610
  • 7
  • 35
  • 43
Azertyfun
  • 49
  • 1
  • 5
  • do you mean you don't detect any collision between bodyRb and the other objects? Have you tried positioning bodyRb as to intersect other objects and then calling contactTest()? – Nic Oct 28 '15 at 11:19

0 Answers0