Another Math Question
by Sean T. Boyette · in Torque X 2D · 11/16/2008 (8:06 pm) · 4 replies
Ok, got another nice one for anyone who may be able to solve it.
I want to take the postion of a character
_player1.Components.FindComponent().Postion
and convert that to a 2D coordinate on the screen.
I know that it has to do something with converting the coordinates using the cam.
any ideas?
Sean
I want to take the postion of a character
_player1.Components.FindComponent
and convert that to a 2D coordinate on the screen.
I know that it has to do something with converting the coordinates using the cam.
any ideas?
Sean
About the author
#2
12/18/2008 (7:06 pm)
[b]Viewport.Project()[/b]
Projects a 3D vector from object space into screen space.
[b]Parameters[/b]
[i]source[/i]
The vector to project.
[i]projection[/i]
The projection matrix.
[i]view[/i]
The view matrix.
[i]world[/i]
The world matrix.
[b]Return Value[/b]
The vector in screen space.public Vector3 Project(Vector3 source, Matrix projection, Matrix view, Matrix world)
{
Matrix matrix = Matrix.Multiply(Matrix.Multiply(world, view), projection);
Vector3 vector = Vector3.Transform(source, matrix);
float a = (((source.X * matrix.M14) + (source.Y * matrix.M24)) + (source.Z * matrix.M34)) + matrix.M44;
if (!WithinEpsilon(a, 1f))
{
vector = (Vector3) (vector / a);
}
vector.X = (((vector.X + 1f) * 0.5f) * this.Width) + this.X;
vector.Y = (((-vector.Y + 1f) * 0.5f) * this.Height) + this.Y;
vector.Z = (vector.Z * (this.MaxDepth - this.MinDepth)) + this.MinDepth;
return vector;
}
#3
12/19/2008 (6:26 am)
Ahh, T3D, I thought it read T2D. You should slap that into the TDN Takuan, point conversion seems to be a common request.
#4
12/20/2008 (7:36 pm)
Thanks Takuan - I will ahve to check that out!
Torque Owner David Everhart
public static Vector2 ConvertScreenToWorld(Vector2 coordinates,Vector2 screenResolution) { // Credit to Joshua A. Thomas for this function :D // Get the Current Camera T2DSceneCamera currentCamera = (T2DSceneCamera)TorqueObjectDatabase.Instance.FindObject("Camera"); // Figure out the relative Position Vector2 relativePosition = Vector2.Divide(coordinates,screenResolution); // Return the world position return Vector2.Multiply(relativePosition, currentCamera.Extent) + currentCamera.SceneMin; }