Page 1 of 3

How to make a 3d rpg camera in 10 steps

Posted: Thu Jul 19, 2007 10:09 pm
by omar shaaban
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:

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;

here i declared also some variables we will use later!
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)
{
4-next we will create the irrlicht device and declare some pointers

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();

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):

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);
    }
this adds the cube and disable the lighting bec we will not add lights here
6-now we will add the camera:

Code: Select all

 ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
7-now we will add the loop where our game will run for about infinity unless the electricity went off :P
:

Code: Select all

 while(device->run())
    {
8-now here we will check if the player had pressed anykeys so we move the hero and move the camera:

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;
         }
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 :wink:

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));
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 :lol:
10-well here we set the drivers and let it draw all the stuff
:P

Code: Select all

driver->beginScene(true, true, SColor(0,200,200,200));

        smgr->drawAll();
        guienv->drawAll();

        driver->endScene();
    }
}
        device->drop();

    return 0;
}

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

Posted: Fri Jul 20, 2007 2:43 am
by GameDude
A good tutorial, thanks.

Posted: Fri Jul 20, 2007 11:12 am
by omar shaaban
thanks for your reply

Posted: Fri Jul 20, 2007 12:00 pm
by BlindSide
Yes nice work, the trig stuff is always what stops me from making a good 3rd person camera :lol:

Posted: Fri Jul 20, 2007 12:13 pm
by omar shaaban
well i am glad i helped you!

Posted: Fri Jul 20, 2007 3:23 pm
by Halan
seems to work but you should use a seperate class for the cam and also use irrlicht constants like PI etc

i normally calculate the position of the cam simply by rotation a vector with a matrix

greets,
halan

Posted: Sat Jul 21, 2007 2:45 am
by dejai
OMG THANKS HEAPS!!

Posted: Sat Jul 21, 2007 2:58 am
by dejai
small problem its not working, typed it out it didn't work so i then Copied and pasted and still a error!!!

Problems located
int xf = (c.X-sin(diry)*125);
int yf =(c.Z-cos(diry)*125);
int zf =100;

C:\Documents and Settings\Compaq_Owner.BENSPC\Desktop\Movement Game\Makefile.win [Build Error] [obj/main.o] Error 1

Posted: Sat Jul 21, 2007 4:36 am
by BlindSide
Halan wrote:seems to work but you should use a seperate class for the cam and also use irrlicht constants like PI etc

i normally calculate the position of the cam simply by rotation a vector with a matrix

greets,
halan
Thats true but there are already millions on this forum that are class based, this one is nice and unique because it shows you the bare steps involved, (Especially the trig) in making a 3rd person camera.

Anyone with half decent C++ knowledge should then be able to use this information to integrate it into a custom camera class suitable for there specific game.

Posted: Sat Jul 21, 2007 8:45 am
by omar shaaban
dejai wrote:small problem its not working, typed it out it didn't work so i then Copied and pasted and still a error!!!

Problems located
int xf = (c.X-sin(diry)*125);
int yf =(c.Z-cos(diry)*125);
int zf =100;

C:\Documents and Settings\Compaq_Owner.BENSPC\Desktop\Movement Game\Makefile.win [Build Error] [obj/main.o] Error 1
could u tell me what the error says!? because it works perfectly at me anyway i posted the .cpp files at the first post

Posted: Sun Jul 22, 2007 4:32 pm
by Forsaken
it works fine for me but i cant figure out how to place the camera exactly where i want relative to my object which line would i edit to adjust the x, y z axis accordingly to get it to follow at the proper angle i wish?

Posted: Sun Jul 22, 2007 5:55 pm
by omar shaaban
Forsaken wrote:it works fine for me but i cant figure out how to place the camera exactly where i want relative to my object which line would i edit to adjust the x, y z axis accordingly to get it to follow at the proper angle i wish?
will change the value for the zf to control the angle

Posted: Sun Jul 22, 2007 6:51 pm
by Forsaken
I'm still having some trouble I edited your zf but i still cant get it right

How it looks with your camera:
http://i204.photobucket.com/albums/bb16 ... ourcam.jpg

How I want it to look (this uses a still cam)
http://i204.photobucket.com/albums/bb16 ... -2copy.jpg

im also trying to get it so when you press A or D it rotates but the characters model doesnt rotate, so it would look like hes always walking straight ahead like in the 2nd screenshot

Posted: Sun Jul 22, 2007 10:44 pm
by afecelis
great tut Omar! thanks for sharing!
Will try it out immediately. ;)

Posted: Mon Jul 23, 2007 4:17 am
by Forsaken
got it fixed with matrixes thanks anyway