Shaking screen

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
Elmo115
Posts: 18
Joined: Wed Feb 06, 2008 11:35 pm

Shaking screen

Post by Elmo115 »

I have a game where it is in third person format. I have created collision detection for my player and the camera and they work correctly. Though my problem is that when the camera gets close or touches objects, the screen starts shaking. How can I stop it from shaking?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Well if we knew why the camera was shaking we might be able to say how to fix it ;)

Presumably you don't know why it's shaking either so post your code that handles the camera setup and collision.
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Ideally, you'd upload a fully compilable example plus any required resources. It's generally quicker to compile and debug than it is to statically analyse source snippets in isolation, especially as the problem is often in the code that you don't provide.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Elmo115
Posts: 18
Joined: Wed Feb 06, 2008 11:35 pm

Post by Elmo115 »

This is all of the code that is part of the game. I have the sections stated that deal with the player (hero) and camera collision and detection.

Code: Select all

#include <irrlicht.h>
#include <math.h>
#include <iostream>

#define PI (3.14159265)
#define radtodeg (180.0/PI)
#define degtorad (PI/180.0)

using namespace irr;

IrrlichtDevice* device = 0;
scene::IAnimatedMeshSceneNode* hero = 0;

//Camera rotation
float camX=-90, camY=40, camDistance=150 /*this one is a distance */, test=0;

//Resolution declaration
int width=1024,height=768,depth=32;

//Key states
bool up    = false;
bool down  = false;
bool right = false;
bool left  = false;

class charControl : public IEventReceiver
{
public:
   virtual bool OnEvent(SEvent event)
   {
      //Get key states      
      if (event.EventType == EET_KEY_INPUT_EVENT)
      {
         if(event.KeyInput.Key==KEY_KEY_W)
          if(event.KeyInput.PressedDown)
             hero->setMD2Animation(scene::EMAT_RUN);
          else
             hero->setMD2Animation(scene::EMAT_STAND);
         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;
      }
      if (event.EventType == EET_MOUSE_INPUT_EVENT)
      {
         camX+=(event.MouseInput.X-width/2)*0.1;//0.5
         camY+=(event.MouseInput.Y-height/2)*0.1;//0.5
    	 
         if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) camDistance-=event.MouseInput.Wheel*7; 
         
         //Limit the movement
         if (camDistance<0) camDistance=0.1;
         if (camY>60) camY=60; //85
         if (camY<0) camY=0;  
      }

      return false;
   }
}; 

int main(int argc, char *argv[])
{
    //check screen dimensions from arguments
    if(argc==3)
     if(strcmp(argv[1],"-screen")==0)
     {
      sscanf(argv[2],"%dx%dx%d",&width,&height,&depth);
     }

	// create device
    charControl receiver; 

	// create device and exit if creation failed
	IrrlichtDevice* device= createDevice(video::EDT_OPENGL, core::dimension2d<s32>(width,height),depth,true,true,true,&receiver);
	//IrrlichtDevice *device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(640, 480),depth,false,false,false,&receiver);
	if (device == 0)	return 1; // could not create selected driver.
	// set caption & hide cursor
	device->setWindowCaption(L"Subversion");
	device->getCursorControl()->setVisible(false);
    device->getCursorControl()->setPosition(width/2,height/2);   //center cursor
	// create scenes
	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	// load the scene
	smgr->loadScene("scenes/level1.irr");

    scene::ISceneNode* node = node=smgr->getSceneNodeFromId(0);
    scene::IAnimatedMesh* mesh = smgr->getMeshCache()->getMeshByIndex(0);
    
    // Add triangle selector
    scene::ITriangleSelector* selector = 0; 
    selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128);    

    // add a user controlled camera
	scene::ICameraSceneNode* camera = 
	camera = smgr->addCameraSceneNode(0);
	camera->setPosition(core::vector3df(-20,20,-20));

    // add animated hero.
	video::SMaterial material;
	material.Textures[0] = driver->getTexture("characters/sydney/texture.bmp");
	material.Lighting = true;
	//scene::IAnimatedMeshSceneNode* node = 0;
	scene::IAnimatedMesh* heromesh = smgr->getMesh("characters/sydney/amesh.md2");
	if (heromesh)
	{
		hero = smgr->addAnimatedMeshSceneNode(heromesh);
		hero->setPosition(core::vector3df(20,-15,20));
		hero->setMD2Animation(scene::EMAT_STAND);
		hero->setScale(core::vector3df(1.5,1.5,1.5));
		hero->getMaterial(0) = material;
	}
	// Hero collision detection.
   if (selector) 
   { 
      scene::ISceneNodeAnimator* anim =
      smgr->createCollisionResponseAnimator( 
          selector, hero,
          core::vector3df(17,25,20), 
          core::vector3df(0,-3,0),
          core::vector3df(0,8,0));        
      hero->addAnimator(anim); 
      anim->drop(); 
      selector->drop(); 
   } 
   // Camera collision detection.
   if (selector) 
   { 
      scene::ISceneNodeAnimator* cameraanim =
      smgr->createCollisionResponseAnimator( 
          selector, camera,
          core::vector3df(15,25,5), 
          core::vector3df(0,3,0),
          core::vector3df(0,5,0));        
      camera->addAnimator(cameraanim); 
      cameraanim->drop(); 
       
   } 

     

	// render loop
	while(device->run())
	if (device->isWindowActive())
	{
		driver->beginScene(true, true, video::SColor(0,255,255,255));
        device->getCursorControl()->setPosition(width/2,height/2);   //center cursor
        
    	
		//Execute moving		//float relDisplacement;
		float speed=4;//6
		float rotspeed=2.5;//4.5
	    
		//Get Position&Rotation
		core::vector3df position = hero->getPosition();
		core::vector3df camposition = position;
        core::vector3df rotation = hero->getRotation();
        
        //Change camera settings
        camera->setTarget(position);
        camposition.X+=sin(camX*degtorad)*camDistance;
        camposition.Z+=cos(camX*degtorad)*camDistance;
        camposition.Y+=sin(camY*degtorad)*camDistance;
        camera->setPosition(camposition);
        
        
         //Modify Position and Rotation        //relDisplacement=0;
        if (up||down)
        {
           float relDisplacement = down ? speed : -speed;
           position.X+= -cos(rotation.Y * degtorad)*relDisplacement;
           position.Z+=  sin(rotation.Y * degtorad)*relDisplacement;
           rotation.Y = camX + 90.0; /*yuuuuuk*/
        }
           
//#define RIGHT_LEFT_ROTATES        
#ifndef RIGHT_LEFT_ROTATES
        if (left||right)
        { 
           float relDisplacement = left ? speed : -speed;
           position.X += sin(rotation.Y * degtorad) * relDisplacement;
           position.Z += cos(rotation.Y * degtorad) * relDisplacement;  //cos
           rotation.Y = camX + 90.0; /*yuuuuuk*/
        }
                   
#else          
        if (left||right)
        {
           rotation.Y += right ? rotspeed : -rotspeed;              
        }
#endif
        //Set Position&Rotation
        hero->setPosition(position);
        hero->setRotation(rotation);
               
        
		smgr->drawAll();
		driver->endScene();
  	}

	device->drop();
	
	//scanf("%c");
	return 0;
}
Something that did make a difference is when I changed the size of the camera collision detection circle, the shaking happened less. I think that this is because I now have to get closer to objects before the camera detects a collision. Then it starts to shake like it did before.
kobsure@hotmail.com
Posts: 1
Joined: Tue Feb 26, 2008 1:29 am

ever same prob

Post by kobsure@hotmail.com »

the problem is

*** camera->setTarget(position); **

"position" is not the last result position until you call smgr->drawall();

(cause you use the collisionrespond with hero right? then the position of hero that camera look at isn't the real position of hero.)

What to do?

I don't know the best way to fix this.
I 've call **hero->onAnimate(difTime);** before set camera target.

BUT there still a problem when u enable gravity then I set gravity to (0,0,0) and find hero.Y by use collisionwith line (head to foot) to get hero Y

If you can find the best way plz tell me
Post Reply