gpt4 book ai didi

java - 如何在 jBullet 中创建运动对象?

转载 作者:行者123 更新时间:2023-12-02 06:47:49 26 4
gpt4 key购买 nike

我正在尝试制作一个游戏引擎,我想要一个由玩家控制的相机,并且还受到Java中其他jBullet实体的影响。有人建议我使用运动学对象,所以我查找了它们。我找不到任何我能理解的文档。

有人可以解释如何设置和使用运动对象,或者至少告诉我可以从哪里开始吗?

最佳答案

KinematicCharacterController 的文档,已找到 here并不是很有帮助,但是 CharacterDemo 中的源代码可以。演示中定义了两个主要属性。

    public KinematicCharacterController character;
public PairCachingGhostObject ghostObject;

幽灵可用于动态碰撞检测,因为它不会自动对这些事件使用react。可以通过改变变换来移动角色。

    //from the source src\com\bulletphysics\demos\character\CharacterDemo.java
Transform startTransform = new Transform();
startTransform.setIdentity();
startTransform.origin.set(0.0f, 4.0f, 0.0f);

Vector3f worldMin = new Vector3f(-1000f,-1000f,-1000f);
Vector3f worldMax = new Vector3f(1000f,1000f,1000f);
AxisSweep3 sweepBP = new AxisSweep3(worldMin, worldMax);

ghostObject = new PairCachingGhostObject();
ghostObject.setWorldTransform(startTransform);
sweepBP.getOverlappingPairCache().setInternalGhostPairCallback(new GhostPairCallback());
float characterHeight = 1.75f * characterScale;
float characterWidth = 1.75f * characterScale;
ConvexShape capsule = new CapsuleShape(characterWidth, characterHeight);
ghostObject.setCollisionShape(capsule);
ghostObject.setCollisionFlags(CollisionFlags.CHARACTER_OBJECT);

float stepHeight = 0.35f * characterScale;
character = new KinematicCharacterController(ghostObject, capsule, stepHeight);


dynamicsWorld.addCollisionObject(ghostObject, CollisionFilterGroups.CHARACTER_FILTER, (short)(CollisionFilterGroups.STATIC_FILTER | CollisionFilterGroups.DEFAULT_FILTER));

dynamicsWorld.addAction(character);

扩展 MotionState 类来保存变换也是明智的

   public class MyMotionState extends MotionState {
private Transform worldTransform;
public MyMotionState()
{
worldTransform = new Transform();
worldTransform.setIdentity();
}

@Override
public Transform getWorldTransform(Transform worldTrans)
{
worldTrans.set(worldTransform);
return worldTrans;
}

@Override
public void setWorldTransform(Transform worldTrans)
{
worldTransform.set(worldTrans);
}
}

并将运动学链接到 RigidBody,以便将物理应用到角色,并获取渲染信息。

    rigidBody.setCollisionFlags(rigidBody.getCollisionFlags() | CollisionFlags.KINEMATIC_OBJECT);  
rigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);

不要忘记在游戏循环的每次迭代中更新物理引擎。

    Transform transform = new Transform();
transform.setIdentity();
transform.origin.set(input.getX(), input.getY(), input.getZ());
myMotionState.setWorldTransform(transform);
rigidBody.setCenterOfMassTransform(myMotionState.getWorldTransform());

如果您愿意,您可以将它们放入 MainCharacter 类中或任何您所称的名称中(我喜欢它的面向对象的感觉并且易于理解)

    public class MainCharacter implements KeyListener, MouseListener
{
private DynamicsWorld world;
private MyMotionState myMotionState;
private RigidBody rigidBody;
private KinematicCharacterController character;
private ConvexShape shape;
private Texture texture;
private GhostObject ghost;
private Vector3f pos;
public MainCharacter(DynamicsWorld world, Vector3f initialPosition, ConvexShape shape, Texture texture)
{
this.world = world;
RigidBodyConstructionInfo constructInfo = new RigidBodyConstructionInfo(...);
this.myMotionState = myMotionState;
rigidBody = new RigidBody(constructInfo);
ghost = new GhostObject();
character = new KinematicCharacterController(ghost,shape,1);
}
public void render()
{
glBegin(GL_QUADS);
glVertex3f(...
...
glEnd();
}
public void mouseMoved(MouseEvent e)
{
//pseudocode
this.yaw = e.getDX();
this.pitch = e.getDY();
}
public void keyPressed(KeyEvent e)
{
Vector3f dPos = null;
if(e.getKeyChar() == 'W')
{
dPos.x = 10;
}
else if(e.getKeyChar() == 'S')
{
dPos.x = -10;
}
etc...

move(dPos.x,dPos.y,dPos.z);
}
public void move(float dx, float dy, float dz) {
pos.z += dx * (float) Math.cos(Math.toRadians(yaw - 90)) + dz * Math.cos(Math.toRadians(yaw));
pos.x -= dx * (float) Math.sin(Math.toRadians(yaw - 90)) + dz * Math.sin(Math.toRadians(yaw));
pos.y += dy * (float) Math.sin(Math.toRadians(pitch - 90)) + dz * Math.sin(Math.toRadians(pitch));
//pseudocode
rigidBody.update(pos);
world.update(pos);
}
}

希望对您有所帮助。

关于java - 如何在 jBullet 中创建运动对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18405592/

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