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