Torque X 3D Projection/View Matrices
by T. Chamill · in Torque X 2D · 03/04/2008 (2:25 pm) · 4 replies
I've been attempting to do some ray casting in 3D using the Unproject method (GraphicsDevice.Viewport.Unproject) and it requires a projection and view matrix. According to the Torque X API these matrices are available from the gui canvas using the property RenderState (GUICanvas.Instance.RenderState), and they are...but they always consist of 0's and nothing else. Going off of the (very) little I know about XNA 3D rendering, these matrices should at the very least change as the camera is moved, but they don't.
Does anyone know where in the engine I can get the versions of these matrices that the engine is using, or, if the engine is actually using the ones available in RenderState, why they are all 0's?
Does anyone know where in the engine I can get the versions of these matrices that the engine is using, or, if the engine is actually using the ones available in RenderState, why they are all 0's?
#2
Thanks, the T3DCameraComponent did have a property called Transform, and it returns a matrix with values besides 0 in it.
The bad news is that it doesn't fully solve the problem. I have to pass this matrix as both the projection and view matrix for the Unproject method, as it is the only matrix I have, and the end result for the ray is inconsistent with the screen (e.g. the ray comes out as projecting from x=-2000 when the point it should be projecting from is x=1000).
If someone doesn't mind glancing over the code, to see if I've messed up my math at all, I've added it here.
03/06/2008 (4:06 pm)
John,Thanks, the T3DCameraComponent did have a property called Transform, and it returns a matrix with values besides 0 in it.
The bad news is that it doesn't fully solve the problem. I have to pass this matrix as both the projection and view matrix for the Unproject method, as it is the only matrix I have, and the end result for the ray is inconsistent with the screen (e.g. the ray comes out as projecting from x=-2000 when the point it should be projecting from is x=1000).
If someone doesn't mind glancing over the code, to see if I've messed up my math at all, I've added it here.
private Ray ComputeRay(int x, int y)
{
GarageGames.Torque.T3D.T3DSceneGraph graph = (GarageGames.Torque.T3D.T3DSceneGraph)TorqueObjectDatabase.Instance.FindObject<GarageGames.Torque.T3D.T3DSceneGraph>("SceneGraph");
Matrix projectionViewMatrix = graph.Camera.Transform;
Vector3 nearsource = new Vector3(1000, 1000, 0);
Vector3 farsource = new Vector3(1000, 1000, 1);
Matrix world = Matrix.CreateTranslation(0, 0, 0);
Vector3 nearpoint = Game.Instance.GraphicsDeviceManager.GraphicsDevice.Viewport.Unproject(
nearsource, projectionViewMatrix, projectionViewMatrix, world);
Vector3 farpoint = Game.Instance.GraphicsDeviceManager.GraphicsDevice.Viewport.Unproject(
farsource, projectionViewMatrix, projectionViewMatrix, world);
Vector3 direction = farpoint - nearpoint;
direction.Normalize();
return new Ray(nearpoint, direction);
}
#3
John K.
03/06/2008 (8:27 pm)
Hi Greth, I forgot to ask what are you trying to do with the ray casting? There might be a massively easier way to do this. From your code sample, it looks like you just want a vector that represents a line from the camera to the given point, based on an X,Y screen coordinite. If you can describe your goal a bit, I might have some clearer ideas.John K.
#4
Sorry for not being very clear with what the intent of the code was. I'm trying to do object selection in the 3D world using the mouse, using the ray for intersection testing with game world objects. So far, I've been following the msdn tutorial for XNA that covers this (http://msdn2.microsoft.com/en-us/library/bb203905.aspx), but if you know a faster / cleaner way to do it, I'm happy to go for it.
03/06/2008 (8:42 pm)
Hey John,Sorry for not being very clear with what the intent of the code was. I'm trying to do object selection in the 3D world using the mouse, using the ray for intersection testing with game world objects. So far, I've been following the msdn tutorial for XNA that covers this (http://msdn2.microsoft.com/en-us/library/bb203905.aspx), but if you know a faster / cleaner way to do it, I'm happy to go for it.
Associate John Kanalakis
EnvyGames
John K.