Page 1 of 1

Rotating Camera

Posted: Tue Aug 25, 2009 2:55 am
by sarabulho
I want rotate the camera through the Y axis, with the E and R keys, but i keep failing, can anyone please help me? i don't know what else to do

Posted: Tue Aug 25, 2009 3:41 am
by Lonesome Ducky
Can you show some of the code that failed? We can't help much without it and such a general question.

Posted: Tue Aug 25, 2009 2:39 pm
by sarabulho
I want replace the mouse using keys from keyboard to do what the mouse do.
The camera stay fixed and i want to look up and down, and look to right and left side but the camera have to stay fixed in a point.

i have tray with two codes:

1:

Code: Select all

core::vector3df cameraRotation = camera->getRotation();
if(receiver.IsKeyDown(irr::KEY_KEY_Q))
         cameraRotation.X -= ROTATION_SPEED * frameDeltaTime;


2:

Code: Select all

// 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();
	  core::vector3df cameraRotation = camera->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)) {
         cameraRotation.Y -= 0.1;
      }
                else if(receiver.IsKeyDown(irr::KEY_RIGHT)) {
         cameraRotation.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(core::vector3df(xf,zf,yf));
//	  camera->setRotation(core::vector3df(cameraRotation.Y);

Posted: Tue Aug 25, 2009 3:22 pm
by JeroenP
Hello,

When you want to set the camera rotation, you first have to set bindTargetAndRotation to true.
(I think it's a bug in irrlicht, if you don't set it to true, the target is not updated, and in the draw code it uses the target, so setting the rotation does nothing...)

A better name for bindTargetAndRotation would be enableRotation or something like that...

first:

Code: Select all

camnode->bindTargetAndRotation(true);
in the main loop:

Code: Select all

core::vector3df cameraRotation = camera->getRotation(); 
if(receiver.IsKeyDown(irr::KEY_KEY_Q)) 
         cameraRotation.X -= ROTATION_SPEED * frameDeltaTime;
camera->setRotation(cameraRotation);
That should work...

Posted: Tue Aug 25, 2009 7:20 pm
by sarabulho
Tanks a lot men it really works.