gpt4 book ai didi

java - jBullet相机 "Holder"不动

转载 作者:行者123 更新时间:2023-12-01 04:28:51 26 4
gpt4 key购买 nike

我正在使用 jBullet 和 OpenGL 来创建一个基本的游戏引擎。我创建了两个名为 ObjectSurfaceObjectEntity 的类。基本上,ObjectSurface 没有质量,因此它们不会移动,而 ObjectEntity 可以通过碰撞或重力移动。

我使用名为 cameraHolder 的 ObjectEntity 对象创建了一个相机,以便它可以掉落并发生碰撞等。

问题在于,当按下特定的 WASD 键时,cameraHolder 应该会移动,但它只移动一个单位,并且不会移动别再动了。释放按钮后,它会向相反方向移动一个单位,以便您位于同一位置。在 cameraHolder 移动后,相机的 XYZ 将设置为 cameraHolderXYZ 坐标。

但是,该运动适用于由于重力而下落的 jBullet 引起的运动。

当我不使用 jBullet 更新时,cameraHolder 会正常移动。所以问题应该出在 jBullet 更新上。 我将在代码中展示这一点。

问题:为什么按下特定的 WASD 按钮时,cameraHolder 仅移动 1 个单位然后又返回?

以下是 Camera.java 类中的运动代码:

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

以下是ObjectEntity的创建方式:

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

以下是物理世界的创建方式:

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

如何获取输入和更新物理(这部分中有可疑代码):

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

最佳答案

我认为比仅仅使用盒子形状更好的原因是拥有一个运动体。要将相机机身变成“运动体”(最好是单向碰撞,由用户移动),请调用以下命令:

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

关于java - jBullet相机 "Holder"不动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18191957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com