Is there a function that do this to my angle?
by Andrea Farid Marsili · in Torque Game Builder · 02/16/2011 (1:34 pm) · 7 replies
vectorToAngle
Usage: vectorToAngle(x,y)
Find the angle relative to the origin, (0,0), given an X and Y coordinate. You can include an offset,(x',y'), to find an angle relative to the offset.
e.g. vectorToAngle(x-x',y-y')
It's a GameSalad function. Is there something like that into Torque/iTorque?
PS: I need this because I want to contraint sprite's rotation to mouse position.
Usage: vectorToAngle(x,y)
Find the angle relative to the origin, (0,0), given an X and Y coordinate. You can include an offset,(x',y'), to find an angle relative to the offset.
e.g. vectorToAngle(x-x',y-y')
It's a GameSalad function. Is there something like that into Torque/iTorque?
PS: I need this because I want to contraint sprite's rotation to mouse position.
#2
While you can use setRotation to instantaneously set the angle, you can also use rotateTo to have it change gradually.
02/16/2011 (2:08 pm)
One other thought for you...While you can use setRotation to instantaneously set the angle, you can also use rotateTo to have it change gradually.
#3
The easiest way to get the angle between two 2D points is arctan, or mAtan in TS:
02/16/2011 (2:09 pm)
I always find it handy to understand what is going on in the background instead of just using some method that appears to give you what you want.The easiest way to get the angle between two 2D points is arctan, or mAtan in TS:
%point = "2 1"; %angleRad = mAtan( %point.Y, %point.X );The result is in radians and can be converted like this:
%angleDeg = mRadToDeg( %angleRad );Now, T2D flips the y-axis so up is -y and down is +y. This means that if you are using coordinates from the scene space, you need to flip the y-point:
%angleRad = mAtan( -%point.Y, %point.X );In the above example, you are finding the angle of the vector from the origin (0,0) to the point (2,1). If you are looking to find the angle of the vector between two points, you merely subtract point A from B:
%pointFrom = "2 1"; %pointTo = "2 3"; %angleRad = mAtan( %pointTo.Y - %pointFrom.Y, %pointTo.X - %pointFrom.X );
#4
This is my code use it if you want to use it (but I'm sure you can do a lot better)
02/20/2011 (12:37 am)
I almost manage to do it. Now my actor rotate but I need to rotate it only when mouse click on the actor and remain pressed.This is my code use it if you want to use it (but I'm sure you can do a lot better)
function TextClass::onLevelLoaded(%this, %scenegraph){
//I set some text object to show mouse position and actor's rotation
posXText.text = "Mouse Position X";
posYText.text = "Mouse Position Y";
rotText.text = "Actor's Angle";
//rotable is the actor's name
$initRotation = rotable.getRotation();
$rotPosX = rotable.getPositionX();
$rotPosY = rotable.getPositionY();
$initAngle = mAtan($mousePositionY - $rotPosY, $mousePositionX - $rotPosX);
}
function sceneWindow2D::onMouseMove(%this, %modifier, %worldPosition, %clicks){
$mousePositionX = getWord(%worldPosition, 0);
$mousePositionY = getWord(%worldPosition, 1);
posXText.text = %mousePositionX;
posYText.text = %mousePositionY;
rotText.text = rotable.getRotation();
%myAngle = mAtan($mousePositionY - $rotPosY, $mousePositionX - $rotPosX);
rotable.setRotation(($initRotation + (%myAngle - $initAngle)) * 45);
}
#5
* Set a global variable to "0" (to mean no actor selected).
* Implement "::onMouseDown" for the actor. Set a global variable to that actor.
* If the global variable isn't "0", have "sceneWindow2D::onMouseMove" rotate the actor stored in the global variable.
* Implement "sceneWindow2D::onMouseUp" to set the global variable back to "0".
02/20/2011 (11:11 am)
Here's a rough outline of what to do:* Set a global variable to "0" (to mean no actor selected).
* Implement "::onMouseDown" for the actor. Set a global variable to that actor.
* If the global variable isn't "0", have "sceneWindow2D::onMouseMove" rotate the actor stored in the global variable.
* Implement "sceneWindow2D::onMouseUp" to set the global variable back to "0".
#6
But maybe I set the onMouseDown function for sceneWindow2D and not for the actor's class.
I'll try.
EDIT:
I tried to set a rotableClass::onMouseDown but it doesn't work. When you click the mouse TGB stop to map mouse position (I understand it because the two TextObject that display mouse position stop to update their text).
02/20/2011 (11:41 am)
I already tried something like that with a boolean variable but it doesn't work because when you click the mouse the onMouseMove doesn't map mouse position.But maybe I set the onMouseDown function for sceneWindow2D and not for the actor's class.
I'll try.
EDIT:
I tried to set a rotableClass::onMouseDown but it doesn't work. When you click the mouse TGB stop to map mouse position (I understand it because the two TextObject that display mouse position stop to update their text).
#7
02/20/2011 (1:22 pm)
I resolved my problem changing sceneWindow2D::onMouseMove with myActorClass:onMouseDragged.
Associate William Lee Sims
Machine Code Games
The first one returns the angle between two vectors. The second would return the angle from the origin (which you could subtract out the sprite's origin from the mouse's position to get the angle to the mouse).