Game Development Community

rotating an object to point at an arbitrary position

by James Peter Fayette · in Torque Game Builder · 11/13/2012 (5:10 pm) · 5 replies

I have been working on a rather basic game for a school project, and one of the things I need to do is rotate a sprite to point at the mouse, with the sprite also being located at an arbitrary position. this is my first project in torque, and the answer is probably ridiculously obvious, but I searched around for a bit and failed to find anything applicable. I appreciate any help.

#1
11/13/2012 (10:39 pm)
Calling this code will point the object towards the mouse if your object naturally points upward.

%this.setRotation(t2dAngleToPoint(%this.getPosition(),sceneWindow2D.getMousePosition()));
#2
11/13/2012 (10:52 pm)
well that seems about as blindingly obvious and straightforward as I expected. thanks for the help.
#3
11/14/2012 (9:39 am)
Back in the day, we actually did trig to figure this out, haha. ;D
#4
11/14/2012 (10:11 am)
I just about spent an entire frustrating day trying to figure out this method on my own while trying to understand vectors and angles and sine and cosine etc because I couldn't find any function like 't2dAngleToPoint' in the documentation. I did figure it out eventually but my eyes nearly bugged out of my head when I stumbled upon it in the Torque source code. Turns out it's in the documentation anyway.
#5
11/14/2012 (12:43 pm)
The old way was something like this:

function playerShip::rotateTowardPosition(%this, %target)
{
    %vec = vectorSub(%this.getPosition(), %target);
    %angle = -mRadToDeg(mATan(getWord(%vec,0),getWord(%vec,1)));
    
    %this.rotateTo(%angle, %this.rotateSpeed);
}