Game Development Community

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.
Page«First 1 2 Next»
#21
09/30/2005 (8:03 am)
Here is my current rotateTo function if anyone would need it. I am running david graces heatbeat fix as an engine mod for more accurate timing but it should still work without the mod. might take some tweaking of the lock zone though.

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;
	    }
		
}
#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.
Page«First 1 2 Next»