0

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

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

4

1 回答 1

2

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

    public KinematicCharacterController character;
    public PairCachingGhostObject ghostObject;

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

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

并将 Kinematic 链接到 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);
        }
   }

我希望我对你有所帮助。

于 2013-08-29T05:47:54.167 回答