Hi DawsonXB360.DawsonXB360 wrote:Cobra or silver.slade. Could either of you perhaps send me some source code showing how to load a .irr file and apply collision, I'm having a little trouble with it myself.
Here a little example (maybe I'll post here a complete source later):
Code: Select all
...
// Create the irrBullet world
world = createIrrBulletWorld(device, true, true);
world->setDebugMode(EPDM_DrawAabb | EPDM_DrawContactPoints);
world->setGravity(vector3df(0,-10,0));
// load irr file to scene
smgr->loadScene("Scene.irr");
// create metatriangleselector
scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
core::array<scene::ISceneNode *> nodes;
// gets all nodes
smgr->getSceneNodesFromType(scene::ESNT_ANY, nodes);
ICollisionShape *shape;
IRigidBody* terrain;
for (u32 i=0; i < nodes.size(); ++i)
{
scene::ISceneNode * node = nodes[i];
scene::ITriangleSelector * selector = 0;
switch(node->getType())
{
case scene::ESNT_CUBE:
case scene::ESNT_ANIMATED_MESH:
selector = smgr->createTriangleSelectorFromBoundingBox(node);
break;
case scene::ESNT_MESH:
case scene::ESNT_SPHERE:
// output for quick debug
printf("(SPHERE OR MESH)%s\n", node->getName());
selector = smgr->createTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
shape = new IBvhTriangleMeshShape(((scene::IMeshSceneNode*)node),
((scene::IMeshSceneNode*)node)->getMesh(),
0.0);
terrain = world->addRigidBody(shape);
terrain->setGravity(vector3df(0,0,0));
shape->setLocalScaling(node->getScale(), ESP_VISUAL);
terrain->setCollisionFlags(ECF_STATIC_OBJECT);
break;
case scene::ESNT_TERRAIN:
// output for quick debug
printf("(TERRAIN)%s\n", node->getName());
selector = smgr->createTerrainTriangleSelector((scene::ITerrainSceneNode*)node);
break;
case scene::ESNT_OCTREE:
// output for quick debug
printf("(OCTREE)%s\n", node->getName());
selector = smgr->createOctreeTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
break;
default:
// Don't create a selector for this node type
printf("%s, NO COLLISIONS!\n", node->getName());
break;
}
if(selector)
{
// used by the camera's collisions
meta->addTriangleSelector(selector);
selector->drop();
}
// the camera
if (strcmp(node->getName(),"Camera")==0)
{
printf("Found a camera...loading its parameters\n");
camera = smgr->addCameraSceneNodeFPS(0, 60.0f, 0.008f, -1, keyMap, 8, true, 0.5f);
camera->setFOV( ((scene::ICameraSceneNode*)node)->getFOV());
posizione = ((scene::ICameraSceneNode*)node)->getPosition();
camera->setPosition( posizione );
camera->setRotation( ((scene::ICameraSceneNode*)node)->getRotation());
camera->setScale(((scene::ICameraSceneNode*)node)->getScale());
camera->setFarValue(((scene::ICameraSceneNode*)node)->getFarValue());
camera->setAspectRatio(((scene::ICameraSceneNode*)node)->getAspectRatio());
camera->setTarget(((scene::ICameraSceneNode*)node)->getTarget());
camera->setUpVector(((scene::ICameraSceneNode*)node)->getUpVector());
camera->setFarValue(15000);
}
}
// camera not found, I'll create one
// usually a camera is always present in .irr file
if (camera==0){
printf("No Camera founded: we'll create one...\n");
camera = smgr->addCameraSceneNodeFPS(0, 40.0f, 0.008f, -1, keyMap, 8, true, 5.0f);
camera->setFOV(0.857);
camera->setPosition(vector3df(28.655060, 5.699696, -18.311363));
camera->setRotation(vector3df(27.098162, -46.688389, -0.903519));
camera->setScale(vector3df(1, 1, 1));
camera->setNearValue(0.10);
camera->setFarValue(100.0);
camera->setAspectRatio(1.25);
}
core::rect<s32> vp = driver->getViewPort();
camera->setAspectRatio((f32)vp.getWidth() / (f32)vp.getHeight());
// collisions with camera and meta
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
meta,
camera,
core::vector3df(1.5,posizione.Y,1.5),
core::vector3df(0,-0.5,0),
core::vector3df(0,0,0));
meta->drop();
camera->addAnimator(anim);
anim->drop();
u32 TimeStamp = device->getTimer()->getTime(), DeltaTime = 0;
while(device->run())
{
if (device->isWindowActive())
{
driver->beginScene(true, true, video::SColor(0,50,50,50));
DeltaTime = device->getTimer()->getTime() - TimeStamp;
TimeStamp = device->getTimer()->getTime();
// irrbullet related
world->stepSimulation(DeltaTime*0.001f, 120);
world->debugDrawWorld(true);
world->debugDrawProperties(true);
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
}
delete world;
device->drop();
return 0;
}