3D Camera rotation around any object in 3D space
Author Message 3D Camera rotation around any object in 3D space

Hi

I use { }  for a column vector, < > for a line vector and [  ]  for a matrix.

Assuming your observer is on the ray between the center of the circle of the trajectory, < Po > and
the point we look at, < Pv >, such that

< observer > =   < Po >   +  lambda  < Pv - Po >

then, after the rotation, the following equations seem to satisfy our requirements:

< P'v >  =  <  Po >  +  < Pv - Po > [ rotation ]

< observer'  > =   < Po >   + lambda   < P'v - Po >

Take care to well defined the matrix of rotation, [rotation], as a matrix of rotation around a
DIRECTION, using D3DXMatrixRotationAxis to get that matrix. For the axis, the direction around
which the observer turn, it is defined as the vectorial product between Po  and Pvo  (Pv  at the
time 0), and so, you can use D3DXVec3Cross to get it.

Unless you try to optimize the number of frames, I will leave the world transform as an identity
matrix, and would just change the projection matrix.

Hoping it may help,
Vanderghast, Access MVP

Quote:

> Does anybody cares to explain me how to implement camera rotation around any
> object in 3D space !?
> For example : imagine the planets in solar system, you set the camera to
> some planet at point Po(200, 100, 500) and then rotate around this selected
> object in any direction (at the same distance of course around any axis).
> I've found some simple code to implement that but it gives me very bad
> result when the selected object is not in origin (0,0,0). All matrix
> rotations and transformations in World space are performed around origin at
> (0,0,0). So if I need to rotate around an object which is not in origin I
> firstly need to translate the object to origin, do the rotation around some
> axis and then rollback the translation. When I do that, result is still OK
> for object in origin (because there is no translation ;-)), but for object
> not in origin it is still wrong ! Why ?

> Complete code is rather big so I'll put just the RotateAroundY() axis
> snippet; it goes something little like this :

> Dim m_LookAt as D3DVECTOR '// Where the camera is looking at, like
> (10,15,10)
> Dim m_CameraAt as D3DVECTOR '// Camera current position , like (-15,20, -10)

> Sub RotateAroundY(Angle as Single)
>     Dim matTemp as D3DMATRIX
>     Dim matTemp1 as D3DMATRIX
>     '
>     Call D3DXMatrixIdentity(matTemp)
>     Call D3DXMatrixIdentity(matTemp1)
>     '
>     '// Do the translation from m_LookAt
>     Call
> D3DXMatrixTranslation(matTemp, -m_lookAt.x, -m_lookAt.y, -m_lookAt.z)
>     '
>     '// Do the rotation around Y axis
>     Call D3DXMatrixRotationY(matTemp1, Angle)
>     '
>     '// Multiply matrices
>     Call D3DXMatrixMultiply(matTemp1, matTemp, matTemp1)
>     '
>     '// Do the translation rollback from m_LookAt
>     Call D3DXMatrixIdentity(matTemp)
>     Call D3DXMatrixTranslation(matTemp, m_lookAt.x,m_lookAt.y,m_lookAt.z)
>     '
>     '// Multiply matrices
>     Call D3DXMatrixMultiply(matTemp, matTemp1, matTemp)
>     '// Change the camera coordinates
>     Call D3DXVec3TransformCoord(m_CameraAt, m_CameraAt, matTemp)
> End Sub
> '
> After all that I call D3DMatrixLookAtLH ... blah, blah , and SetTransform
> for VIEW matrix
> '
> Any idea ? What am I doing wrong ? Some tutorials on that ? I did not find
> anything realy usefull in the web eather.

> Bigy

Sun, 05 Jun 2005 04:03:33 GMT  3D Camera rotation around any object in 3D space
Hi,

I was wrong about the axis around which the observer turn. It is not defined as the cross product
of < Po >  and < Pv >   but as the cross product of two points you will look at, < Pvo >   and < Pv
1 > .

You should also have noted that lambda values are in the range 0.0  to 1.0, probably much closer to
0.0 than to 1.0, and define the "radius"  of the circular trajectory made by the observer, turning
around  < Po >.

Vanderghast, Access MVP

Quote:

> Hi

> I use { }  for a column vector, < > for a line vector and [  ]  for a matrix.

> Assuming your observer is on the ray between the center of the circle of the trajectory, < Po >
and
> the point we look at, < Pv >, such that

> < observer > =   < Po >   +  lambda  < Pv - Po >

> then, after the rotation, the following equations seem to satisfy our requirements:

> < P'v >  =  <  Po >  +  < Pv - Po > [ rotation ]

> < observer'  > =   < Po >   + lambda   < P'v - Po >

> Take care to well defined the matrix of rotation, [rotation], as a matrix of rotation around a
> DIRECTION, using D3DXMatrixRotationAxis to get that matrix. For the axis, the direction around
> which the observer turn, it is defined as the vectorial product between Po  and Pvo  (Pv  at the
> time 0), and so, you can use D3DXVec3Cross to get it.

> Unless you try to optimize the number of frames, I will leave the world transform as an identity
> matrix, and would just change the projection matrix.

> Hoping it may help,
> Vanderghast, Access MVP

Sun, 05 Jun 2005 19:38:52 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages