Game Development Community

Mouse Position Problem

by Viktor Rumanuk · in Torque X 2D · 08/12/2008 (10:18 pm) · 2 replies

I am snapping an object to the mouse position and find that as I move my mouse lower down the screen my object's Y position becomes off. Everything is fine at the top of the screen and the X position is always correct. The object's Y position gradually moves upwards away from the mouse pointer as I move it down. I'm pretty sure that I'm converting between screen and world coordinates correctly, but that could be the problem. Any suggestions?
Thanks, Viktor

#1
08/13/2008 (12:48 am)
I'm confident that the engine is working correctly. I had to convert the Torque X 2D coordinates (origin in the center) to XNA coordinates (origin at the top-left) and back... not to mention the different units, whereby Torque X 2D has a width of 100 and height of 75 as opposed to XNA's (at the time) 1024 width and 768 height. And even these values change when your aspect ratio is set to 16:9. I eventually got it working at some point ;) If you want to post your conversion code, I'd be happy to take a peak and offer some input. I can try to hunt down my conversion code, but it may take a long while to find it among my nearly 100 Torque X project files (yep, I'm a pack-rat with my old project files).

John K.
#2
08/13/2008 (9:57 am)
Here is my conversion code, stolen off of some other thread.
public Vector2 GetWorldCoordinates(Vector2 screenPosition)
        {
            T2DSceneCamera camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>();
            Vector2 vect1 = new Vector2(GUICanvas.Instance.Size.X, GUICanvas.Instance.Size.Y);
            Vector2 vect2 = screenPosition / vect1;
            Vector2 vect3 = Vector2.Multiply(vect2, (camera.SceneMax - camera.SceneMin));
            Vector2 vect4 = vect3 + camera.SceneMin;
            return vect4;
        }
All I'm doing is getting the mouse position every tick, converting it, and setting my object's position.
Thanks
By the way, great work on the Builder, will definitely make things a lot easier if I ever decide to move into 3D.