World Units
by David Everhart · in Torque X 2D · 12/10/2007 (9:09 am) · 5 replies
I have been through quite a few resources (threads, articles) and understand the value of using world units. However, I have not figured out what the conversion ratio is just yet. I have read some say to base it on the viewport, but then that becomes camera dependent. What does a world unit correspond to in regards to what is displayed on the screen? Is it viewport dependent, resolution dependent? Any help would be appreciated, thanks.
#2
12/10/2007 (7:56 pm)
Hmm, Ill try that out. I have a 165 x 165 pixel image, but wasnt sure how to convert to world units. Ill give it a whirl and let you know how it turns uot. Thanks josh!
#3
01/27/2008 (3:24 pm)
Works like a champ Josh. Below is my slightly modified version and the code that uses it, but it calculates the angle perfectly. private void OnMousebuttonClicked(float buttonstate)
{
MouseState state = Mouse.GetState();
if (buttonstate == 1) // Means button was pressed
{
Vector2 mouseCoordinates = ConvertScreenToWorld(new Vector2((float)state.X,(float)state.Y));
float angle = T2DVectorUtil.AngleFromTarget(mouseCoordinates, Player.Instance.Sprite.Position);
float distance = Vector2.Distance(mouseCoordinates,Player.Instance.Sprite.Position);
// This is a test, now that the Angle is calculated correctly, can implement a angle to Enumeration.Direction function
Player.Instance.Direction = Arillian.Common.Enumeration.Direction.SouthWest;
Player.Instance.UpdateAnimation();
}
}
protected Vector2 ConvertScreenToWorld(Vector2 coordinates)
{
// 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, GameData.Instance.ScreenResolution);
// Return the world position
return Vector2.Multiply(relativePosition, currentCamera.Extent) + currentCamera.SceneMin;
}
#4
01/28/2008 (4:18 pm)
Glad to help.
#5
OK, so I have implemented the above code into my test app and my preoblem is this:
When trying to capture mouseEvent info, the X,Y coords are DIFFERENT whenever you move the game window around!!! Example: I start the sample game (game window displays center of computer screen) and click the most top-left area of the screen and get a value of something like 2.299999, BUT when I move the game window around (lets say to the top-left corner of my computer screen, the mouse coords COMPLETELY change!!??? At that point I return a value of something like -49.02929292?????
PLEASE HELP!!
thanks,
04/24/2008 (9:05 am)
First of all: could any replies to this be forwarded to monty@phoenixortho.netOK, so I have implemented the above code into my test app and my preoblem is this:
When trying to capture mouseEvent info, the X,Y coords are DIFFERENT whenever you move the game window around!!! Example: I start the sample game (game window displays center of computer screen) and click the most top-left area of the screen and get a value of something like 2.299999, BUT when I move the game window around (lets say to the top-left corner of my computer screen, the mouse coords COMPLETELY change!!??? At that point I return a value of something like -49.02929292?????
PLEASE HELP!!
thanks,
Torque Owner Joshua A. Thomas
protected Vector2 _ConvertScreenToWorld(Vector2 coords) { T2DSceneCamera cam = T2DSceneGraph.Instance.Camera as T2DSceneCamera; Vector2 relativePos = Vector2.Divide(coords, _screenSize); return Vector2.Multiply(relativePos, cam.Extent) + cam.SceneMin; }XNA's X and Y of the mouse state are passed. They are relative to the upper left of the window. I divide those by the size of the client window to get X and Y values between 0 and 1, which I then scale by the camera extent and add to the minimum position camera position.
Is this what you are looking for, or are you trying for the opposite of this?