2012-10-17 4 views
5

मैं सीखने की कोशिश कर रहा हूं कि जिस परियोजना पर मैं काम कर रहा हूं उसमें जेबलेट का उपयोग कैसे किया जाए और मैंने उस डेमो की समीक्षा की है जो स्रोत प्रदान करता है लेकिन मैं यह नहीं समझ सकता कि इन डेमो वस्तुओं को कैसे प्रदर्शित करते हैं। क्या किसी के पास कोई अच्छा संसाधन है, वे मुझे इंगित कर सकते हैं या एक मूल उदाहरण प्रदान कर सकते हैं जो स्क्रीन पर एक या दो ऑब्जेक्ट प्रदर्शित करता है?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

मैं लाइब्रेरी के साथ शामिल डेमो को देख रहा था, मुझे खेद है, लेकिन उन्होंने उन्हें लिंक नहीं किया है। मैं कोड पोस्ट करूंगा लेकिन इसे कई क्लास फाइलों में विभाजित किया जाएगा, और यहां पर बोझिल होगा।मैं बुलेट ट्यूटोरियल देख रहा हूं, लेकिन वे वास्तव में जेबलेट के लिए अच्छी तरह से लागू नहीं होते हैं क्योंकि फ़ंक्शन नाम अलग हैं: http://bulletphysics.org/mediawiki-1.5.8/index.php/Hello_World – Kenneth

+0

यदि आप बात कर रहे हैं स्क्रीन पर भौतिकी डीबग जानकारी प्रदर्शित करने के बारे में आपको अपनी 'आईडीबग ड्रा' कक्षा को लागू करने की आवश्यकता होगी। यदि आप उन वस्तुओं को चित्रित करने के बारे में बात कर रहे हैं जिन्हें आप भौतिकी द्वारा नियंत्रित करना चाहते हैं, तो आप भौतिकी लाइब्रेरी के बिना भी इसमें शामिल होते हैं। जेबलेट का उपयोग सिर्फ अपनी वस्तुओं के भौतिकी को नियंत्रित करने के लिए किया जाना चाहिए, यानी उन्हें चारों ओर ले जाना और एक-दूसरे के साथ टकराने देना चाहिए। अपने मॉडल या वस्तुओं को चित्रित करना आपके कोड द्वारा किया जाना चाहिए। यदि आप परिभाषित करते हैं कि आप क्या करने की कोशिश कर रहे हैं, तो मैं इसे कैसे करना है इसका उत्तर दूंगा। – 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 डेमो और नमूना कोड की जांच की है?

इनमें से कुछ उपयोग जेबलेट को भौतिकी इंजन के रूप में उपयोग करते हैं, निश्चित रूप से इसके साथ खेलने लायक है।

+0

मैंने jMonkeyEngine के साथ कुछ चीजों को देखा है, लेकिन यह पता लगाने में कठिनाई है कि वास्तव में मेरे लिए क्या लागू होता है क्योंकि मैं अपने गेम को लिखने के लिए एलडब्ल्यूजेजीएल का उपयोग कर रहा हूं। वास्तव में यदि मैं भौतिक विज्ञान को घन या गेंद में जोड़ने के लिए जेबलेट का उपयोग करके किसी का मूल उदाहरण देख सकता हूं, और फिर उस घन या गेंद को प्रदर्शित करना मैं ठोस होगा। मेरी समस्या यह है कि जब मैंने इसे लिखने की कोशिश की है तो कुछ भी प्रदर्शित नहीं होता है, इसलिए यह देखना उपयोगी होगा कि किसी और ने इसे कैसे किया है। मुझे लगता है कि मुझे कुछ टेस्ट कोड लिखना चाहिए और इसे पोस्ट करना चाहिए ताकि मुझे बताया जा सके कि मैं क्या गलत कर रहा हूं, मुझे पता है कि मुझे पहले स्थान पर क्या करना चाहिए था। – Kenneth

+0

हालांकि, मैं वास्तव में आपके इनपुट की सराहना करता हूं, हालांकि, मैं जेएमई सामान को देखने के लिए जारी रखने जा रहा हूं यह देखने के लिए कि क्या मैं यह समझ सकता हूं कि वे जेएमई विशिष्ट सामग्री को कैसे कम करते हैं। – 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

इससे बहुत सारे बाइट 56 में मदद मिली, मुझे अब आउटपुट मिला और मुझे लगता है कि मैं समझता हूं कि दो दुनिया बनाकर आपका क्या मतलब है। मैं वास्तव में डर गया था कि मुझे बहुत सारे कोड दोबारा लिखना होगा, लेकिन आपकी व्याख्या मुझे सोचती है कि मुझे ऐसा नहीं करना पड़ेगा! वादा करता हूं कि जैसे ही मुझे चरित्र निर्माण का हिस्सा मिल जाए, आपको मेरी सामग्री का परीक्षण करने के लिए पहली बार पहुंच प्राप्त होगी। – Kenneth

+0

कोड त्रुटि नहीं दे रहा है, बस काम नहीं कर रहा है क्योंकि मुझे उम्मीद है कि जब मैं इसे अपने वास्तविक गेम में ले जाऊंगा। परीक्षण कोड हालांकि महान काम करता है। – Kenneth

संबंधित मुद्दे