[SOLVED] xyz rotations to match spherical coordinates?

Started by
5 comments, last by sqpat 15 years, 8 months ago
I'm writing a graphics engine that will display 2d sprites on a 3d backdrop. The way i have the camera set up, it looks at some point (x, y, z). Relative to that point, the camera's location is stored in spherical coordinates (theta, phi, rho). This allows for easy rotation and zoom around a point, just change the theta phi and rho values. However when i render my 2d sprites, i want them to always face the camera. They're textured quads, thus, they're really squares with four vertices that have xyz coordinates themselves. Because of my mixed spherical coordinate system i have no idea how to figure out the angle to allow the sprites to always face the camera. I can easily figure out the camera's position (x,y,z) as well as its direction vector (u,v,w). How do i use this to transform the world space and allow me to easily get the four vertices' coordinates? I'm pretty sure it could be done with a rotation and a translation to match up the camera to the origin or some such, but im not very good with xyz rotations. Help with this, or a different way to get the vertices needed would be very much appreciated. [Edited by - sqpat on July 27, 2008 7:49:36 PM]
Advertisement
I suggest that you look at billboarding. Billboarding uses a matrix to transform the quad so that it is facing the camera.

You could also use sprite objects if you are using DirectX (I don't know if OpenGL has this feature, but probably). In DirectX the sprites are automatically facing the camera (once again, OpenGL probably acts the same way, but don't quote me on that).

Hope this helps.
Okay. I'm looking into billboarding and it's giving me problems, but I'll keep working at it. (I think the matrix transformations are ruining everythingn on the display or something...)

The reason i didnt immediately go to the inherent directX sprite stuff is that i heard it was slow and inaccurate, which kind of scared me off. Sorry since this isnt quite the place to be asking it, but is this really the case (for directx 9)?
Inverse your ViewMatrix then Transpose it in order to have your points face the camera. Now if you want to know what the position of those points will be on screen you can always go and transform each point by the same Matrix.
I think that's what I'm doing; but is it really as simple as something like this?:



// Create billboard matrixD3DXMATRIXA16 m_matBillboard;D3DXMatrixInverse( &m_matBillboard, NULL, &m_View ); m_matBillboard._41 = charPos[x].x;m_matBillboard._42 = charPos[x].y;   // set transpose to 0 for nowm_matBillboard._43 = charPos[x].z;// Set the final matrix as the world matrixp_dx_Device->SetTransform(D3DTS_WORLD, &m_matBillboard);



m_View is a pointer to the current camera view matrix. CharPos[x] contains a vertex that maps to the point where the sprite is. This is what im doing, and what seems to be happening is that the entire world is being shifted, despite the other stuff all being drawn before this part.
You would first of all have to transpose your matrix. By transpose, we do not mean translate. There is a big difference. What you are doing is setting the translation to zero.

Transposing a matrix is different. You take each element in a matrix and exchange it with the element with the opposite position (i know this isn't clear so i'll give an example):

Original matrix :
| m11 m12 m13 m14 |
| m21 m22 m23 m24 |
| m31 m32 m33 m34 |
| m41 m42 m43 m44 |

Becomes :
| m11 m21 m31 m41 |
| m12 m22 m32 m42 |
| m13 m23 m33 m43 |
| m14 m24 m34 m44 |

Secondly, you say that the entire world is shifted. Have you reset your view matrix between renderings? If you do not specify a normal matrix for rendering (not the billboard matrix) you will get wierd results.

Now, finally, about the directX sprites. I don't know if it is slow and inaccurate in dx9 cause i never used it. In dx10, it works very well. I hardly notice any frame drop from the sprites. However, if you are worried about the speed and accuracy, you could create your own sprite class by using simple billboarded quads with a texture.
EDIT:

okay, i got it. nevermind!

[Edited by - sqpat on July 27, 2008 7:30:06 PM]

This topic is closed to new replies.

Advertisement