cam rotations

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
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

cam rotations

Post by Acki »

Hi,
I have a node and a camera (FPS, receiver disabled).
the camera rotates around the node (like moon around earth).
I have the positions and the angle of the cam to the node.
Now I want to set the rotation of the cam, so that it looks straight to the node !!!
I don't want to use setTarget, I want to calculate the angle(s) !!!

thx, Acki
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Dances
Posts: 454
Joined: Sat Jul 02, 2005 1:45 am
Location: Canada
Contact:

Post by Dances »

if you need the angles for math use setTarget anyway and then use getRotation to figure out where exactly its facing and do your math from there...
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

I have the positions and the angle of the cam to the node.
Hi,

I am not sure I understand the problem. You say you have the angles. Why can't you use 'camera->setRotation' ?

If you want to calculate the angles relative to the x, y, z axis you can do the following:

Calculate the vector nodeposition (n) minus camera position (c). This gives you a directional vector: R(Xr, Yr, Zr) = (Xn - Xc, Yn - Yc, Zn - Zc)

Rotation about x axis = arctan(Zr / Yr)
Rotation about y axis = arctan(Zr / Xr)
Rotation about z axis = arctan(Yr / Xr)

In C++ it is best to write this as atan2(Xr, Yr) (X and Y are swapped !)

These angles can be used in the 'camera->setRotation' function.
In case the camera behaves 'strangely' you are propably witnessing the gimbal lock problem.

I am sure there are other solutions, for instance using the matrix4 class
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Well, I want to create a camera like in Splinter Cell.
The cam rotation around the y axis works.
The cam rotation around the x axis works.
But both together screws the movement up !!!

Code: Select all

void setCamera(bool recalc){
  // move the camera
  static ICursorControl* curs = device->getCursorControl();
  static s32 scrCenterX = driver->getScreenSize().Width / 2;
  static s32 scrCenterY = driver->getScreenSize().Height / 2;
  static position2d<s32> mp;
  static s32 moveX, moveY;
  static float camDist2, gTob = 3.14156 / 180.0, bTog = 180.0 / 3.14156;
  static vector3df posCam(0, 5, -5), rotCam(0, 0, 0), rotPivot(0, 0, 0);
  // get mouse movements
  mp = curs->getPosition();
  moveX = scrCenterX - mp.X;
  moveY = scrCenterY - mp.Y;
  // move mouse back to center of screen
  curs->setPosition(position2d<s32>(scrCenterX, scrCenterY));
  if(moveX){
    // rotate camera around world Y axis of player center
    rotPivot.Y += moveX;
    if(rotPivot.Y >= 360.0) rotPivot.Y -= 360.0;
    if(rotPivot.Y < 0.0) rotPivot.Y += 360.0;
    recalc = true;
  }
  if(moveY){
    rotPivot.X -= moveY;
    if(rotPivot.X > 80.0) rotPivot.X = 80.0;
    if(rotPivot.X < 10.0) rotPivot.X = 10.0;
    recalc = true;
  }
  if(recalc){
    camDist2 = cos(rotPivot.X * gTob) * camDistanz;
    posCam.Y = sin(rotPivot.X * gTob) * camDistanz;
    posCam.X = cos(rotPivot.Y * gTob) * camDist2;
    posCam.Z = sin(rotPivot.Y * gTob) * camDist2;

    // here seems to be the error !?!?!
    rotCam.Y = atan2(-posCam.X, -posCam.Z) * bTog;
    rotCam.X = atan2(posCam.Y, posCam.X) * bTog;

  }
  // get the player position
  vector3df posPlayer = player->getPosition();
  Cam->setPosition(posPlayer + posCam);
  Cam->setRotation(rotCam);
}
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
evolutional
Posts: 16
Joined: Mon Apr 11, 2005 2:32 pm
Location: LEEDS, England
Contact:

Post by evolutional »

Acki wrote:Well, I want to create a camera like in Splinter Cell.
The cam rotation around the y axis works.
The cam rotation around the x axis works.
But both together screws the movement up !!!
Sounds like a case of Gimbal lock to me. I've not used them (yet), but it should be possible to create a quaternion-based camera and transform the orientations from the quat to use in your camera. There's a tutorial on GameDev.net that might help you.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Well, I now created a second camera (inactive) set the position to the first cam.
Then I set the target of the 2nd cam and get their rotation for the first cam...

The problem is, if I use the setTarget on the first cam and poth (cam and node) are moving the cam is flipping up and down.... :shock:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

This seems strange:

Code: Select all

rotCam.X = atan2(posCam.Y, posCam.X) * bTog; 
I would expect Y and Z as parameters:

Code: Select all

rotCam.X = atan2(posCam.Y, posCam.Z) * bTog; 
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

No, Y and X are correct !!!
It calculates the up/down look of the cam...
It depends on the coordinate system of Irrlicht:

Code: Select all

Y
       Z
|    /
|   /
|  /
| /
|/____________ X
for example 3DSMax uses this system:

Code: Select all

Z
       Y
|    /
|   /
|  /
| /
|/____________ X
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply