我正在尝试学习如何在我正在工作的项目中使用jBullet,我已经查看了源代码提供的演示,但我就是不明白这些演示get对象是如何显示的。有没有人有很好的资源可以给我指点,或者提供一个在屏幕上显示一两个对象的基本示例?
提前感谢你,我很抱歉我没有任何代码来显示我可以快速写一些,如果需要的话,但只是真的寻找一个方向。
谢谢,
我正在使用的Cube的代码,所以我试图向它添加碰撞,但我不确定如何使用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();
}这是我从hello world测试代码转换而来的代码,这对每个人来说都是正确的吗?:
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);
}
}发布于 2012-10-22 01:36:08
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);
}}
发布于 2012-10-17 13:39:30
你看过jMonkeyEngine演示和示例代码了吗?
其中有相当一部分使用jBullet作为物理引擎,绝对值得一试。
发布于 2012-10-19 12:59:31
让我们看一下您正在使用的教程中的示例代码。我在代码中添加了注释,这样您就可以更好地了解发生了什么以及应该如何设置代码。需要注意的是,下面的代码实际上不会显示任何内容。它基本上只是创建了一个物理对象,一个地面,并让该对象落到地面上,输出该对象在模拟过程中的高度。
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,而不是将球体位置写出到控制台。现在,立方体的移动与模拟中的球体一样!
因此,最后再来推进一下要点。你在创造两个世界。其中一种是绘制详细的图形,另一种是使用简单的形状来表示详细图形对象的物理形状。物理世界正在模拟所有对象的交互,而细节图形对象只是反映这些简单物理形状的位置。
希望这能让事情变得更清楚。
https://stackoverflow.com/questions/12927654
复制相似问题