# Help, how to calculate a coordinate

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

## Recommended Posts

I'm not to good at maths so please bear with me. I've managed to program a 3d game with very poor maths skills but I'm a bit stuck now. I need to keep a 3d object infront of the camera no matter where it looks. I can get it working in 2 dimensions by working out the polar coordinates by treating the camera as center and giving the object a radius from the camera. Can anyone give me a simple programable formula that takes a cameras x,y,z coordinate a distance from the camera to the object, and pitch and rotation of the camera. I also know the cameras look at coordinates if that helps. Thanks people.

##### Share on other sites
http://en.wikipedia.org/wiki/Spherical_coordinates#Coordinate_system_conversions

##### Share on other sites
Have you tried NeHe? It has tutorials for the very thing you're trying to do.

##### Share on other sites
the camera view matrix keeps information about the camera orientation. Something like this:

| X.x  Y.x  Z.x 0 || X.y  Y.y  Z.y 0 || X.z  Y.z  Z.z 0 || P.x  P.y  P.z 1 |

This matrix defines the camera's local coordinate system. the Z axis points in the direction you're looking. You can get the camera position and sum with the Z axis multiplied by some scalar value to put the object at a specific distance. you can use the top left 3x3 sub-matrix of this matrix to orient your object if you want its orientation to be always aligned with the camera. to displace it in front of the camera use the X and Y axis of the camera local coordinate system. Hey, correct me if Im wrong expert guys! and I hope this helps . . .

##### Share on other sites
Thanks guys for the help. Unfortunately none of it worked for me.
Turns out the firstperson camera class I'm using from the DX9 sdk doesn't
store all the angles. Only pitch. Yaw doesn't work and the other one the rotation angle is not there either. Here's my code so far:-

x=g_Camera.m_vEye.x;
y=g_Camera.m_vEye.y;
z=g_Camera.m_vEye.z;
th = -g_Camera.m_fCameraPitchAngle;

object3d_setangle(obj_gmsframe,g_Camera.m_fCameraPitchAngle,0,0);
object3d_setposition(obj_gmsframe,x,y,z);

This works great for tipping the camera up and down and keeps the object nice and square infront of the camera. Without knowing pitch and rotation as a radian I can't complete this problem. How do I calculate rotation around y and z axis from two vectors? The lookat vector and the eye vector.

I might need to do it all with matrices but this is a last resort as it may as well be hebrew to me.

If I do use matrices, which one from the camera class should I use? The view or projection matrix?

##### Share on other sites
Why don't you just draw these objects without multiplying by the view-matrix?

All your objects you draw, you do by multiplying the vertex positions by the WorldViewProjection Matrix, while the View Matrix just is the invers of the Worldtransformation Matrix of the "camera".
So if you multiply just by the WorldProjection matrix for these objects instead of the WorldViewProjection (you can just set the View Matrix to the identity matrix before you draw), then your objects will stick to your camera and you don't have to move them arround in 3d space!

##### Share on other sites
Quote:
 Original post by genesysWhy don't you just draw these objects without multiplying by the view-matrix?All your objects you draw, you do by multiplying the vertex positions by the WorldViewProjection Matrix, while the View Matrix just is the invers of the Worldtransformation Matrix of the "camera".So if you multiply just by the WorldProjection matrix for these objects instead of the WorldViewProjection (you can just set the View Matrix to the identity matrix before you draw), then your objects will stick to your camera and you don't have to move them arround in 3d space!

Thanks for that. I've just solved it, I think it is the way you just described but my poor matrix knowledge means I'm not sure. Here is my working code to help anyone else in a fix in the future....
 	  pd3dDevice->SetTransform( D3DTS_VIEW,       (D3DXMATRIX *)&mat_view );	  pd3dDevice->SetTransform( D3DTS_PROJECTION, (D3DXMATRIX *)&mat_projection );	  pd3dDevice->SetTransform(D3DTS_WORLD,&optr->mesh->m_matWorld); 	  render_savestates();                                       /* save render states */	  render_setalphablend(TRUE);				     /* enable alpha blending */	  render_setblendmode(BLEND_SRCALPHA,BLEND_INVSRCALPHA);     /* set both source and inverse blending modes */	  optr->mesh->Render(pd3dDevice,TRUE,TRUE);		     /* render it */	  render_restorestates();                                    /* restore render states */

All I ended up having to do was add the first two lines. Delete them and the object gets rendered seperately where ever you want in the scene. Add them and it's stuck like glue to the screen wherever the camera moves. Thanks everyone.

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009410
• ### Who's Online (See full list)

There are no registered users currently online

×