Project 3D position on GUI
by AIDan · in Torque X 3D · 05/21/2009 (9:01 am) · 3 replies
I am trying to place a HUD element at the position of a 3D object. This is what I do in a GUISceneView:
CameraComponent camera= (CameraComponent)Camera;
Matrix m= Matrix.CreateTranslation(camera.SceneGroup.Position) * Camera.SceneGraph.CurrentSceneRenderState.View * Camera.SceneGraph.CurrentSceneRenderState.Projection;
Vector3 pos= Vector3.TransformNormal(objectWorldPos, m);
objectHUDPos= new Vector2(pos.X, pos.Y);
I am not sure why this does not work. Maybe someone can give me a hint. Thx
CameraComponent camera= (CameraComponent)Camera;
Matrix m= Matrix.CreateTranslation(camera.SceneGroup.Position) * Camera.SceneGraph.CurrentSceneRenderState.View * Camera.SceneGraph.CurrentSceneRenderState.Projection;
Vector3 pos= Vector3.TransformNormal(objectWorldPos, m);
objectHUDPos= new Vector2(pos.X, pos.Y);
I am not sure why this does not work. Maybe someone can give me a hint. Thx
About the author
#2
This is the jist of it and there might be some other code missing. I haven't tried compiling the code as it's shown above, but if there are problems post back.
John K.
www.envygames.com
05/21/2009 (10:32 am)
Try something like this in the Game.cs file...Vector3 posLabel; //3D position of the label
T3DSceneComponent compSceneLabel; //target
protected override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
//need the scene component of the target scene object
compSceneLabel = objItemLabeled.Components.FindComponent<T3DSceneComponent>();
//translate the 3D world coords into 2D screen coordinates
posLabel = GFXDevice.Instance.Device.Viewport.Project(
compSceneLabel.Position,
Camera.SceneGraph.CurrentSceneRenderState.Projection,
Camera.SceneGraph.CurrentSceneRenderState.View,
Camera.SceneGraph.CurrentSceneRenderState.World.Top);
//create the label to show (just showing the position)
strLabelCoords = "(" + compSceneLabel.Position.X.ToString("0:0") + "," + compSceneLabel.Position.Y.ToString("0:0") + "," +
compSceneLabel.Position.Z.ToString("0:0") + ")";
//use the fontrenderer to draw the text to the screen
FontRenderer.Instance.DrawString(spriteFont, new Vector2(posLabel.X + 1f, posLabel.Y + 1f), Color.Black, objItemLabeled.Name + "n" + strLabelCoords, "");
FontRenderer.Instance.DrawString(spriteFont, new Vector2(posLabel.X, posLabel.Y), Color.White, objItemLabeled.Name + "n" + strLabelCoords, "")
}; This is the jist of it and there might be some other code missing. I haven't tried compiling the code as it's shown above, but if there are problems post back.
John K.
www.envygames.com
#3
05/21/2009 (10:48 am)
Thank you both. The second code instantly worked.
Associate Orion Elenzil
Real Life Plus
i wrote pretty much exactly this over in the TGE side of the world, the resource is here. even though it's for a different engine, it's only a couple hundred lines of source and might give you some ideas.