Game Development Community

[Solved]canvas.setCursorPos does it with a slight offset Why?

by Vlad I · in Torque Game Builder · 09/12/2012 (12:59 pm) · 3 replies

I have an object Pincers in my scene and I want my mouse to become this object and set position where it is.
It all works only with the slight offset from where the cursor should take place.
I can't figure out why this happens, no errors.
Here is the code:
function PincersGrasp01Class::onAnimationEnd(%this)  
{  
     Canvas.setCursor(PincersGearCursor01); 
     %worldPos = PincersGear01.getPosition();  
     %windowPos = sceneWindow2D.getWindowPoint( %worldPos );  
     //canvas.setCursorPos( %windowPos );  
     canvas.setCursorPos( getWord(%windowPos,0), getWord(%windowPos,1) ); 
}

What do I miss?

#1
09/15/2012 (7:13 am)
Hey Vlad!

It's hard to say without seeing the image for PincersGearCursor01 but I think that when you set the cursor at the point you got in %windowPos, TGB sets the top left of that image to where the mouse was clicked. So if there is any transparency around the image, then there is a little offset that remains.

Try to figure out how many pixels there are on the x and y axes (%xOffset and %yOffset) between the top left of that image and where you want the actual click point of the PincersGearCursor01 and compensate for that in the setCursorPos call.
canvas.setCursorPos( getWord(%windowPos,0) - %xOffset, getWord(%windowPos,1) - %yOffset);

If you want it to set to the center of the image, you could do something like this:
canvas.setCursorPos( getWord(%windowPos,0) - (PincersGearCursor01.getWidth() / 2), getWord(%windowPos,1)  - (PincersGearCursor01.getHeight() / 2) );
#2
09/15/2012 (8:23 am)
Patrick!
You are 100% right!
I didn't know about offset for setCursorPos.
Thanks!
#3
09/15/2012 (8:36 am)
Good deal, glad it worked!