This manual is copyrighted and exclusively for private use for Irrlicht forum users and Irrlicht wiki users. any redistribution of this manual without express consent is Illegal.
NOTE: Some users have been having problems with the event reciever. see This Post for a fix.
NOTE: I Tried to fix the video and driver thingy.. but apparently its still in some snippets, see This Post for a fix.
NOTE: The second collision function(bounding box) appears to be broken, see This Post for a fix.
Here it is:
Irrlicht basics tutorial.
This tutorial assumes that you have a basic knowlage of C++,(objects, pointers, etc) and maybe
have written a game before using some 2D library like SDL or Allegro.
List of chapters:
- Chapter 0: Installing
- Chapter 1: Basics and init
- Chapter 2: Creating scene nodes
- Chapter 3: Keypress detection
- Chapter 4: Cameras
- Chapter 5: Collision
- Chapter 6: Advanced scene nodes
- Final Chapter
==Chapter 0==
----
You need to install irrlicht, ofcourse!
----
To install irrlicht, open the folder you downloaded from http://irrlicht.sf.net/download.html
and click "lib", copy either the win32 or linux files to "C:\Dev-Cpp\lib\" or "/usr/lib"
(or anything else, if you use VC++ or something), next, do the same with the "include" folder,
copy to "c:\dev-cpp\include\" or "/usr/include".
Now, to compile your program, you'll have to link with the Irrlicht libraries.
on Linux/g++ this is simple: -lIrrlicht -lGL -lGLU
on Win32, you'll want to go to the trouble of doing:
Code: Select all
#pragma comment(lib, Irrlicht.lib);
you will also need to
Code: Select all
#include <irrlicht.h>
==Chapter 1==
----
In this chapter, you will how to start irrlicht up and get a blue
screen to open up without anything rendering on it.
----
First of all, you need to create the basic irrlicht stuff.
Ofcourse, the namespaces:
Code: Select all
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace gui;
Code: Select all
IrrlichtDevice* device = createDevice(EDT_OPENGL, core::dimension2d<s32>(640,480));
just change EDT_OPENGL to EDT_DIRECT3D9. A List of irrlicht devices:
Code: Select all
OpenGL - EDT_OPENGL
DirectX 8 - EDT_DIRECT3D8
DirectX 9 - EDT_DIRECT3D9
Software - EDT_SOFTWARE
Sofware 2 - EDT_BURNINGSVIDEO
other managers from it.
Code: Select all
IVideoDriver* video = device->getVideoDriver();
ever need to use this, but it's still good to have it there incase you need it later.
Code: Select all
ISceneManager* smgr = device->getSceneManager();
you will handle irrlicht from here on out, from creating a cube to adding complex animators.
Now that you have your device and managers set up, it's time for a simple main function!
in your main function, you'll have a while loop. this loop will render the irrlicht scene.
Code: Select all
while(device->run() && device)
now, it's time to add some stuff to inside that while loop.
Code: Select all
video->beginScene(true, true, video::SColor(255,0,0,255));
smgr->drawAll();
video->endScene();
smgr->drawAll() will draw everything you've added to the scene manager(we'll take another look at
this in chapter 2)
Well, that concludes chapter 1! So far you've learned to initalize irrlicht and render a
blank window!
Here's a simple demo program. we will take this program, and modify
it through this tutorial/book until it is a fully-functional game
Code: Select all
int main() {
IrrlichtDevice *device = createDevice(EDT_OPENGL);
IVideoDriver* video = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
while(device->run() && device) {
video->beginScene(true, true, video::SColor(255,0,0,255));
smgr->drawAll();
video->endScene();
}
}
==Chapter 2==
----
In this chapter, you'll learn how to
add scene nodes, both meshes and cubes
and spheres and everything else to
the irrlicht scene!
----
Now that irrlicht is setup, you can add cubes, or even 3d models to your scene.
before the 'while' loop, add this line:
Code: Select all
smgr->addCubeSceneNode();
to see the cube, because both the camera and the cube are at the same position. so change that
line to:
Code: Select all
ISceneNode* cube = smgr->addCubeSceneNode();
now, we will want to move it forward so we can see it. if you know a little bit of 3-D, you'll know
that X is left/right, Y is up/down, and Z is forward/backward.
Code: Select all
cube->setPosition(vector3df(0,0,5));
Code: Select all
cube->setPosition(vector3df(X, Y, Z));
that line right below the adding of the cube and re-compile and re-run the program. you should
see a cube. try experimenting with the position of the cube, and familiarise yourself with
irrlicht's position system.
ISceneNode's have 2 other basic functions:
Code: Select all
setPosition()
setScale()
setRotation()
Code: Select all
cube->setScale(vector3df(25,1,1));
mess around with scale, position, and rotation until you fully understand irrlicht's coordinate
system!!
-- Take a break at this point, mess with your program, walk around, etc --
Now, i assume that you know and understand irrlicht's basic movement functions. theres another
way to create scene nodes:
Code: Select all
smgr->addAnimatedMeshSceneNode((const c8*)"myfile.3ds");
not char array pointers, so you need to cast it.
addAnimatedMeshSceneNode returns a ISceneNode pointer, just like addCubeSceneNode. you can do the
same things to a cube as you can to a mesh. Irrlicht can load .x, .3ds, .lwo, .mesh, and a few
other 3d formats. some models will be too big/small for irrlicht, so you'll need to setScale them
to a reasonable size.
Irrlicht scene nodes also have 3 basic functions:
Code: Select all
getPosition()
getRotation()
getScale()
a scene node forward(for example, if someone pressed the 'W' or UP key), you might do:
Code: Select all
myNode->setPosition(myNode->getPosition+vector3df(0,0,5));
to access the X/Y/Z variables in a vector3df, you just do:
Code: Select all
myVector3df.X = 10;
with this. try different things.
Well, looks like you've learned how to load meshes, or create cubes, and move them around, resize
them, and rotate them.
Our simple game now:
Code: Select all
int main() {
IrrlichtDevice *device = createDevice(ETD_OPENGL);
IVideoDriver* video = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
ISceneNode* cube = smgr->addCubeSceneNode();
cube->setPosition(vector3df(0,0,5));
while(device->run() && device) {
video->beginScene(true, true, video::SColor(255,0,0,255));
smgr->drawAll();
video->endScene();
}
}
==Chapter 3==
----
This isn't really a chapter but
a code-snippet disguised as a
chapter you'll learn how to
get key input here
----
Put this code at the top of your program:
Code: Select all
bool keys[irr::KEY_KEY_CODES_COUNT];
Code: Select all
class MyEventReceiver : public IEventReceiver {
public:
bool OnEvent(SEvent event) {
if(event.EventType == irr::EET_KEY_INPUT_EVENT){
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
return false;
}
};
Code: Select all
MyEventReceiver rv;
device->setEventReceiver(&rv);
Code: Select all
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false;
and that should do it. from here on out, you can use:
Code: Select all
if(keys[KEY_KEY_W]) {
// do stuff
}
KEY_KEY_[that key]. for other keys, check the irrlicht documentation
so far, our code is:
Code: Select all
bool keys[irr::KEY_KEY_CODES_COUNT];
class MyEventReceiver : public IEventReceiver {
public:
bool OnEvent(SEvent event) {
if(event.EventType == irr::EET_KEY_INPUT_EVENT){
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
return false;
}
};
int main() {
IrrlichtDevice *device = createDevice(ETD_OPENGL);
IVideoDriver* video = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
MyEventReceiver rv;
device->setEventReceiver(&rv);
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false;
ISceneNode* cube = smgr->addCubeSceneNode();
cube->setPosition(vector3df(0,0,5));
while(device->run() && device) {
if(keys[KEY_KEY_W]) {
cube->setPosition(cube->getPosition()+vector3df(0,0,5));
}
video->beginScene(true, true, video::SColor(255,0,0,255));
smgr->drawAll();
video->endScene();
}
}
look at that snippet, and try to make the W/A/S/D keys move your cube around.
==Chapter 4==
----
In this chapter, you'll learn what a camera
is and how to use one.
----
So far, you've setup irrlicht, made a cube, and you can move it around with the W,A,S,D keys.
Not a bad start
but now, you'll want to learn about cameras. cameras are the viewpoint in which you see everything
in your game. before now, you've been using the static camera at 0,0,0 that looks to 0,0,900.
but now, you're going to create your own camera!
Code: Select all
ICameraSceneNode* cam = smgr->addCameraSceneNode();
the same way as a normal scene node, except you can't scale or rotate it. try to move it around,
see how the viewpoint changes! there is another function of interest here:
Code: Select all
cam->setTarget(cube->getPosition());
look at that position. try it out, maybe make it so when you press a key, the camera looks to
the node!
-- Stop here and try some things out. take a break. walk around. etc. --
Back? Great!
there's another camera type i'd like to introduce you to. the FPS Camera. this camera behaves
exactly like one in a first-person shooter would. the mouse looks around, and the arrow keys
move according to where you're looking. FPS cameras can be rotated, unlike normal cameras.
Code: Select all
cam = smgr->addCameraSceneNodeFPS();
Let's take another look at our game:
Code: Select all
bool keys[irr::KEY_KEY_CODES_COUNT];
class MyEventReceiver : public IEventReceiver {
public:
bool OnEvent(SEvent event) {
if(event.EventType == irr::EET_KEY_INPUT_EVENT){
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
return false;
}
};
int main() {
IrrlichtDevice *device = createDevice(ETD_OPENGL);
IVideoDriver* video = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
MyEventReceiver rv;
device->setEventReceiver(&rv);
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false;
ISceneNode* cube = smgr->addCubeSceneNode();
cube->setPosition(vector3df(0,0,5));
ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS();
while(device->run() && device) {
if(keys[KEY_KEY_W]) {
cube->setPosition(cube->getPosition()+vector3df(0,0,5));
}
video->beginScene(true, true, video::SColor(255,0,0,255));
smgr->drawAll();
video->endScene();
}
}
camera-moving adventure!
==Chapter 5==
----
Oh, no! another code-snippet chapter!
Here you will learn how to do
basic collision detection between nodes,
without having to bother with Triangle
Selectors!
---
This is a REALLY short chapter! Here is a function i use for REALLY
simple collision detection, which isn't pixel-perfect, but good enough
for checking if a bullet hit an enemy or checking if the player hit a powerup,
etc, etc.
Code: Select all
bool collision(ISceneNode* one, ISceneNode* two, int size) {
if(one->getAbsolutePosition().getDistanceFrom(two->getAbsolutePosition()) < size)
return true;
return false;
}
That function basically checks if the distance of the nodes is less than size.
to use this, you might do:
Code: Select all
if(collision(cube,sphere,100)) {
std::cout<<"Cube hit sphere!"<<std::endl;
}
See if you can move the cube around and make it print "Cube hit sphere!" when they collide.
Cool, eh?
or, another more "accurate" collision function:
Code: Select all
bool collision(ISceneNode* one, ISceneNode* two) {
if(one->getBoundingBox().intersectsWithBox(two->getBoundingBox())) {
return true;
return false;
}
And the "game":
Code: Select all
bool keys[irr::KEY_KEY_CODES_COUNT];
class MyEventReceiver : public IEventReceiver {
public:
bool OnEvent(SEvent event) {
if(event.EventType == irr::EET_KEY_INPUT_EVENT){
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
return false;
}
};
int main() {
IrrlichtDevice *device = createDevice(ETD_OPENGL);
IVideoDriver* video = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
MyEventReceiver rv;
device->setEventReceiver(&rv);
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false;
ISceneNode* cube = smgr->addCubeSceneNode();
cube->setPosition(vector3df(0,0,5));
ISceneNode* sphere = smgr->addSphereSceneNode();
sphere->setPosition(vector3df(0,0,300));
ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS();
while(device->run() && device) {
if(keys[KEY_KEY_W]) {
cube->setPosition(cube->getPosition()+vector3df(0,0,5));
}
if(collision(cube,sphere,100)) {
std::cout<<"Collided!"<<std::endl;
}
video->beginScene(true, true, video::SColor(255,0,0,255));
smgr->drawAll();
video->endScene();
}
}
==Chapter 6==
----
Here, you'll learn how to attach
nodes to other nodes, animate nodes,
etc.
----
Irrlicht supports frame-based animation. i assume you know that frames in model animation
are like a flipbook. each page would be a frame. setting the frame loop is used for, say, a door:
pages/frames 1-15 is opening
pages/frames 16-30 is closing
frames 31-60 is being destroyed
we obviously dont want to loop through all of those, so we set the frame loop like so:
Code: Select all
myNode->setFrameLoop(1,15);
set the speed. basically:
Code: Select all
myNode->setAnimationSpeed(25);
ofcourse, this will only work on actual meshes, not cubes or spheres, so i can't really
add anything to our example
now that you know about node animation, what about attaching?
Code: Select all
myNode->setParent(cam);
myNode is a child of cam, if myNode moves, cam won't.
Another thing you will use all the time in a game is a skybox. it gives the appearence that
your game is in the sky, in space, etc. a skybox is basically a box of images around the camera.
Code: Select all
ISceneNode* SkyBox = smgr->addSkyBoxSceneNode(
driver->getTexture("Top image"),
driver->getTexture("Bottom image"),
driver->getTexture("Forward image"),
driver->getTexture("Backward image"),
driver->getTexture("Left image"),
driver->getTexture("Right Image.bmp"));
Let's attach the sphere to the camera and add a skybox to our game:
Code: Select all
bool keys[irr::KEY_KEY_CODES_COUNT];
class MyEventReceiver : public IEventReceiver {
public:
bool OnEvent(SEvent event) {
if(event.EventType == irr::EET_KEY_INPUT_EVENT){
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
return false;
}
};
int main() {
IrrlichtDevice *device = createDevice(ETD_OPENGL);
IVideoDriver* video = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
MyEventReceiver rv;
device->setEventReceiver(&rv);
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false;
ISceneNode* cube = smgr->addCubeSceneNode();
cube->setPosition(vector3df(0,0,5));
ISceneNode* sphere = smgr->addSphereSceneNode();
sphere->setPosition(vector3df(0,0,300));
ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS();
sphere->setParent(cam);
ISceneNode* SkyBox = smgr->addSkyBoxSceneNode(
driver->getTexture("background.bmp"),
driver->getTexture("background.bmp"),
driver->getTexture("background.bmp"),
driver->getTexture("background.bmp"),
driver->getTexture("background.bmp"),
driver->getTexture("background.bmp"));
while(device->run() && device) {
if(keys[KEY_KEY_W]) {
cube->setPosition(cube->getPosition()+vector3df(0,0,5));
}
if(collision(cube,sphere,100)) {
std::cout<<"Collided!"<<std::endl;
}
video-->beginScene(true, true, video::SColor(255,0,0,255));
smgr->drawAll();
video-->endScene();
}
}
==Final Chapter==
Well, a single guide can't cover everything in game development, but i hope this guide
got you started with irrlicht.
Check out the docs and tutorials for info on GUIs and such:
Code: Select all
http://irrlicht.sf.net/docu/
http://irrlicht.sf.net/tutorials.html
Code: Select all
if(enemy->getPosition().X > player->getPosition().X)
enemy->setPosition(enemy->getPosition() + 5);
like ice trying to hit you
Well, bye for now
Code: Select all
bool keys[irr::KEY_KEY_CODES_COUNT];
class MyEventReceiver : public IEventReceiver {
public:
bool OnEvent(SEvent event) {
if(event.EventType == irr::EET_KEY_INPUT_EVENT){
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
return false;
}
};
int main() {
// Init irrlicht
IrrlichtDevice *device = createDevice(ETD_OPENGL);
IVideoDriver* video = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
// Setup event reciever
MyEventReceiver rv;
device->setEventReceiver(&rv);
// Init keystates array
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false;
// Add a cube
ISceneNode* cube = smgr->addCubeSceneNode();
cube->setPosition(vector3df(0,0,5));
// Add a sphere
ISceneNode* sphere = smgr->addSphereSceneNode();
sphere->setPosition(vector3df(0,0,300));
// Add a camera
ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS();
// Attach sphere to camera
sphere->setParent(cam);
// Add a skybox
ISceneNode* SkyBox = smgr->addSkyBoxSceneNode(
driver->getTexture("background.bmp"),
driver->getTexture("background.bmp"),
driver->getTexture("background.bmp"),
driver->getTexture("background.bmp"),
driver->getTexture("background.bmp"),
driver->getTexture("background.bmp"));
// Main game loop
while(device->run() && device) {
// Cube movement
if(keys[KEY_KEY_W]) {
cube->setPosition(cube->getPosition()+vector3df(0,0,5));
}
// Cube/Sphere collision detection
if(collision(cube,sphere,100)) {
std::cout<<"Collided!"<<std::endl;
}
// Irrlicht rendering
video->beginScene(true, true, video::SColor(255,0,0,255));
smgr->drawAll();
video->endScene();
}
}