# How to rotate view point around x axis properly??

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

## Recommended Posts

Hi, I'm trying to rotate the view point of my camera around the vector (50,50,50), at a distance of 49. So I'm doing this: float r = 2.2f; viewPoint.z = cos(r)*49 + 50; viewPoint.y = sin(r)*49 + 50; And my up vector for my view matrix is (0,1,0) using the lh functions, left hand i think that stands for, with the axis's being like this: y z | / |/ |___x I've got it rotating horizontally fine, but i think the up vector is mucking it up, that is when I rotate the screen vertically, it flip flops the screen. So I guess I gotta be changing the up vector aswell as the viewpoint for it to rotate properly. Any idea how? Thanks

##### Share on other sites
I must tell I got lost in your problem... You're trying to rotate around axis (50,50,50)? It's the same as axis (1,1,1). So I don't understand. Please, add some more info (or write, by the numbers, what RU trying to do) - maybe I could help you. Greetz.

##### Share on other sites
I'd hav a look at this guy's work, on a D3DSmartCamera..... its very good!

http://www.gamedev.net/community/forums/topic.asp?topic_id=301805

SHould have as much camera manipulation in there as you can shake a stick at!

Si

##### Share on other sites
It is not so clear. Based on your equations, it seems you would like to rotate the view point around an axis that passes by the point (50,50,50) and has the x direction, correct?

Well, the pUp and (plookAt - pEye) vectors are used to generate a coordinate system for the camera. The x-axis's orientation of this coordinate system depends the angle between pUp and (plookAt - pEye), because a cross product always considers the smaller angle between two vectors to generate the 3th vector. In other words, on a cross product C = AxB, when you apply the left-handed rule (from A to B) to determine the direction/orientation of C (by the thumb), you always go on the smaller angle between A and B.

So, the problem in keeping the pUp vector constant (like you shown) is: If you change the (plookAt - pEye) direction, the camera coordinate system will be inverted when the angle between pUp and (plookAt - pEye) > 180 degrees.

To avoid this, you must update the pUp vector when the (plookAt - pEye) direction is changed. If you know (stored) the camera's x-axis, you could do:

D3DXVec3Cross( &pUp, &(plookAt - pEye), &Xaxis );

Then use pUp, plookAt and pEye to create the view matrix.

##### Share on other sites
Sorry for my bad explanation, just to quickly update what it is now.

I want to rotate around the origin, on both the x and y axis's at a distance of 10;

So I'll be calculating the rotating around x and y axis's, I'm am yet to work out the exact calculations for this, but thats fine I can do that, the problem is calculating the lookUp vector when the viewPoint spins upside down.

------------------------------

Ok so I need to calculate the up vector from the viewPoint and viewAt, but I don't totally get what the values you entered into the function D3DXVec3Cross.

Like
Quote:
 D3DXVec3Cross( &pUp, &(plookAt - pEye), &Xaxis );

Fine I put in the pUp vector as the 'outvector', but I don't why you are doing the 'plookAt - pEye', Xaxis, how do I get the camera's Xaxis, and why only the Xaxis?

Thanks

sipickles Im looking at that code, but I'm finding it hard to understand what to lookat.

##### Share on other sites
Quote:
 I want to rotate around the origin, on both the x and y axis's at a distance of 10;

Mathematically, we usually define a rotation around an axis (not around a point). This can confuse us. We are going for steps, OK?! [smile]. Firstly, let me understand exactly what you would like to do. I suppose you want the camera's eye position rotate spherically around the origin. So, your camera's target would be at the origin. Is it correct? If it is not correct, where is the camera's target?

##### Share on other sites
Yes that is exactly it. But for now all I need to do is rotate the viewPoint around the x axis.

Thanks

##### Share on other sites

[edit: snip]

you need to modify your up vector as your lookat vector approaches the world up vector. You can cross the current lookat vector with the current right vector to get this.

##### Share on other sites
so from adriano_usp's code is it this:

D3DXVECTOR3 point(0,0,10);
D3DXVECTOR3 at(0,0,0);
D3DXVECTOR3 up;
D3DXVec3Cross(&up, &(look - point), &D3DXVECTOR3(0,0,1));

?

Thanks

##### Share on other sites
Quote:
 Yes that is exactly it. But for now all I need to do is rotate the viewPoint around the x axis.

OK, now is easier [smile]:

// Inputs
if ( Left == TRUE )
x -= 0.01f;

if ( Right == TRUE )
x += 0.01f;

if ( Up == TRUE )
y += 0.01f;

if ( Down == TRUE )
y -= 0.01f;

D3DXVECTOR3 point( 0.0f, 0.0f,10.0f );
D3DXVECTOR3 at( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 up( 0.0f, 1.0f, 0.0f );

D3DXMATRIX RotX, RotY, RotFinal;
D3DXMatrixRotationX( &RotX, y );
D3DXMatrixRotationY( &RotY, x );

RotFinal = RotX * RotY;

D3DXVec3TransformCoord( &point, &point, &RotFinal );

// Now the key: We also transform the up vector to avoid the "screen flip"
D3DXVec3TransformCoord( &up, &up, &RotFinal );

// Creating the view matrix
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &point, &at, &up );
pDevice->SetTransform( D3DTS_VIEW, &matView );

// Creating the projection matrix
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, ... , ..., ... , ... );
pDevice->SetTransform( D3DTS_PROJECTION, &matProj );

Notice that this solution rotates the camera's eye around the X and Y axes from the world coordinate system. To rotate it around the camera's axes, you need to create a local coordinate system for the camera and using D3DXMatrixRotationAxis function [wink].

1. 1
Rutin
25
2. 2
3. 3
4. 4
5. 5

• 9
• 10
• 13
• 19
• 14
• ### Forum Statistics

• Total Topics
632944
• Total Posts
3009356
• ### Who's Online (See full list)

There are no registered users currently online

×