如何解决Bullet Physcis中的旋转问题?

时间:2019-04-04 14:30:26

标签: java opengl game-engine game-physics bulletphysics

我目前正在尝试向我的LWJGL引擎添加物理学。 我使用JBullet库。 一切正常,可以与其他对象碰撞并反弹,但不会旋转。我进行了很多搜索,但找不到任何解决方案。 这是我的代码片段:

public void initPhysics() {

    broadphase = new DbvtBroadphase();
    collisionConfiguration = new DefaultCollisionConfiguration();
    dispatcher = new CollisionDispatcher(collisionConfiguration);
    solver = new SequentialImpulseConstraintSolver();
    dynamicsWorld = new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
    dynamicsWorld.setGravity(new Vector3f(0, -50, 0));


    CollisionShape groundShape = new StaticPlaneShape(new Vector3f(0, 1, 0), 1);
    DefaultMotionState groundMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(0, -1, 0), 1.0f))); 
    groundShape.calculateLocalInertia(0, new Vector3f(0,0,0)); 
    Vector3f groundInertia = new Vector3f(0f, 0f, 0f);
    RigidBodyConstructionInfo groundRigidBodyCI = new RigidBodyConstructionInfo(0, groundMotionState, groundShape, groundInertia); 
    groundRigidBodyCI.restitution = .25f;
    groundRigidBody = new RigidBody(groundRigidBodyCI); 
    dynamicsWorld.addRigidBody(groundRigidBody);
    rigidBodies.add(groundRigidBody);


    CollisionShape fallShape = new BoxShape(new Vector3f(3,3,3));
    DefaultMotionState fallMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(-830, 50,-80), 1.0f)));
    Vector3f fallShapeInertia = new Vector3f(0f, 0f, 0f);
    fallShape.calculateLocalInertia(1, fallShapeInertia); // mass ve inertia
    RigidBodyConstructionInfo fallRigidBodyCI = new RigidBodyConstructionInfo(1, fallMotionState, fallShape, fallShapeInertia); 
    fallRigidBodyCI.restitution = .25f;
    fallRigidBody = new RigidBody(fallRigidBodyCI); 
    dynamicsWorld.addRigidBody(fallRigidBody);
    rigidBodies.add(fallRigidBody);


    CollisionShape fallShape2 = new BoxShape(new Vector3f(3,3,3));
    DefaultMotionState fallMotionState2 = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(-834, 70,-80), 1.0f)));
    Vector3f fallShape2Inertia = new Vector3f(0f, 0f, 0f);
    fallShape2.calculateLocalInertia(1, fallShape2Inertia); // mass ve inertia
    RigidBodyConstructionInfo fallRigidBodyCI2 = new RigidBodyConstructionInfo(1, fallMotionState2, fallShape2, fallShape2Inertia); 
    fallRigidBodyCI2.restitution = .25f;
    fallRigidBody2 = new RigidBody(fallRigidBodyCI2); 
    dynamicsWorld.addRigidBody(fallRigidBody2);
    rigidBodies.add(fallRigidBody2);


    CollisionShape player = new BoxShape(new Vector3f(3,3,3));
    DefaultMotionState playerMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(-834, 0, -60), 1.0f)));
    Vector3f playerShapeInertia = new Vector3f(0f, 0f, 0f);
    player.calculateLocalInertia(1, playerShapeInertia); // mass ve inertia
    RigidBodyConstructionInfo playerRigidBodyCI = new RigidBodyConstructionInfo(1, playerMotionState, player, playerShapeInertia); 
    playerRigidBodyCI.restitution = .25f;
    playerRigidBody = new RigidBody(playerRigidBodyCI); 
    dynamicsWorld.addRigidBody(playerRigidBody);
    rigidBodies.add(playerRigidBody);


    System.out.println("Fizik objeleri: " + dynamicsWorld.getNumCollisionObjects()); // addObject fonksiyonu saymıyor!

}

public void addObject(Entity entity) 
{

    Quat4f outRot = new Quat4f();
    Vector3f entityPosition = fallRigidBody.getWorldTransform(new Transform()).origin;
    Quat4f entityRotation   = fallRigidBody.getWorldTransform(new Transform()).getRotation(outRot);
    entityRotation.w = 1;
    entity.setPositionn(entityPosition.x, entityPosition.y, entityPosition.z);
    entity.setRotation (entityRotation.x, entityRotation.y, entityRotation.z);

}

public void addObject2(Entity entity) 
{

    Quat4f outRot = new Quat4f();
    Vector3f entityPosition = fallRigidBody2.getWorldTransform(new Transform()).origin;
    Quat4f entityRotation   = fallRigidBody2.getWorldTransform(new Transform()).getRotation(outRot);
    entityRotation.w = 1;
    entity.setPositionn(entityPosition.x, entityPosition.y, entityPosition.z);
    entity.setRotation (entityRotation.x, entityRotation.y, entityRotation.z);

}

public void addPlayerObject(Entity entity) 
{

    Vector3f playerPosition = playerRigidBody.getWorldTransform(new Transform()).origin;
    playerPosition.x = entity.getPosition().x;
    playerPosition.y = entity.getPosition().y;
    playerPosition.z = entity.getPosition().z;

}

public void updatePhysics() 
{

    dynamicsWorld.stepSimulation(1/60.f, 10); 

}

public void cleanUpPhysics() 
{

    rigidBodies.clear();

}

顺便说一句,我的addPlayerObject()函数不起作用... 我仍然可以像鬼一样穿过物理对象。有什么问题?

0 个答案:

没有答案