We will describe 2 methods: Automatic collision detection for moving through 3d worlds with stair climbing and sliding, and manual scene node and triangle picking using a ray. In this case, we will use a ray coming out from the camera, but you can use any ray.
To start, we take the program from tutorial 2, which loads and displays a quake 3 level. We will use the level to walk in it and to pick triangles from. In addition we'll place 3 animated models into it for triangle picking. The following code starts up the engine and loads the level, as per tutorial 2.
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
enum
{
ID_IsNotPickable = 0,
IDFlag_IsPickable = 1 << 0,
IDFlag_IsHighlightable = 1 << 1
};
int main()
{
if (driverType==video::EDT_COUNT)
return 1;
if (device == 0)
return 1;
if (q3levelmesh)
The Irrlicht device. You can create it with createDevice() or createDeviceEx().
virtual scene::ISceneManager * getSceneManager()=0
Provides access to the scene manager.
virtual video::IVideoDriver * getVideoDriver()=0
Provides access to the video driver for drawing 3d and 2d geometry.
virtual io::IFileSystem * getFileSystem()=0
Provides access to the virtual file system.
Axis aligned bounding box in 3d dimensional space.
virtual bool addFileArchive(const path &filename, bool ignoreCase=true, bool ignorePaths=true, E_FILE_ARCHIVE_TYPE archiveType=EFAT_UNKNOWN, const core::stringc &password="", IFileArchive **retArchive=0)=0
Adds an archive to the file system.
Interface for an animated mesh.
virtual IMesh * getMesh(s32 frame, s32 detailLevel=255, s32 startFrameLoop=-1, s32 endFrameLoop=-1)=0
Returns the IMesh interface for a frame.
A scene node displaying a static mesh.
The Scene Manager manages scene nodes, mesh recources, cameras and all the other stuff.
virtual IAnimatedMesh * getMesh(const io::path &filename)=0
Get pointer to an animateable mesh. Loads the file if not loaded already.
virtual IMeshSceneNode * addOctreeSceneNode(IAnimatedMesh *mesh, ISceneNode *parent=0, s32 id=-1, s32 minimalPolysPerNode=512, bool alsoAddIfMeshPointerZero=false)=0
Adds a scene node for rendering using a octree to the scene graph.
Interface to driver which is able to perform 2d and 3d graphics functions.
Main header file of the irrlicht, the only file needed to include.
E_DRIVER_TYPE
An enum for all types of drivers the Irrlicht Engine supports.
Everything in the Irrlicht Engine can be found in this namespace.
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 OctreeTriangleSelector, which optimizes the triangle output a little bit by reducing it like an octree. This is very useful for huge meshes like quake 3 levels. After 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.
if (q3node)
{
}
virtual IMesh * getMesh(void)=0
Get the currently defined mesh for display.
virtual ITriangleSelector * createOctreeTriangleSelector(IMesh *mesh, ISceneNode *node, s32 minimalPolysPerNode=32)=0
Creates a Triangle Selector, optimized by an octree.
virtual void setPosition(const core::vector3df &newpos)
Sets the position of the node relative to its parent.
virtual void setTriangleSelector(ITriangleSelector *selector)
Sets the triangle selector of the scene node.
Interface to return triangles with specific properties.
We add a first person shooter camera to the scene so that we can see and 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 animator modifies the scene node to which it is attached to in order to prevent it moving through walls, and to add gravity to it. The only thing we have to tell the animator is how the world looks like, how big the scene node is, how much gravity to apply 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 automatically. The rest of the collision detection code below is for picking. And please note another cool feature: The collision 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 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. We'll set it to (0, -10, 0), which approximates to realistic gravity, assuming that our units are metres. You could set it to (0,0,0) to disable gravity. 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.
if (selector)
{
}
bill->
setID(ID_IsNotPickable);
bool drop() const
Drops the object. Decrements the reference counter by one.
virtual gui::ICursorControl * getCursorControl()=0
Provides access to the cursor control.
virtual void setVisible(bool visible)=0
Changes the visible state of the mouse cursor.
virtual void setSize(const core::dimension2d< f32 > &size)=0
Sets the size of the billboard, making it rectangular.
Scene Node which is a (controlable) camera.
virtual void setTarget(const core::vector3df &pos)=0
Sets the look at target of the camera.
virtual ISceneNodeAnimatorCollisionResponse * createCollisionResponseAnimator(ITriangleSelector *world, ISceneNode *sceneNode, const core::vector3df &ellipsoidRadius=core::vector3df(30, 60, 30), const core::vector3df &gravityPerSecond=core::vector3df(0,-10.0f, 0), const core::vector3df &ellipsoidTranslation=core::vector3df(0, 0, 0), f32 slidingValue=0.0005f)=0
Creates a special scene node animator for doing automatic collision detection and response.
virtual ICameraSceneNode * addCameraSceneNodeFPS(ISceneNode *parent=0, f32 rotateSpeed=100.0f, f32 moveSpeed=0.5f, s32 id=-1, SKeyMap *keyMapArray=0, s32 keyMapSize=0, bool noVerticalMovement=false, f32 jumpSpeed=0.f, bool invertMouse=false, bool makeActive=true)=0
Adds a camera scene node with an animator which provides mouse and keyboard control appropriate for f...
virtual IBillboardSceneNode * addBillboardSceneNode(ISceneNode *parent=0, const core::dimension2d< f32 > &size=core::dimension2d< f32 >(10.0f, 10.0f), const core::vector3df &position=core::vector3df(0, 0, 0), s32 id=-1, video::SColor colorTop=0xFFFFFFFF, video::SColor colorBottom=0xFFFFFFFF)=0
Adds a billboard scene node to the scene graph.
Animates a scene node. Can animate position, rotation, material, and so on.
void setMaterialTexture(u32 textureLayer, video::ITexture *texture)
Sets the texture of the specified layer in all materials of this scene node to the new texture.
virtual void addAnimator(ISceneNodeAnimator *animator)
Adds an animator which should animate this node.
void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
Sets all material flags at once to a new value.
void setMaterialType(video::E_MATERIAL_TYPE newType)
Sets the material type of all materials in this scene node to a new material type.
virtual void setID(s32 id)
Sets the id of the scene node.
virtual ITexture * getTexture(const io::path &filename)=0
Get access to a named texture.
Add 3 animated hominids, which we can pick using a ray-triangle intersection. They all animate quite slowly, to make it easier to see that accurate triangle selection is being performed.
0, IDFlag_IsPickable | IDFlag_IsHighlightable);
0, IDFlag_IsPickable | IDFlag_IsHighlightable);
0, IDFlag_IsPickable | IDFlag_IsHighlightable);
0, IDFlag_IsPickable | IDFlag_IsHighlightable);
light->
setID(ID_IsNotPickable);
int lastFPS = -1;
{
if (highlightedSceneNode)
{
highlightedSceneNode = 0;
}
ray,
intersection,
hitTriangle,
IDFlag_IsPickable,
0);
if(selectedSceneNode)
{
if((selectedSceneNode->
getID() & IDFlag_IsHighlightable) == IDFlag_IsHighlightable)
{
highlightedSceneNode = selectedSceneNode;
}
}
if (lastFPS != fps)
{
core::stringw str = L
"Collision detection example - Irrlicht Engine [";
str += "] FPS:";
str += fps;
lastFPS = fps;
}
}
return 0;
}
virtual bool run()=0
Runs the device.
virtual void setWindowCaption(const wchar_t *text)=0
Sets the caption of the window.
virtual bool isWindowActive() const =0
Returns if the window is active.
4x4 matrix. Mostly used as transformation matrix for 3d calculations.
vector3d< T > start
Start point of line.
vector3d< T > end
End point of line.
Scene node capable of displaying an animated mesh and its shadow.
virtual void setAnimationSpeed(f32 framesPerSecond)=0
Sets the speed with which the animation is played.
virtual bool setMD2Animation(EMD2_ANIMATION_TYPE anim)=0
Starts a default MD2 animation.
virtual const core::vector3df & getTarget() const =0
Gets the current look at target of the camera.
Scene node which is a dynamic light.
The Scene Collision Manager provides methods for performing collision tests and picking on scene node...
virtual ISceneNode * getSceneNodeAndCollisionPointFromRay(core::line3df ray, core::vector3df &outCollisionPoint, core::triangle3df &outTriangle, s32 idBitMask=0, ISceneNode *collisionRootNode=0, bool noDebugObjects=false)=0
Perform a ray/box and ray/triangle collision check on a heirarchy of scene nodes.
virtual ITriangleSelector * createTriangleSelector(IMesh *mesh, ISceneNode *node)=0
Creates a simple ITriangleSelector, based on a mesh.
virtual void drawAll()=0
Draws all the scene nodes.
virtual ISceneCollisionManager * getSceneCollisionManager()=0
Get pointer to the scene collision manager.
virtual IAnimatedMeshSceneNode * addAnimatedMeshSceneNode(IAnimatedMesh *mesh, ISceneNode *parent=0, s32 id=-1, const core::vector3df &position=core::vector3df(0, 0, 0), const core::vector3df &rotation=core::vector3df(0, 0, 0), const core::vector3df &scale=core::vector3df(1.0f, 1.0f, 1.0f), bool alsoAddIfMeshPointerZero=false)=0
Adds a scene node for rendering an animated mesh model.
virtual ILightSceneNode * addLightSceneNode(ISceneNode *parent=0, const core::vector3df &position=core::vector3df(0, 0, 0), video::SColorf color=video::SColorf(1.0f, 1.0f, 1.0f), f32 radius=100.0f, s32 id=-1)=0
Adds a dynamic light scene node to the scene graph.
virtual s32 getID() const
Get the id of the scene node.
virtual void setScale(const core::vector3df &scale)
Sets the relative scale of the scene node.
virtual video::SMaterial & getMaterial(u32 num)
Returns the material based on the zero based index i.
virtual const core::vector3df & getPosition() const
Gets the position of the node relative to its parent.
virtual void setRotation(const core::vector3df &rotation)
Sets the rotation of the node relative to its parent.
virtual bool beginScene(bool backBuffer=true, bool zBuffer=true, SColor color=SColor(255, 0, 0, 0), const SExposedVideoData &videoData=SExposedVideoData(), core::rect< s32 > *sourceRect=0)=0
Applications must call this method before performing any rendering.
virtual s32 getFPS() const =0
Returns current frames per second value.
virtual const wchar_t * getName() const =0
Gets name of this video driver.
virtual bool endScene()=0
Presents the rendered image to the screen.
virtual void setMaterial(const SMaterial &material)=0
Sets a material.
virtual void setTransform(E_TRANSFORMATION_STATE state, const core::matrix4 &mat)=0
Sets transformation matrices.
virtual void draw3DTriangle(const core::triangle3df &triangle, SColor color=SColor(255, 255, 255, 255))=0
Draws a 3d triangle.
Class representing a 32 bit ARGB color.
Class representing a color with four floats.
Struct for holding parameters for a material renderer.
void setTexture(u32 i, ITexture *tex)
Sets the i-th texture.
bool Wireframe
Draw as wireframe or filled triangles? Default: false.
bool NormalizeNormals
Should normals be normalized?
bool Lighting
Will this material be lighted? Default: true.