身体穿过其他物体

时间:2014-03-15 22:11:18

标签: java physics bulletphysics jbullet

我正在开发一个小型FPS(个人项目),用java编写并使用JBullet(子弹端口)。我有一个使用OpenGL的基本渲染引擎,子弹效果很好。我使用RigidBody实现了一个角色,但是我的角色不断穿过每个物体,除了地面。这是一个demo of what happens

这是我的第一个涉及子弹的项目,但我没有发现任何人在谷歌(也不是StackExchange)上有这种奇怪的行为。

以下是我code的摘要。

主要课程:

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
    }
}

播放器:

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);
    }
}

为什么我的身体不像其他人一样关注物理?我必须恢复它的重力,唯一的碰撞对象是地面(可能像玩家一样有缺陷)。看起来我不明白。

0 个答案:

没有答案
相关问题