我正在开发一个小型 FPS(个人项目),用 java 编写并使用 JBullet(子弹端口)。我有一个带有 OpenGL 的基本渲染引擎,并且子弹效果很好。我已经使用 RigidBody 实现了一个角色,但我的角色一直在穿过除地面之外的所有物体。这是发生的事情的演示
这是我第一个涉及子弹的项目,但我没有发现任何人在谷歌(也不是 StackExchange)上有这种奇怪的行为。
这是我的代码的摘要。
主类:
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);
}
}
为什么我的身体不像其他人那样关注物理?我必须为它恢复重力,唯一的碰撞物体是地面(可能和玩家一样有问题)。好像没听懂什么。