Code: Select all
IrrlichtDevice *device =
createDevice(video::EDT_DIRECTX8, core::dimension2d<s32>(640, 480), 16, false);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("../../media/cube.3ds");
scene::ISceneNode* q3node = 0;
if (q3levelmesh)
q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));
/*
So far so good, we've loaded the quake 3 level like in tutorial 2. Now, here
comes something different: We create a triangle selector. A triangle selector
is a class which can fetch the triangles from scene nodes for doing different
things with them, for example collision detection. There are different triangle
selectors, and all can be created with the ISceneManager. In this example,
we create an OctTreeTriangleSelector, which optimizes the triangle output a l
little bit by reducing it like an octree. This is very useful for huge meshes
like quake 3 levels.
Afte we created the triangle selector, we attach it to the q3node. This is not
necessary, but in this way, we do not need to care for the selector, for example
dropping it after we do not need it anymore.
*/
scene::ITriangleSelector* selector = 0;
if (q3node)
{
q3node->setPosition(core::vector3df(0,100,50));
selector = smgr->createOctTreeTriangleSelector(q3levelmesh->getMesh(0), q3node, 128);
q3node->setTriangleSelector(selector);
selector->drop();
}
/*
We add a first person shooter camera to the scene for being able to move in the quake 3
level like in tutorial 2. But this, time, we add a special animator to the
camera: A Collision Response animator. This thing modifies the scene node to which
it is attached to in that way, that it may no more move through walls and is affected
by gravity. The only thing we have to tell the animator is how the world looks like,
how big the scene node is, how gravity and so on. After the collision response animator
is attached to the camera, we do not have to do anything more for collision detection,
anything is done automaticly, all other collision detection code below is for picking.
And please note another cool feature: The collsion response animator can be attached
also to all other scene nodes, not only to cameras. And it can be mixed with other
scene node animators. In this way, collision detection and response in the Irrlicht
engine is really, really easy.
Now we'll take a closer look on the parameters of createCollisionResponseAnimator().
The first parameter is the TriangleSelector, which specifies how the world, against
collision detection is done looks like. The second parameter is the scene node, which
is the object, which is affected by collision detection, in our case it is the camera.
The third defines how big the object is, it is the radius of an ellipsoid. Try it out
and change the radius to smaller values, the camera will be able to move closer to walls
after this. The next parameter is the direction and speed of gravity. You could
set it to (0,0,0) to disable gravity. The value following after this defines the
acceleration value when falling down. And the last value is just a translation: Without
this, the ellipsoid with which collision detection is done would be around the camera,
and the camera would be in the middle of the ellipsoid. But as human beings, we are
used to have our eyes on top of the body, with which we collide with our world, not
in the middle of it. So we place the scene node 50 units over the center of the
ellipsoid with this parameter. And that's it, collision detection works now.
*/
scene::ICameraSceneNode* camera =
smgr->addCameraSceneNodeFPS(0,100.0f,300.0f);
camera->setPosition(core::vector3df(0,100,0));
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(30,50,30),
core::vector3df(0,0,0), 100.0f,
core::vector3df(0,50,0));
camera->addAnimator(anim);
anim->drop();