Как исправить проблему с вращением в Bullet Physcis? - PullRequest
0 голосов
/ 04 апреля 2019

Я сейчас пытаюсь добавить физику в мой движок 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 () не работает ... Я все еще могу проходить физические объекты как призрак.В чем проблема?

...