2012-10-17 46 views
5

Tôi đang cố gắng tìm hiểu cách sử dụng jBullet trong một dự án mà tôi đang làm việc và tôi đã xem xét bản demo mà nguồn cung cấp nhưng tôi không thể tìm ra cách các bản demo này hiển thị đối tượng. Có ai có một nguồn lực tốt họ có thể chỉ cho tôi hoặc cung cấp một ví dụ cơ bản hiển thị một hoặc hai đối tượng trên màn hình?Ví dụ jBullet

Cảm ơn bạn trước, tôi xin lỗi tôi không có bất kỳ mã nào để hiển thị tôi có thể nhanh chóng viết lên một số nếu cần nhưng chỉ thực sự tìm kiếm hướng đi.

Cảm ơn bạn,

Mã cho Cube mà tôi đang sử dụng, vì vậy tôi đang cố gắng để thêm va chạm với nó, nhưng tôi không chắc chắn cách sử dụng 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(); 
} 

Đây là mã chuyển đổi của tôi từ mã kiểm tra hello world, điều này có đúng với mọi người không? :

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

lẽ bạn có thể liên kết đến các bản demo bạn đang tìm kiếm tại? – theJollySin

+0

Tôi đang xem các bản trình diễn có trong thư viện, tôi xin lỗi nhưng họ không liên kết chúng với nhau. Tôi sẽ đăng mã nhưng nó được chia thành nhiều tập tin lớp, và sẽ là cồng kềnh để làm ở đây.Tôi đã xem qua các hướng dẫn viên đạn, nhưng chúng thực sự không áp dụng tốt cho jBullet vì tên hàm là khác nhau: http://bulletphysics.org/mediawiki-1.5.8/index.php/Hello_World – Kenneth

+0

Nếu bạn đang nói về việc hiển thị thông tin gỡ lỗi vật lý trên màn hình, bạn sẽ cần phải triển khai lớp 'IDebugDraw' của riêng bạn. Nếu bạn đang nói về việc vẽ các đối tượng mà bạn muốn được kiểm soát bởi vật lý, bạn làm điều đó mà không có thư viện vật lý nào có liên quan gì cả. jBullet chỉ nên được sử dụng để kiểm soát vật lý của đối tượng của bạn, tức là di chuyển chúng xung quanh và va chạm với nhau. Vẽ các mô hình hoặc đối tượng của bạn phải được thực hiện bằng mã của riêng bạn. Nếu bạn xác định những gì bạn đang cố gắng làm tôi sẽ cung cấp một câu trả lời về cách làm điều đó. – MichaelHouse

Trả lời

4

Ví dụ mã cho jBullet HelloWorld:

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

Bạn đã xem các mã demo và mẫu jMonkeyEngine chưa?

Khá một vài trong số này sử dụng jBullet làm công cụ vật lý, chắc chắn đáng để chơi cùng.

+0

Tôi đã xem xét một vài điều với jMonkeyEngine, nhưng thật khó để tìm ra những gì thực sự áp dụng cho tôi vì tôi đang sử dụng LWJGL để viết trò chơi của mình. Thực sự nếu tôi có thể nhìn thấy một ví dụ cơ bản của một người sử dụng jBullet để thêm vật lý vào một khối lập phương hoặc bóng, và sau đó hiển thị khối lập phương hoặc quả bóng đó tôi sẽ chắc chắn. Vấn đề của tôi là khi tôi đã cố gắng tự viết điều này, không có gì hiển thị, vì vậy sẽ hữu ích khi xem người khác đã làm như thế nào. Tôi đoán tôi nên viết một số mã kiểm tra và đăng nó để tôi có thể nói với những gì tôi đang làm sai, tôi biết đó là những gì tôi nên làm ngay từ đầu. – Kenneth

+0

Tôi thực sự đánh giá cao đầu vào của bạn được cung cấp mặc dù, tôi sẽ tiếp tục nhìn vào công cụ JME để xem nếu tôi có thể tìm ra cách họ làm điều đó trừ đi những thứ cụ thể JME. – Kenneth

1

Cho phép xem mã ví dụ từ hướng dẫn bạn đang làm việc. Tôi đã thêm nhận xét vào mã để bạn có thể hiểu rõ hơn những gì đang diễn ra và cách bạn nên thiết lập mã của mình. Điều quan trọng cần lưu ý là mã bên dưới không thực sự hiển thị bất kỳ thứ gì. Về cơ bản nó chỉ tạo ra một vật thể vật lý, một mặt đất và cho phép vật thể rơi xuống đất, đưa ra chiều cao của vật thể khi nó bước qua mô phỏng.

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

Về cơ bản bạn muốn tạo lại thế giới trò chơi của mình bên trong mô phỏng vật lý. Sau đó, khi bạn bước mô phỏng vật lý, bạn cập nhật thế giới trò chơi của mình với các vị trí mới từ mô phỏng. Mô phỏng vật lý đang cho bạn biết cách di chuyển các đối tượng trò chơi của bạn để nó xuất hiện như thể chúng đang va chạm với nhau.

Vì vậy, để thiết lập, bạn sẽ di chuyển nội dung trong vòng lặp for vào vòng lặp cập nhật của mình. Sau đó, thay vì viết vị trí hình cầu ra khỏi bảng điều khiển, bạn cập nhật posX, posY, posZ với vị trí của hình cầu. Bây giờ khối lập phương của bạn đang di chuyển giống như quả cầu trong mô phỏng!

Vì vậy, chỉ cần đẩy điểm cuối cùng. Bạn đang tạo ra hai thế giới. Một trong những nơi bạn đang vẽ đồ họa chi tiết và một nơi bạn có hình dạng đơn giản đại diện cho các hình dạng vật lý của các đối tượng đồ họa chi tiết của bạn. Thế giới vật lý đang mô phỏng tương tác của tất cả các đối tượng của bạn, và các đối tượng đồ họa chi tiết chỉ phản chiếu vị trí của các hình dạng vật lý đơn giản này.

Hy vọng rằng mọi thứ sẽ rõ ràng hơn.

+0

Điều đó đã giúp ích rất nhiều cho Byte56, tôi đã nhận được kết quả ngay bây giờ và tôi nghĩ rằng tôi hiểu ý của bạn bằng cách tạo ra hai thế giới. Tôi đã thực sự sợ tôi sẽ phải viết lại rất nhiều mã, nhưng lời giải thích của bạn làm cho tôi nghĩ rằng tôi sẽ không phải là tuyệt vời! Hứa hẹn bạn sẽ có quyền truy cập đầu tiên để kiểm tra nội dung của tôi ngay khi tôi hoàn thành phần tạo nhân vật. – Kenneth

+0

mã không bị lỗi, không hoạt động như tôi mong đợi khi tôi chuyển nó sang trò chơi thực tế của mình. Mã thử nghiệm hoạt động tốt mặc dù. – Kenneth