Camera Movement

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
tomtetlaw
Posts: 110
Joined: Sun Jul 05, 2009 9:57 am

Camera Movement

Post by tomtetlaw »

When the camera moves, how come it doesn't move relative to the camera's Y angle?
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Re: Camera Movement

Post by Virion »

tomtetlaw wrote:When the camera moves, how come it doesn't move relative to the camera's Y angle?
can you be more specific about your problem? and maybe show some code?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Simply because it's up to you to calculate the movement according to your needs. If you just add some value onto one of the position elements, you won't get anything else than a movement to that specific position.
tomtetlaw
Posts: 110
Joined: Sun Jul 05, 2009 9:57 am

Post by tomtetlaw »

Well, I'm trying to get my FPS controls properly working, I've got movement worked out, but when I'm not looking straight, but side to side, then I press the W key, he still just moves forward instead of the way the camera is looking.

Here's the relevant code:

Code: Select all

ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();

	const f32 MOVEMENT_SPEED = 5.f;
	u32 then = device->getTimer()->getTime();

	while(device->run())
	{
		const u32 now = device->getTimer()->getTime();
		const f32 frameDeltaTime = (f32)(now - then) / 1000.f;
		then = now;

		core::vector3df nodePosition = camera->getPosition();

		if(receiver.IsKeyDown(irr::KEY_KEY_W))
			nodePosition.Z += MOVEMENT_SPEED * frameDeltaTime;

		else if(receiver.IsKeyDown(irr::KEY_KEY_S))
			nodePosition.Z -= 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;

		camera->setPosition(nodePosition);
		driver->beginScene(true, true, SColor(255,100,101,140));
		smgr->drawAll();

		driver->endScene();
	}
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Ok now we do a simple test.

First take a breath. Now take a look at your code.
Now take a look at your code again.
Do you see anything that tells the camera to move to the direction it is looking at? Well I don't. And your compiler probably neither.

So we have cleared this question. There is no code that does what you want. So here comes the next question: Why do you think that this code will do something it can't ?

And finally the one million dollar question: Why are you trying to implement your own FPS camera? There is a built-in FPS camera that does all the things for you.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Saumane
Posts: 27
Joined: Wed Jul 09, 2008 6:14 pm
Location: Finland

Post by Saumane »

Here you go:

Code: Select all

ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(); 

   const f32 MOVEMENT_SPEED = 5.f; 
   u32 then = device->getTimer()->getTime(); 

   while(device->run()) 
   { 
      const u32 now = device->getTimer()->getTime(); 
      const f32 frameDeltaTime = (f32)(now - then) / 1000.f; 
      then = now; 

      core::vector3df nodePosition = camera->getPosition(); 
      core::vector3df vel(0,0,0);

      if(receiver.IsKeyDown(irr::KEY_KEY_W)) 
         vel.Z += MOVEMENT_SPEED * frameDeltaTime; 

      else if(receiver.IsKeyDown(irr::KEY_KEY_S)) 
         vel.Z -= MOVEMENT_SPEED * frameDeltaTime; 

      if(receiver.IsKeyDown(irr::KEY_KEY_A)) 
         vel.X -= MOVEMENT_SPEED * frameDeltaTime; 

      else if(receiver.IsKeyDown(irr::KEY_KEY_D)) 
         vel.X += MOVEMENT_SPEED * frameDeltaTime; 

      matrix4 mat = camera->getAbsoluteTransformation();
      mat.rotateVect(vel);

      camera->setPosition(nodePosition+vel);
 
      driver->beginScene(true, true, SColor(255,100,101,140)); 
      smgr->drawAll(); 

      driver->endScene(); 
   }
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Wow great. Now he learned what ?
Ah yes. Copy+Paste.
Well I'm sure he knew how to do that before...
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Saumane
Posts: 27
Joined: Wed Jul 09, 2008 6:14 pm
Location: Finland

Post by Saumane »

Well, maybe from now he will know how to do it :)
tomtetlaw
Posts: 110
Joined: Sun Jul 05, 2009 9:57 am

Post by tomtetlaw »

I didn't even copy that answer, I used

Code: Select all

scene::ICameraSceneNode* camera=smgr>smgr>addCameraSceneNodeFPS()
#1 Most Common Error:
Reality Error: Object behind keyboard cannot program.
Post Reply