Click to See Complete Forum and Search --> : DirectX - 3rd person camera like homeworld


^Johnny2Bad
February 27th, 2005, 06:38 AM
Hi,
I am a newbie with directx and this forum so please be patient with me.

I am trying to implement a 3rd person camera that is like the one used in Homeworld ie rotates around latitude and longitude of a given point and always faces that point.

I know that this can be done using matrix math (which I happen to be pretty bad at), I have attempted to cheat my way around it but I end up with movement problems. So I have given up and want to use Matrices.

Has anybody got any idea where I should start? Has anybody implemented such a camera before and is willing to share their source code? Or has someone got a reference to some code that will show me the math for this algorithm?

Thanking you in advance,
^Johnny2Bad.

Elementer
February 27th, 2005, 08:21 AM
Try tutorials on the web, there are ton of them, for example this can be useful, there are some camera lessons:

http://www.gametutorials.com/gtstore/c-5-directx.aspx

Cheers :)

Ejaz
February 28th, 2005, 04:49 AM
See if you can tailor this (http://www.codeguru.com/Cpp/G-M/directx/directx8/article.php/c8279/) :thumb:

^Johnny2Bad
March 3rd, 2005, 08:03 AM
Ejaz,
Thankyou for your code it has given me inspiration to start building a flight simulator however I couldn't get it to do the 3rd person homeworld type camera.

What I did was I thought pretty straight forward...

D3DXVECTOR3 v_Target(0,0,0); //object is at world origin

//Camera
TheCamera.SetPos(v_Target); // Move to location of object being rotated around
TheCamera.Pitch(m_Alpha); // Rotate pitch by a certain amount
TheCamera.Move(-100); // move back 100 units

// Update the view matrix representing the cameras
// new position/orientation.
D3DXMATRIX V = *TheCamera.GetViewMatrix();
DXDevice->SetTransform(D3DTS_VIEW, &V);

Now what happens is the object appears to rotate around the camera rather than what is required. i.e camera rotating around the object. Have u any ideas on why this is occuring?

I also believe that I'm probably going about this the wrong way and should be concentrating on matrix math to solve the problem rather than trying to cheat my way by moving to the objects location and then changing pitch etc and moving backwards.

Ejaz
March 3rd, 2005, 09:03 AM
Ok, here you go. Attached is a camera class for both FPS and Flight Sim. Have fun, and btw, don't give me credit for that, it wasn't written by me :p

Notsosuperhero
March 4th, 2005, 01:01 PM
If you go to gametutorials.com, alot of the tutorials aren't free anymore :cry:

^Johnny2Bad
March 4th, 2005, 03:03 PM
Okay guys...
I did pay for a 3rd person camera from that game dev site and it turned out to be unsuitable as it did not rotate correctly. There goes $5. Although I'm sure if I looked at the code a bit more I might be able to do something about that, but as I have said matrix math is not my thing.

Anyhow I decided to use polar co-ordinates and trigonometry to try and solve the problem and it nearly works. The one problem is when the angle alpha is close to 90 degrees the rotation goes mongy and the program decides to rotate about the incorrect axis.

Anyhow here is the piece of code to convert sperical co-ordinates to cartesian

void CCamera::GetCartCords(D3DXVECTOR3* v_Pos)
{
float x,y,z,l;

if (Alpha>1.5708f)
{
D3DXVECTOR3 temp(0,-1,0);
v_Up=temp;

Beta=Beta+D3DX_PI;
if (Beta>D3DX_PI*2)
Beta=Beta-D3DX_PI;
Alpha=D3DX_PI-Alpha;
}else{
D3DXVECTOR3 temp(0,1,0);
v_Up=temp;
}
y=Radius*sin(Alpha);

l=sqrt(Radius*Radius-y*y);

z=l*sin(Beta);
x=l*cos(Beta);

D3DXVECTOR3 temp(x,y,z);
*v_Pos=temp;
}

and the results are pumped into...

D3DXVECTOR3 v_Target(0,0,0);
D3DXVECTOR3 v_Pos;
D3DXVECTOR3 Up;
D3DXMATRIX m_View;


TheCamera.GetCartCords(&v_Pos);
Up=TheCamera.v_Up;

D3DXMatrixLookAtLH(&m_View, &v_Pos, &v_Target, &Up);
DXDevice->SetTransform(D3DTS_VIEW , &m_View);

then follows the directx scene and rendering of objects.

Any ideas people?