I wrote a program that displays a level in a special format. The loader I wrote for Irrlicht works correctly.
When I started developing the Loader, I could only slide with my camera (left, right, up, down, forward, back). But now I also want to implement turning.
This is what I wrote (some of the code was copied from this tutorial: http://irrlicht.sourceforge.net/phpBB2/ ... a+rotation ):
Code: Select all
// 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 = 5.f;
camera->bindTargetAndRotation(true);
while(device->run())
{
// 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;
core::vector3df nodePosition = node->getPosition();
core::vector3df nodeRotation = node->getRotation();
//keys W,A,S,D,I,O for sliding
if(receiver.IsKeyDown(irr::KEY_KEY_S))
nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(irr::KEY_KEY_W))
nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;
if(receiver.IsKeyDown(irr::KEY_KEY_A))
nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(irr::KEY_KEY_D))
nodePosition.X += MOVEMENT_SPEED * frameDeltaTime;
if(receiver.IsKeyDown(irr::KEY_KEY_O))
nodePosition.Z -= MOVEMENT_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(irr::KEY_KEY_I))
nodePosition.Z += MOVEMENT_SPEED * frameDeltaTime;
float diry = ((nodeRotation.Y+90)*3.14)/180;
//arrow keys for rotation
if(receiver.IsKeyDown(irr::KEY_DOWN)) {
nodePosition.X += MOVEMENT_SPEED * cos((nodeRotation.Y) * 3.14 / 180);
nodePosition.Z -= MOVEMENT_SPEED * sin((nodeRotation.Y) * 3.14 / 180);
}
else if(receiver.IsKeyDown(irr::KEY_UP)) {
nodePosition.X -= MOVEMENT_SPEED * cos((nodeRotation.Y) * 3.14 / 180);
nodePosition.Z += MOVEMENT_SPEED * sin((nodeRotation.Y) * 3.14 / 180);
}
if(receiver.IsKeyDown(irr::KEY_LEFT)) {
nodeRotation.Y -= 0.1;
}
else if(receiver.IsKeyDown(irr::KEY_RIGHT)) {
nodeRotation.Y += 0.1;
}
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
break;
node->setRotation(nodeRotation);
int xf = (nodePosition.X-sin(diry)*125);
int yf = (nodePosition.Z-cos(diry)*125);
int zf = 0;
node->setPosition(nodePosition);
camera->setTarget(nodePosition);
nodePosition.Y +=200.f;
nodePosition.Z -= 150.f;
camera->setPosition(vector3df(xf,zf,yf));
driver->beginScene(true, true, video::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)
{
core::stringw tmp(L"Movement Example - Irrlicht Engine [");
tmp += driver->getName();
tmp += L"] fps: ";
tmp += fps;
device->setWindowCaption(tmp.c_str());
lastFPS = fps;
}
}
What's wrong here?