camera rotation.. it's wobbling.. help me please..
Posted: Wed Sep 30, 2009 2:54 pm
HI.. I have a question.. please help me..
I put a camera behind my character... and at first, I didn't ratate at all.. it worked.. there was no wobbling..no jagged..
but when I try to rotate as I want...as of now, for the test, I rotated my camera -40 degrees......
yeah.. it rotated well. Looks everything is fine... but when I move my character(at the moment when I press a key or release a key.. or while I'm pressing a key...), the screen wobbles .. it makes me even sick..
so... what I want to know is.... why does it wobbles when I rotate the camera?? and doesn't when I didn't rotate the camera?
following is my code .... please help me....
I put a camera behind my character... and at first, I didn't ratate at all.. it worked.. there was no wobbling..no jagged..
but when I try to rotate as I want...as of now, for the test, I rotated my camera -40 degrees......
yeah.. it rotated well. Looks everything is fine... but when I move my character(at the moment when I press a key or release a key.. or while I'm pressing a key...), the screen wobbles .. it makes me even sick..
so... what I want to know is.... why does it wobbles when I rotate the camera?? and doesn't when I didn't rotate the camera?
following is my code .... please help me....
#include <irrlicht.h>
#include <iostream>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
float CameraDistance;
class MyEventReceiver : public IEventReceiver
{
public:
struct SMouseState
{
core::position2di Position;
core::position2di OldPosition;
bool LeftButtonDown;
SMouseState() : LeftButtonDown(false) { }
} MouseState;
virtual bool OnEvent(const SEvent& event)
{
// Remember whether each key is down or up
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
// Remember the mouse state
if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
switch(event.MouseInput.Event)
{
case EMIE_LMOUSE_PRESSED_DOWN:
MouseState.LeftButtonDown = true;
break;
case EMIE_LMOUSE_LEFT_UP:
MouseState.LeftButtonDown = false;
break;
case EMIE_MOUSE_MOVED:
MouseState.Position.X = event.MouseInput.X;
MouseState.Position.Y = event.MouseInput.Y;
MouseState.OldPosition.X = MouseState.Position.X;
MouseState.OldPosition.Y = MouseState.Position.Y;
break;
case EMIE_MOUSE_WHEEL:
CameraDistance -= event.MouseInput.Wheel;
if (CameraDistance>25.0f)
CameraDistance = 25.0f;
if (CameraDistance<-5.0f)
CameraDistance = -5.0f;
break;
default:
break;
}
}
return false;
}
virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}
MyEventReceiver()
{
for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
KeyIsDown = false;
MouseState.OldPosition.X = 0;
MouseState.OldPosition.Y = 0;
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
int main()
{
MyEventReceiver receiver;
IrrlichtDevice *device =
createDevice(EDT_DIRECT3D9, core::dimension2d<s32>(640, 480), 16, false, false, false, &receiver);
if (device == 0)
return 1;
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3");
scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
scene::ISceneNode* node = 0;
if (mesh)
node = smgr->addOctTreeSceneNode(mesh->getMesh(0), 0, -1, 1024);
node = smgr->addMeshSceneNode(mesh->getMesh(0));
if (node)
node->setPosition(core::vector3df(-1300,-144,-1249));
IAnimatedMesh * NinjaMesh = smgr->getMesh("ninja.b3d");
IAnimatedMeshSceneNode * NinjaNode = smgr->addAnimatedMeshSceneNode(NinjaMesh);
NinjaNode->setScale(vector3df(3, 3, 3));
NinjaNode->setMaterialFlag(EMF_LIGHTING, false);
NinjaNode->setAnimationSpeed(15);
NinjaNode->setPosition(vector3df(0, 0, 0));
vector3df CameraTarget = vector3df(NinjaNode->getPosition().X, NinjaNode->getPosition().Y+5, NinjaNode->getPosition().Z);
ICameraSceneNode * camera = smgr->addCameraSceneNode(NinjaNode, vector3df(0, 10, -10), CameraTarget);
camera->bindTargetAndRotation(true);
device->getCursorControl()->setVisible(false);
u32 then = device->getTimer()->getTime();
position2di oldMousePosition = receiver.MouseState.Position;
int lastFPS = -1;
const f32 MOVEMENT_SPEED = 80.f;
const f32 ROTATION_SPEED = 40.f;
while(device->run())
{
if (device->isWindowActive())
{
const u32 now = device->getTimer()->getTime();
const f32 FrameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
then = now;
vector3df NinjaPosition = NinjaNode->getPosition();
vector3df NinjaRotation = NinjaNode->getRotation();
if(receiver.IsKeyDown(irr::KEY_KEY_W))
{
NinjaPosition.X -= MOVEMENT_SPEED * FrameDeltaTime * cos( (NinjaRotation.Y+90)*PI/180.0f );
NinjaPosition.Z += MOVEMENT_SPEED * FrameDeltaTime * sin( (NinjaRotation.Y+90)*PI/180.0f );
}
else if(receiver.IsKeyDown(irr::KEY_KEY_S))
{
NinjaPosition.X += MOVEMENT_SPEED * FrameDeltaTime * cos( (NinjaRotation.Y+90)*PI/180.0f );
NinjaPosition.Z -= MOVEMENT_SPEED * FrameDeltaTime * sin( (NinjaRotation.Y+90)*PI/180.0f );
}
if(receiver.IsKeyDown(irr::KEY_KEY_A))
{
NinjaRotation.Y -= ROTATION_SPEED * FrameDeltaTime;
if (NinjaRotation.Y<0.0f)
NinjaRotation.Y = 360.0f;
}
else if(receiver.IsKeyDown(irr::KEY_KEY_D))
{
NinjaRotation.Y += ROTATION_SPEED * FrameDeltaTime;
if (NinjaRotation.Y>360.0f)
NinjaRotation.Y = 0.0f;
}
NinjaNode->setPosition(NinjaPosition);
NinjaNode->setRotation(NinjaRotation);
CameraTarget = vector3df(NinjaNode->getPosition().X, NinjaNode->getPosition().Y+5, NinjaNode->getPosition().Z);
camera->setPosition(vector3df(0, 10+CameraDistance, -10-CameraDistance));
vector3df target = CameraTarget - camera->getAbsolutePosition();
vector3df relativeRotation = target.getHorizontalAngle();
relativeRotation.X -= 40.0f; // this line of code makes wobbling problem....
target.set(0, 0, target.getLength());
matrix4 mat;
mat.setRotationDegrees(vector3df(relativeRotation.X, relativeRotation.Y, 0));
mat.transformVect(target);
target += camera->getAbsolutePosition();
camera->setTarget(target);
driver->beginScene(true, true, video::SColor(255,200,200,200));
smgr->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"Irrlicht Engine - Quake 3 Map example [";
str += driver->getName();
str += "] FPS:";
str += fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
else
device->yield();
}
device->drop();
return 0;
}