How to make a 3d rpg camera in 10 steps

A forum to store posts deemed exceptionally wise and useful
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

How to make a 3d rpg camera in 10 steps

Post 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
Last edited by omar shaaban on Sat Nov 10, 2007 7:27 am, edited 3 times in total.
GameDude
Posts: 498
Joined: Thu May 24, 2007 12:24 am

Post by GameDude »

A good tutorial, thanks.
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

thanks for your reply
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Yes nice work, the trig stuff is always what stops me from making a good 3rd person camera :lol:
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

well i am glad i helped you!
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post 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
dejai
Posts: 522
Joined: Sat Apr 21, 2007 9:00 am

Post by dejai »

OMG THANKS HEAPS!!
Programming Blog: http://www.uberwolf.com
dejai
Posts: 522
Joined: Sat Apr 21, 2007 9:00 am

Post 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
Programming Blog: http://www.uberwolf.com
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post 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.
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post 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
Forsaken
Posts: 3
Joined: Sun Jul 22, 2007 4:31 pm

Post 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?
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post 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
Forsaken
Posts: 3
Joined: Sun Jul 22, 2007 4:31 pm

Post 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
afecelis
Admin
Posts: 3075
Joined: Sun Feb 22, 2004 10:44 pm
Location: Colombia
Contact:

Post by afecelis »

great tut Omar! thanks for sharing!
Will try it out immediately. ;)
Forsaken
Posts: 3
Joined: Sun Jul 22, 2007 4:31 pm

Post by Forsaken »

got it fixed with matrixes thanks anyway
Post Reply