Game Development Community

Convert mouse world position to object position

by Isaac Barbosa · in Torque Game Builder · 08/16/2008 (9:35 am) · 7 replies

Hello,

I want to set the mouse position to a specific object position, but I can't reference the object and I get always the wrong coordinates for the mouse position. So my question is, how can I convert an object X and Y positions to a usable mouse coordinates? That's it: I don't want to get my mouse on the upper left corner of the window, I want it placed in the coordinates of my object, but it seems that this code is wrong.

%newPosX = %myObject.getPositionX();
%newPosY = %myObject.getPositionY();
canvas.setCursorPos(%newPosX, %newPosY);

with this code mouse takes the object coordinates but is placed in the world position. I don't know how to explain this more clearly, but mouse is not taking the X of the object but the X of the world (where 0 is the upper left corner).

Thanks for any help with this :)

Isaac

#1
08/16/2008 (1:12 pm)
Check these out!

t2dSceneWindow::getWindowPoint
t2dSceneWindow::getWorldPoint
#2
08/16/2008 (2:43 pm)
@James, Thanks for the quick answer :)

I have reading through the docs, but since there are no examples, I'm lost after some attempts.

I use this lines to echo

echo(sceneWindow2D.getWindowPoint("x y"));
echo(sceneWindow2D.getWorldPoint("x y"));

and I get the windowpoint and worldpoint coordinates, but that doesn't help to any conversion and I'm still getting the mouse cursor at the upper left corner of the window when implement this code:

$item.positionY = $item.getPositionY() - 5;
echo($item.getWorldPoint("y"));
//if I get a 5, 5 coordinates, the cursor is send tho that coordinates but not in the same plane as my item is. It seems that there are two coordinates system in TGB and I really do not know how to make them be in the same plane -this is the best explanation I can do- so even when the cursor coordinate is in fact 5, 5 is not the same 5,5 of the item.
canvas.setCursorPos($dado1.getWorldPoint("x y"));

can go further and give an example on how to implement this?

Thanks!

The most simple question should be: how can I get an object position (doing a click) and pass that position to the cursor? -I know how to do the click, but not how to pass the values-.
#3
08/16/2008 (3:00 pm)
If I echo this when left click over the object I get two different coordinates,

echo(canvas.getCursorPos() SPC "current cursor position");
echo(%this.getWorldPoint("x y"));

337 398 current cursor position
2.550000 24.950001

So if I state:

canvas.setCursorPos(%this.getPosition);

the cursor is send to the upper left corner (to the 2.55 24.95 position of its coordinates system, but is not send to the object position.

Don't know what to do :(
#4
08/16/2008 (3:03 pm)
%worldPos = sceneObject.getPosition();
%windowPos = sceneWindow2D.getWindowPoint( %worldPos );
canvas.setCursorPos( %windowPos );
#5
08/16/2008 (3:21 pm)
@James,

This is confusing me since I see different coordinates when echoing.

2.550000 19.950001 //sceneobject position
336.320007 367.679993 //cursor position

Using the code you provided, I get the cursor moving to the same x position, but to a very different y. It is possible to achieve to send the cursor to both x and y coordinates instead of only the x?

Maybe I'm also confused because there is no a method to set the cursor Y position. In this case, how it works when you want to change only the x or y position of the cursor but not both of them?

something like:

%worldPosX = babe.getPositionX();
%worldPosY = babe.getPositionY(); 
%windowPos = sceneWindow2D.getWindowPoint( %worldPosX, %worldPosY );
canvas.setCursorPos(%windowPos);

Thanks
#6
08/16/2008 (3:33 pm)
They should be different, one is the position in the scene/world, which is dependent on your camera size. One is the position on the window/screen, eg, your monitor, which is dependent on the resolution of the game.

It should set both x and y, its possible setCursorPos is intended to take the x, y as separate parameters?
Try,

canvas.setCursorPos( getWord(%windowPos,0), getWord(%windowPos,1) );
#7
08/16/2008 (3:41 pm)
Thanks James!

That did the trick :)

I will study this to understand the differences between both systems.