#### Archived

This topic is now archived and is closed to further replies.

# Pitch camera or rotate by x-axe

This topic is 5686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all, I got a problem which is difficult to explain: I want to rotate my cam around y and x axes. The rotation by y-axe is solved. I go like this: - first I get the direction via "view_look - view_start" in "direction" - after that I normalize the vector and rotate it by "D3DXMatrixRotationY" with an angle of 90 degrees (so I get the vector to the side :-) - I transform it with my "direction" and then move in given steps into that direction - after all I add the new "direction"-vector to the old "view_look" and "view_start" and it turns around itself :-) So far so good ... I tried that with the x-axe too, to pitch the camera, but everytime it reaches the bottom or the top of the world, it turns around and inverts its view, so that on the other side (in the back) the camera is not upside down but normal ... I hope I could explain this with my slight english-knowledge and anyone could help me out. Have a nice day, :-) Python Spin the wheel of fortune - or learn to navigate.

##### Share on other sites
The problem you are having is that you are not actually always rotating on the x -axis, you want to rotate on the right vector of the camera.

The way I do it in my engine is to calculate what the new view vector is and then build the matrix using a function similar to D3DXMatrixLookAtLH to form the matrix( I wrote my own version since i have the option to use DX or OpenGL). To calculate the new view vector I use:

  // Find the direction we are facing        Vector3d View = (_view - _position);        float cosTheta = cos(amount);        float sinTheta = sin(amount);        float x, y, z;        x = _strafe.X;        y = _strafe.Y;        z = _strafe.Z;        // Find the new x position for the new rotated point        NewView.X = (cosTheta + (1 - cosTheta) * x * x) * View.X;        NewView.X = NewView.X + ((1 - cosTheta) * x * y - z * sinTheta) * View.Y;        NewView.X = NewView.X + ((1 - cosTheta) * x * z + y * sinTheta) * View.Z;        // Find the new y position for the new rotated point        NewView.Y = ((1 - cosTheta) * x * y + z * sinTheta) * View.X;        NewView.Y += (cosTheta + (1 - cosTheta) * y * y) * View.Y;        NewView.Y += ((1 - cosTheta) * y * z - x * sinTheta) * View.Z;        // Find the new z position for the new rotated point        NewView.Z = ((1 - cosTheta) * x * z - y * sinTheta) * View.X;        NewView.Z += ((1 - cosTheta) * y * z + x * sinTheta) * View.Y;        NewView.Z += (cosTheta + (1 - cosTheta) * z * z) * View.Z;        _view = _position + NewView;

##### Share on other sites
I''ll have a look on it .... btw: I meant that I calculate only the look-vec, the start-vec stays the same ... :-)

Spin the wheel of fortune - or learn to navigate.

##### Share on other sites
Sorry forgot to mention you find the strafe vector by taking the cross product of the view direction and the Upvector of the world.

##### Share on other sites
This may be my problem. Could you explain the use of the upvector in that special case?

:-) Python

Spin the wheel of fortune - or learn to navigate.

##### Share on other sites
I just always use the constant upvector of (0, 1, 0) since y is up in my world. Unless you want to be able to roll and fly upside down like you would be able to in a plane. Just make sure that your view vector is a unit vector before taking the cross product. The cross product will return a unit vector in the direction of the right of the right vector.

Then just plug it in to the code I posted earlier.

I had a lot of trouble getting this right!

##### Share on other sites
You will also want to keep track of how far up or down you are looking, if you don''t want the camera to spin right round. I use something like this at the beg. of my function:

  _rotupdown += amount;if(_rotupdown >= 170.0f || _rotupdown <= -170.0f){       return;}

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 20
• 12
• 13
• 14
• 80
• ### Forum Statistics

• Total Topics
632144
• Total Posts
3004406

×