How to make a 3d rpg camera in 10 steps
Posted: Thu Jul 19, 2007 10:09 pm
Well hi guys i saw many peaple asking how to make a 3d rpg camera, i know there are many out there but i will post mine so we can have many choices in choosing the code u prefer anyway lets go>>>:
1-our character will be a cube so to make it easy to understand camera code
our cube will be named cube!? (easy huh?)
2-we will make a plane that will be a ground for moving and we will name it plane!
3-will lets start by including the irrlicht headers and this kind of stuff:
here i declared also some variables we will use later!
then we will add the event receiver that will handle our keyboard input
4-next we will create the irrlicht device and declare some pointers
well i will not explain this,if u didnt understand this u should see toutorial no1 it tells u how to set irrlicht application
5-now we will add our hero(well it is only just a cube):
this adds the cube and disable the lighting bec we will not add lights here
6-now we will add the camera:
7-now we will add the loop where our game will run for about infinity unless the electricity went off
:
8-now here we will check if the player had pressed anykeys so we move the hero and move the camera:
ok let me explain this ,lets see for example the up when u press the W key the up will equal true so the hero moves in x and z axis acording to the rotation lets say the rotation =0 so the c.z will be changed by 0 because sin(0)=0 so the c.z will not change but in c.x,cos(0)=1 so the c.x will be changed by adding or subtracting in up function the x of the hero will be increased, well u dont have to understand this because it is a trig talk 
9-and now here is the most important part the 3d rpg camera code:
well here we set the rotation of the hero and also in it is a trig functions that by using cos and sin u can set the pos of the camera but since the getrotation returns degree so we have to change it to rad and apply the sin as i explained in the up function if u understood anything
10-well here we set the drivers and let it draw all the stuff
and this is all in easy 10 steps any qustion just drop it here
well here is the main.cpp :
http://files-upload.com/files/610009/main.cpp
1-our character will be a cube so to make it easy to understand camera code
our cube will be named cube!? (easy huh?)
2-we will make a plane that will be a ground for moving and we will name it plane!
3-will lets start by including the irrlicht headers and this kind of stuff:
Code: Select all
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
bool up = false;
bool down = false;
bool right = false;
bool left = false;
int speed=5;
then we will add the event receiver that will handle our keyboard input
Code: Select all
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == EET_KEY_INPUT_EVENT)
{
switch(event.KeyInput.Key)
{
case KEY_KEY_W:
up = event.KeyInput.PressedDown;
break;
case KEY_KEY_S:
down = event.KeyInput.PressedDown;
break;
case KEY_KEY_A:
left = event.KeyInput.PressedDown;
break;
case KEY_KEY_D:
right = event.KeyInput.PressedDown;
break;
}
//return true;
}
return false;
}
};
int main(int argc, char** argv)
{
Code: Select all
MyEventReceiver receiver;
IrrlichtDevice *device =
createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 16,
false, false, false, &receiver);
device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
5-now we will add our hero(well it is only just a cube):
Code: Select all
ISceneNode* hero = smgr->addCubeSceneNode();
IAnimatedMesh * movingplane;
movingplane=smgr->addHillPlaneMesh("floor", core::dimension2df(15, 15), core::dimension2di(40, 40));
IAnimatedMeshSceneNode *floor=smgr->addAnimatedMeshSceneNode(movingplane);
if (hero)
{
hero->setMaterialFlag(EMF_LIGHTING, false);
}
6-now we will add the camera:
Code: Select all
ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
:
Code: Select all
while(device->run())
{
Code: Select all
core::vector3df c = hero->getPosition();
core::vector3df d= hero->getRotation();
float diry = ((d.Y+90)*3.14)/180;
if (up)
{
c.X += speed * cos((d.Y) * 3.14 / 180);
c.Z -= speed * sin((d.Y) * 3.14 / 180);
}
if (down)
{
c.X -= speed * cos((d.Y) * 3.14 / 180);
c.Z += speed * sin((d.Y) * 3.14 / 180);
}
if (left)
{
d.Y -= 0.1;
}
if (right)
{
d.Y += 0.1;
}
9-and now here is the most important part the 3d rpg camera code:
Code: Select all
hero->setRotation(d);
int xf = (c.X-sin(diry)*125);
int yf =(c.Z-cos(diry)*125);
int zf =100;
hero->setPosition(c);
camera->setTarget(c);
c.Y +=200.f;
c.Z -= 150.f;
camera->setPosition(vector3df(xf,zf,yf));
10-well here we set the drivers and let it draw all the stuff
Code: Select all
driver->beginScene(true, true, SColor(0,200,200,200));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
}
device->drop();
return 0;
}
well here is the main.cpp :
http://files-upload.com/files/610009/main.cpp