3rd person camera vibration!?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

3rd person camera vibration!?

Post by omar shaaban »

:? well i have made a 3rd person camera and the camera follows the ship but when i move the ship forward the camera moves with the ship in a vibratin way the screen vibrate whenever i move my object!!??
here is my code:

Code: Select all

#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>

using namespace irr;

#pragma comment(lib, "Irrlicht.lib")

using namespace irr;
int sec =0;
int speed=5.0f;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
scene::ISceneNode* ship = 0;
IrrlichtDevice* device = 0;
int up,down,right,left;
int far =350;
class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
	    //UP A
		 if (event.KeyInput.Key==KEY_KEY_W)
    {
        down=false;
up=true;

    }
    //DOWN D
     if (event.KeyInput.Key==KEY_KEY_S)
    {
        up=false;
   down=true;
    }
    //LEFT A
     if (event.KeyInput.Key==KEY_KEY_A)
    {
        right=false;
   left=true;
    }
    //RIGHT D
     if (event.KeyInput.Key==KEY_KEY_D)
    {
        left=false;
 right=true;
    }

	return false;
 }

 };




 //game




int main()
{
     MyEventReceiver receiver;
device =createDevice(EDT_DIRECT3D9, dimension2d<s32>(800, 600), 32,
			false, false, false, &receiver);

	device->setWindowCaption(L"the funking 3d game");

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();


ship = smgr->addAnimatedMeshSceneNode( smgr->getMesh("ship.obj") );
 scene::ISceneNode * box = smgr->addCubeSceneNode();
  box->setMaterialTexture(0,driver->getTexture("char.bmp"));


if (ship)
{
	ship->setMaterialFlag(EMF_LIGHTING, false);
box->setMaterialFlag(EMF_LIGHTING, false);
	ship->setMaterialTexture( 0, driver->getTexture("nave.bmp") );
}

//camera
scene::ICameraSceneNode * camera = smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));


driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);

smgr->addSkyBoxSceneNode(
	driver->getTexture("irrlicht2_up.jpg"),
	driver->getTexture("irrlicht2_dn.jpg"),
	driver->getTexture("irrlicht2_lf.jpg"),
	driver->getTexture("irrlicht2_rt.jpg"),
	driver->getTexture("irrlicht2_ft.jpg"),
	driver->getTexture("irrlicht2_bk.jpg"));

driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);




//run time!!!

while(device->run())
	{
core::vector3df c = ship->getPosition();


//UP A
		 if (up==true)
    {
core::vector3df v = ship->getPosition();
     	v.Z +=speed;
camera->setPosition(core::vector3df(v.X,v.Y+50,v.Z-far));
camera->setTarget(core::vector3df(v.X,v.Y,v.Z));
ship->setPosition(v);

up=false;
    }
    //DOWN D
     if (down==true)
    {
     	core::vector3df v = ship->getPosition();

v.Z -= speed;
ship->setPosition(v);
    }
    //LEFT A
     if (left==true)
    {
     	core::vector3df v = ship->getPosition();
     	v.X -=speed;
ship->setPosition(v);

camera->setPosition(core::vector3df(v.X,v.Y+50,v.Z-far));
camera->setTarget(core::vector3df(v.X,v.Y,v.Z));
    }
    //RIGHT D
     if (right==true)
    {
     core::vector3df v = ship->getPosition();
     	v.X += speed;
ship->setPosition(v);

camera->setPosition(core::vector3df(v.X,v.Y+50,v.Z-far));
camera->setTarget(core::vector3df(v.X,v.Y,v.Z));
    }

right=down=left=false;


		int fps = driver->getFPS();



	core::stringw ddf = fps;
		ddf+=" Z =  ";
	ddf+=c.Z;
		ddf+=" Y =  ";
	ddf+=c.Y;
		ddf+=" X =  ";
	ddf+=c.X;
	ddf+=" sec: ";

	ddf+=sec;

device->setWindowCaption(ddf.c_str());
driver->beginScene(true, true, SColor(0,200,200,200));
smgr->drawAll();
		guienv->drawAll();


		driver->endScene();


}

	device -> drop() ;

	return 0;
}

thanx in advance
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

When you make changes to the camera's position call cam->updateAbsolutePosition()

also for the node which you are moving along with the camera, i.e your player's node, that may need its absolute position updating too.
Image Image Image
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

:shock: the same problem can u give me example code anything and thanks for the reply
rewb0rn
Posts: 15
Joined: Thu May 25, 2006 9:26 pm
Location: Berlin, Germany

Post by rewb0rn »

without having read your code, i had similiar problem once which was solved by changing my cameras up vector from (0.0, 1.0, 0.0) to (0.0, 1000.0, 0.0), this was caused by rounding inaccuracy, maybe its the same in your project.
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

without having read your code, i had similiar problem once which was solved by changing my cameras up vector from (0.0, 1.0, 0.0) to (0.0, 1000.0, 0.0), this was caused by rounding inaccuracy, maybe its the same in your project.
how?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Always check the API if you're not sure!

http://irrlicht.sourceforge.net/docu/cl ... _node.html
Image Image Image
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

:? i have set up the camera vector but nothing changed
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

First off, you only really need to modify the up vector if the camera is looking nearly straight up or down. This is not the case for his code.

I see several problems with the code above. Here is a list...
  1. There is no check that the event type is a key input event before checking which key is pressed. This can cause your code to think a key is pressed when it is not.
  2. The ship speed is frame rate dependent. The ship will move faster when you are getting high frame rates and slower when they are lower.
  3. The keyboard handling is causing the camera/ship to jitter. The key pressed flag is reset after every render, but the key press events aren't coming in that fast. This causes the motion to start, stop, start, stop, start, stop...
Here is some modified code. It is slightly different because it allows the ship to travel diagonally, but that could easily be fixed.

Code: Select all

bool up    = false;
bool down  = false;
bool right = false;
bool left  = false; 

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() 
{ 
   // create device

   // setup scene

   ITimer* timer = device->getTimer();

   u32 then = timer->getRealTime();
   while(device->run()) 
   {
      u32 now = timer->getRealTime();

      // calculate the elapsed time in seconds
      f32 elapsed = (now - then) / 1000.f;
      then = now;

      if (device->isWindowActive())
      {
         // render the scene
         if (driver->beginScene(true, true, SColor(0,200,200,200)))
         {
            smgr->drawAll(); 

            guienv->drawAll(); 

            driver->endScene(); 
         }

         core::vector3df c = ship->getPosition(); 

         // set window caption code

         if (up) 
         { 
            c.Z += (speed * elapsed);
         } 
         if (down) 
         { 
            c.Z -= (speed * elapsed); 
         } 
         if (left) 
         { 
            c.X -= (speed * elapsed); 
         } 
         if (right) 
         { 
            c.X += (speed * elapsed); 
         }

         ship->setPosition(c);
         camera->setTarget(c);

         c.Y += 50.f;
         c.Z -= far;

         camera->setPosition(c); 
      }
      else
      {
         // clear keys when window goes out of focus
         up = down = left = right = false;
      }
   }

   device->drop(); 

   return 0;
}
BAD_ANGEL
Posts: 5
Joined: Thu Nov 09, 2006 7:39 pm
Location: Germany

Post by BAD_ANGEL »

i had the same problem,too....i updated the relative positions and it works THX

But why??Whats the reason for that??
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

:D thanks very much vitek
big thanks :P
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

BAD_ANGEL wrote:i had the same problem,too....i updated the relative positions and it works THX

But why??Whats the reason for that??
I'm assuming that you're asking why updating the positions causes a jitter.

few things to consider first..

1. I have never had to do this myself..yet
2. I don't know the real answer

on assumption alone I'm going to guess that the code automaticly updates the possitions of ALL nodes based on what you would call the frame rate which is directly equal to the times the rendering code is read.

ok imagine this your ship moves...camera follows but until the code reaches update position somewhere deep in the node code then the actual view is not rendered...thats another thing rendering has to pass through this phase also so you get this...

ship/cam moves then
within a few say 20-infinate milliseconds it reachs the update stage for both the position and the rendering cause a pop in the view.

if you call update position at the same time as the movement then this pop never accures or rather it's so fast you never see it or even more indepth it never happens because it's done before it's rendered.

considering I'm not just totally wrong here thats an idea of how it works or maybe just enough to passify either way my work is done here.
Post Reply