Help with "look at" ( using vector ops? )
by Phil Shenk · in Torque Game Builder · 02/27/2005 (9:05 pm) · 23 replies
I'm trying to take a sprite and set it's orientation to look at a point on the screen (such as the mouse cursor). Ideally ( I think ), I'd have a way to get a positive or negative angular offset from the sprite's current angle. Then I could apply set the sprite's rotation towards that new direction.
I'm pretty light on the vector maths, so any help is appreciated.
I'm pretty light on the vector maths, so any help is appreciated.
About the author
#22
08/22/2008 (11:35 am)
-
#23
08/22/2008 (1:19 pm)
If you are using TGEA then the code above will do nothing for you, since there is no "setAutoRotation" and the player looks left/right/up/down using dsq blend animations. But I don't know off the top of my head how to do something similar in TGEA.
Torque Owner Jesse Hall
function rotateTo( %obj, %targetAngle, %currentAngle, %arcPower){ //======================================================= // convert all angles to positive //======================================================= %targetAngle = anglePosi( %targetAngle ); %currentAngle = anglePosi( %currentAngle ); //======================================================= // snap lock and stop if target has been met //======================================================= if ( %currentAngle > %targetAngle - 3 && %currentAngle < %targetAngle + 3 ){ %currentAngle = %targetAngle; %obj.setAutoRotation( 0 ); %obj.setRotation( %targetAngle ); return; } //======================================================= // calculate the outer and inner arc //======================================================= if ( %currentAngle > %targetAngle){ %outerArc = %currentAngle - %targetAngle; } else{ %outerArc = %targetAngle - %currentAngle; } %innerArc = 360 - %outerArc; //======================================================= // setup the rotation //======================================================= // outer arc is bigger if ( %outerArc > %innerArc){ if ( %currentAngle > %targetAngle ){ %obj.setAutoRotation( %arcPower ); } if ( %targetAngle > %currentAngle){ %obj.setAutoRotation( - %arcPower ); } } // inner arc is bigger if ( %innerArc > %outerArc){ if ( %currentAngle > %targetAngle ){ %obj.setAutoRotation( - %arcPower ); } if ( %targetAngle > %currentAngle){ %obj.setAutoRotation( %arcPower ); } } }//end function rotateTo //======================================================= // convert angles to positive //======================================================= function anglePosi( %angle ){ if ( %angle < 0){ return ( %angle + 360); } else{ return %angle; } }