Noob needs help with camera rotation :)
Posted: Wed Mar 17, 2010 12:21 pm
Hi folks,
Before jumping into writing my own camera class, I figured it would be good idea to fully understand how cameras work, so I'm writing a short simple program to move/rotate a static camera as follows:
Basically, I'm just increasing and decreasing the value of camPosition along the Z axis when moving forward or backward:
This works, though I'm not sure igf its the right way, please correct me if its not.
And I rotate the camera in same way, by simply increasing/decreasing the value of camRotate along Y axis :
The camera does rotate with A and D keys, but if you try to move forward/backward again after rotating everything gets mixed-up.
I'm pretty sure this is not the right way to rotate since I'm not taking any drgrees/radians into account at all.
Could someone please provide me a simple formula to use here, which would achieve rotation of the camera?
Many thanks in advance!
cheers!
Before jumping into writing my own camera class, I figured it would be good idea to fully understand how cameras work, so I'm writing a short simple program to move/rotate a static camera as follows:
Code: Select all
int main()
{
// create device
MyEventReceiver receiver;
IrrlichtDevice* device = createDevice(EDT_OPENGL,
dimension2d<u32>(1024, 768), 16, false, false, false, &receiver);
if (device == 0)
return 1; // could not create selected driver.
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
// add background
device->getFileSystem()->addZipFileArchive("map-20kdm2.pk3");
scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
scene::ISceneNode* room = 0;
if (mesh)
room = smgr->addOctreeSceneNode(mesh->getMesh(0), 0, -1, 1024);
if (room)
room->setPosition(core::vector3df(-1300,-144,-1249));
// add a camera
ICameraSceneNode *camera = smgr->addCameraSceneNode();
camera->setPosition(vector3df(0,0,0));
camera->bindTargetAndRotation(true);
// hide mouse
device->getCursorControl()->setVisible(false);
int lastFPS = -1;
// In order to do framerate independent movement, we have to know
// how long it was since the last frame
u32 then = device->getTimer()->getTime();
// This is the movemen speed in units per second.
const f32 MOVEMENT_SPEED = 50.0f; // how fast camera moves
const f32 ROTATION_SPEED = 50.0f; // how fast camera rotates
while(device->run())
{
core::vector3df camPosition = camera->getPosition();
core::vector3df camRotation = camera->getRotation();
// Work out a frame delta time.
const u32 now = device->getTimer()->getTime();
const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
then = now;
/* Check if keys W, S, A or D are being held down, and move the
camera around respectively. */
if(receiver.IsKeyDown(KEY_KEY_W))
camPosition.Z += MOVEMENT_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(KEY_KEY_S))
camPosition.Z -= MOVEMENT_SPEED * frameDeltaTime;
if(receiver.IsKeyDown(KEY_KEY_A))
camRotation.Y -= ROTATION_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(KEY_KEY_D))
camRotation.Y += ROTATION_SPEED * frameDeltaTime;
camera->setRotation(camRotation);
camera->setPosition(camPosition);
driver->beginScene(true, true, SColor(255,113,113,133));
smgr->drawAll(); // draw the 3d scene
device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
//stringw tmp(L"Movement Example - Irrlicht Engine [");
//tmp += driver->getName();
//tmp += L"] fps: ";
//tmp += fps;
wchar_t tmp[1024];
swprintf(tmp, 1024, L"FP Camera Example (%s)(fps:%d)(Cam:%2.2f %2.2f %2.2f) (Tar:%2.2f %2.2f %2.2f) )",
driver->getName(), fps,camera->getAbsolutePosition().X,camera->getAbsolutePosition().Y, camera->getAbsolutePosition().Z, camera->getTarget().X, camera->getTarget().Y, camera->getTarget().Z);
device->setWindowCaption(tmp);
//device->setWindowCaption(tmp.c_str());
lastFPS = fps;
}
}
/*
In the end, delete the Irrlicht device.
*/
device->drop();
return 0;
}
Code: Select all
if(receiver.IsKeyDown(KEY_KEY_W))
camPosition.Z += MOVEMENT_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(KEY_KEY_S))
camPosition.Z -= MOVEMENT_SPEED * frameDeltaTime;
And I rotate the camera in same way, by simply increasing/decreasing the value of camRotate along Y axis :
Code: Select all
if(receiver.IsKeyDown(KEY_KEY_A))
camRotation.Y -= ROTATION_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(KEY_KEY_D))
camRotation.Y += ROTATION_SPEED * frameDeltaTime;
I'm pretty sure this is not the right way to rotate since I'm not taking any drgrees/radians into account at all.
Could someone please provide me a simple formula to use here, which would achieve rotation of the camera?
Many thanks in advance!
cheers!