2012-10-17 2 views
5

저는 작업중인 프로젝트에서 jBullet을 사용하는 방법을 배우려고하고 있으며 소스가 제공하는 데모를 리뷰했지만이 데모가 오브젝트를 표시하는 방법을 이해할 수 없습니다. 누구든지 나를 가리킬 수있는 좋은 리소스가 있거나 화면에 하나 또는 두 개의 개체를 표시하는 기본 예제를 제공합니까?jBullet 예

미리 감사드립니다. 필요한 경우 신속하게 작성할 수있는 코드가 없지만 정말 갈 방향을 찾고있는 것은 미안합니다. jbullet 사용 방법

은 내가 사용하고있는 큐브,

코드를 주셔서 감사합니다, 그래서 나는 그것에 충돌을 추가하기 위해 노력하고 있어요,하지만 난 확실하지 오전 :

여기
public void Draw() { 
    // center point posX, posY, posZ 
    float radius = size/2; 

    //top 
    glPushMatrix(); 
    glBegin(GL_QUADS); 
     { 
      glColor3f(1.0f,0.0f,0.0f); // red 
      glVertex3f(posX + radius, posY + radius, posZ - radius); 
      glVertex3f(posX - radius, posY + radius, posZ - radius); 
      glVertex3f(posX - radius, posY + radius, posZ + radius); 
      glVertex3f(posX + radius, posY + radius, posZ + radius); 
     } 
    glEnd(); 
    glPopMatrix(); 

    //bottom 
    glPushMatrix(); 
    glBegin(GL_QUADS); 
     { 
      glColor3f(1.0f,1.0f,0.0f); // ?? color 
      glVertex3f(posX + radius, posY - radius, posZ + radius); 
      glVertex3f(posX - radius, posY - radius, posZ + radius); 
      glVertex3f(posX - radius, posY - radius, posZ - radius); 
      glVertex3f(posX + radius, posY - radius, posZ - radius); 
     } 
    glEnd(); 
    glPopMatrix(); 

    //right side 
    glPushMatrix(); 
    glBegin(GL_QUADS); 
     { 
      glColor3f(1.0f,0.0f,1.0f); // ?? color 
      glVertex3f(posX + radius, posY + radius, posZ + radius); 
      glVertex3f(posX + radius, posY - radius, posZ + radius); 
      glVertex3f(posX + radius, posY - radius, posZ - radius); 
      glVertex3f(posX + radius, posY + radius, posZ - radius); 
     } 
    glEnd(); 
    glPopMatrix(); 

    //left side 
    glPushMatrix(); 
    glBegin(GL_QUADS); 
     { 
      glColor3f(0.0f,1.0f,1.0f); // ?? color 
      glVertex3f(posX - radius, posY + radius, posZ - radius); 
      glVertex3f(posX - radius, posY - radius, posZ - radius); 
      glVertex3f(posX - radius, posY - radius, posZ + radius); 
      glVertex3f(posX - radius, posY + radius, posZ + radius); 
     } 
    glEnd(); 
    glPopMatrix(); 

    //front side 
    glPushMatrix(); 
    glBegin(GL_QUADS); 
     { 
      glColor3f(0.0f,0.0f,1.0f); //blue 
      glVertex3f(posX + radius, posY + radius, posZ + radius); 
      glVertex3f(posX - radius, posY + radius, posZ + radius); 
      glVertex3f(posX - radius, posY - radius, posZ + radius); 
      glVertex3f(posX + radius, posY - radius, posZ + radius); 
     } 
    glEnd(); 
    glPopMatrix(); 

    //back side 
    glPushMatrix(); 
    glBegin(GL_QUADS); 
     { 
      glColor3f(0.0f,1.0f,0.0f); // green 
      glVertex3f(posX + radius, posY - radius, posZ - radius); 
      glVertex3f(posX - radius, posY - radius, posZ - radius); 
      glVertex3f(posX - radius, posY + radius, posZ - radius); 
      glVertex3f(posX + radius, posY + radius, posZ - radius); 
     } 
    glEnd(); 
    glPopMatrix(); 
} 

내 변환 코드 안녕하세요 세계 테스트 코드에서 모든 사람에게이 모양이 맞습니까? :

public static void HelloWorld() { 

    BroadphaseInterface broadphase = new DbvtBroadphase(); 
    DefaultCollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration(); 
    CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration); 

    SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver(); 

    DiscreteDynamicsWorld dynamicsWorld = new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration); 

    // set the gravity of our world 
    dynamicsWorld.setGravity(new Vector3f(0, -10, 0)); 

    // setup our collision shapes 
    CollisionShape groundShape = new StaticPlaneShape(new Vector3f(0, 1, 0), 1); 
    CollisionShape fallShape = new SphereShape(1); 

    // setup the motion state 
    DefaultMotionState groundMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(0, -1, 0), 1.0f))); 

    RigidBodyConstructionInfo groundRigidBodyCI = new RigidBodyConstructionInfo(0, groundMotionState, groundShape, new Vector3f(0,0,0)); 
    RigidBody groundRigidBody = new RigidBody(groundRigidBodyCI); 

    dynamicsWorld.addRigidBody(groundRigidBody); // add our ground to the dynamic world.. 

    // setup the motion state for the ball 
    DefaultMotionState fallMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(0, 50, 0), 1.0f))); 

    //This we're going to give mass so it responds to gravity 
    int mass = 1; 

    Vector3f fallInertia = new Vector3f(0,0,0); 
    fallShape.calculateLocalInertia(mass,fallInertia); 

    RigidBodyConstructionInfo fallRigidBodyCI = new RigidBodyConstructionInfo(mass,fallMotionState,fallShape,fallInertia); 
    RigidBody fallRigidBody = new RigidBody(fallRigidBodyCI); 

    //now we add it to our physics simulation 
    dynamicsWorld.addRigidBody(fallRigidBody); 

    for (int i=0 ; i<300 ; i++) { 
     dynamicsWorld.stepSimulation(1/60.f, 10); 

     Transform trans = new Transform(); 
     fallRigidBody.getMotionState().getWorldTransform(trans); 


     System.out.println("sphere height: " + trans.origin.y); 
    } 

} 
+0

은 아마 당신이보고 된 데모에 링크 할 수 있습니까? – theJollySin

+0

도서관에 포함 된 데모를보고 있었지만 미안하지만 도서관과 연결되어 있지 않습니다. 코드를 게시 할 것이지만 여러 개의 클래스 파일로 분리되어 있으며 여기서 수행하는 것이 번거롭다.총알 자습서를 살펴 보았지만 함수 이름이 다르기 때문에 사실 jBullet에 잘 적용되지 않습니다. http://bulletphysics.org/mediawiki-1.5.8/index.php/Hello_World – Kenneth

+0

물리 디버그 정보를 화면에 표시하는 것에 대해서는 자신 만의'IDebugDraw' 클래스를 구현해야합니다. 물리학에 의해 제어되기를 원하는 객체를 그리는 것에 대해 이야기하고 있다면, 물리 라이브러리가 전혀 개입되지 않은 상태에서 그렇게 할 수 있습니다. jBullet은 객체의 물리학을 제어하는 ​​데 사용되어야합니다. 즉, 객체를 이동하고 서로 충돌하는 것입니다. 모델 또는 객체 그리기는 자신의 코드로 수행해야합니다. 당신이하고자하는 것을 정의한다면, 나는 그것을하는 방법에 대한 답을 제공 할 것입니다. – MichaelHouse

답변

4

예제 코드 :

public static void HelloWorld() { 

BroadphaseInterface broadphase = new DbvtBroadphase(); 
DefaultCollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration(); 
CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration); 

SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver(); 

DiscreteDynamicsWorld dynamicsWorld = new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration); 

// set the gravity of our world 
dynamicsWorld.setGravity(new Vector3f(0, -10, 0)); 

// setup our collision shapes 
CollisionShape groundShape = new StaticPlaneShape(new Vector3f(0, 1, 0), 1); 
CollisionShape fallShape = new SphereShape(1); 

// setup the motion state 
DefaultMotionState groundMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(0, -1, 0), 1.0f))); 

RigidBodyConstructionInfo groundRigidBodyCI = new RigidBodyConstructionInfo(0, groundMotionState, groundShape, new Vector3f(0,0,0)); 
RigidBody groundRigidBody = new RigidBody(groundRigidBodyCI); 

dynamicsWorld.addRigidBody(groundRigidBody); // add our ground to the dynamic world.. 

// setup the motion state for the ball 
DefaultMotionState fallMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(0, 50, 0), 1.0f))); 

//This we're going to give mass so it responds to gravity 
int mass = 1; 

Vector3f fallInertia = new Vector3f(0,0,0); 
fallShape.calculateLocalInertia(mass,fallInertia); 

RigidBodyConstructionInfo fallRigidBodyCI = new RigidBodyConstructionInfo(mass,fallMotionState,fallShape,fallInertia); 
RigidBody fallRigidBody = new RigidBody(fallRigidBodyCI); 

//now we add it to our physics simulation 
dynamicsWorld.addRigidBody(fallRigidBody); 

for (int i=0 ; i<300 ; i++) { 
    dynamicsWorld.stepSimulation(1/60.f, 10); 

    Transform trans = new Transform(); 
    fallRigidBody.getMotionState().getWorldTransform(trans); 


    System.out.println("sphere height: " + trans.origin.y); 
} 

}

1

jMonkeyEngine 데모 및 샘플 코드를 확인 했습니까?

이들 중 상당수는 jBullet을 물리 엔진으로 사용하고 있습니다.

+0

jMonkeyEngine으로 몇 가지를 살펴 봤지만 LWJGL을 사용하여 게임을 작성한 이후 실제로 적용되는 것을 파악하기가 어렵습니다. 실제로 jBullet을 사용하여 큐브 나 공에 물리학을 추가 한 다음 큐브 나 공을 표시하는 사람의 기본 예를 볼 수 있다면 나는 견고 할 것입니다. 내 문제는 내가 직접이 글을 쓰려고 시도한 것이므로 누군가 다른 사람이 어떻게했는지 알면 도움이 될 것입니다. 테스트 코드를 작성하고 게시하여 내가 잘못한 것을 말할 수 있도록해야한다고 생각합니다. 처음에해야 할 일이 무엇인지 알았습니다. – Kenneth

+0

나는 귀하의 의견을 진심으로 감사드립니다. JME 자료를보고 계속해서 JME 특정 자료를 빼는 방법을 알아낼 수 있는지 알아 보겠습니다. – Kenneth

1

작업중인 튜토리얼의 예제 코드를 살펴 보겠습니다. 코드에 주석을 추가하여 어떤 일이 벌어지고 있는지, 그리고 코드를 어떻게 설정해야하는지 더 잘 알려줄 수 있습니다. 아래 코드는 실제로 아무것도 표시하지 않는다는 점에 유의해야합니다. 기본적으로 피직스 오브젝트와지면을 생성하고 오브젝트가지면에 떨어지도록하여 시뮬레이션을 통해 단계별로 오브젝트의 높이를 출력합니다.

int main (void) 
{ 
    //Set up all the required objects and controllers for simulating the physics 
    //all this stuff would actually go into whatever initialize function you have 
    btBroadphaseInterface* broadphase = new btDbvtBroadphase(); 

    btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); 
    btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); 

    btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver; 

    btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration); 

    dynamicsWorld->setGravity(btVector3(0,-10,0)); 

    //Create our physics objects, the planeShape is the ground 
    btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1); 

    //A sphere that will be dropping to the ground, 
    btCollisionShape* fallShape = new btSphereShape(1); 

    //Create motion states for our objects 
    //First the ground object. It will be in the XZ plane at -1 Y 
    //note that we're not giving it any mass 
    //zero mass in a physics simulation means it won't move when collided with 
    //it also means that it won't respond to gravity 
    btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0))); 
    btRigidBody::btRigidBodyConstructionInfo 
       groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0)); 
    btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI); 

    //Add the ground to the simulation 
    dynamicsWorld->addRigidBody(groundRigidBody); 

    //now set up the motion state for our sphere, we'll put it at 50 Y 
    btDefaultMotionState* fallMotionState = 
       new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0))); 
    //This we're going to give mass so it responds to gravity 
    btScalar mass = 1; 
    btVector3 fallInertia(0,0,0); 
    fallShape->calculateLocalInertia(mass,fallInertia); 
    btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,fallShape,fallInertia); 
    btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI); 

    //now we add it to our physics simulation 
    dynamicsWorld->addRigidBody(fallRigidBody); 


    //Here's where the magic happens. The physics simulation is stepped. 
    //for each step, we're going to get the balls current position and write it out. 
    //Everything inside this for loop would actually go into your *update* loop 
    //your update loop would step the physics simulation 
    //after stepping the simulation, you get the positions of your physics bodies 
    //and make sure your object positions match those. 

    for (int i=0 ; i<300 ; i++) { 
      dynamicsWorld->stepSimulation(1/60.f,10); 

      btTransform trans; 
      fallRigidBody->getMotionState()->getWorldTransform(trans); 

      //so you would take `trans` and use it to set the position of your cube 
      //then your cube position would be updated to the same position as 
      //this physics object that's representing it. 

      std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl; 
    } 

    //everything else is clean up 

    dynamicsWorld->removeRigidBody(fallRigidBody); 
    delete fallRigidBody->getMotionState(); 
    delete fallRigidBody; 

    dynamicsWorld->removeRigidBody(groundRigidBody); 
    delete groundRigidBody->getMotionState(); 
    delete groundRigidBody; 


    delete fallShape; 

    delete groundShape; 


    delete dynamicsWorld; 
    delete solver; 
    delete collisionConfiguration; 
    delete dispatcher; 
    delete broadphase; 

    return 0; 
} 

기본적으로 물리 시뮬레이션에서 게임 세계를 재현하고 싶습니다. 그런 다음 물리 시뮬레이션을 수행하면 시뮬레이션의 새로운 위치로 게임 세계를 업데이트합니다. 물리 시뮬레이션은 게임 개체가 서로 충돌하는 것처럼 보이도록 게임 개체를 이동하는 방법을 알려줍니다.

설정을 위해 for 루프의 내용을 업데이트 루프로 옮깁니다. 그러면 구체 위치를 콘솔에 쓰는 대신 posX, posY, posZ을 구체의 위치로 업데이트하십시오. 이제 큐브는 시뮬레이션에서 구와 똑같이 움직입니다!

마지막으로 요점을 마무리합니다. 당신은 두 개의 세계를 창조하고 있습니다. 상세한 그래픽을 그리는 곳과 상세한 그래픽 오브젝트의 실제 모양을 나타내는 간단한 모양이있는 곳. 물리 세계는 모든 객체의 상호 작용을 시뮬레이션하고 있으며, 세부 그래픽 객체는 이러한 단순한 물리적 모양의 위치를 ​​그대로 반영합니다.

잘하면 그게 더 명확 해집니다. jBullet하여 HelloWorld에 대한

+0

Byte56을 많이 돕는 결과를 얻었습니다. 두 세계를 만드는 것이 무엇을 의미하는지 이해할 수 있다고 생각합니다. 나는 실제로 많은 코드를 다시 써야 할 것이라고 두려워했지만, 당신의 설명을 통해 내가 끔찍한 생각을하지 않을 것입니다! 캐릭터 제작 부분을 완성하자마자 내 물건을 테스트 할 수있는 첫 번째 액세스 권한이 부여됩니다. – Kenneth

+0

코드가 오류를 발생시키지 않고 실제 게임으로 옮길 때 예상대로 작동하지 않습니다. 테스트 코드는 훌륭하게 작동합니다. – Kenneth