I haven’t been able to figure out how to turn an enemy to correctly follow the user in a game i’m playing around with – take a look here:
flash player 8.5 required (as3) –
http://www.danielhai.com/blog/wp-content/uploads/2006/03/Game1.swf
when I cross the 180 / -180 degree line, I can’t figure out how to make the enemies turn the correct way (they decide to go the long way)
I tried modding this script into something that looks like the algorithm here:
http://www.gotoandplay.it/_forums/viewtopic.php?t=1776&sid=6bda54a1a82304c54b78bced0b2ac7dd
but couldn’t get it to work right still – here’s what my code looks like:
private static const RADIANS:Number = 180 / Math.PI;
private static const PI2:Number = Math.PI * 2;
public function turnTo(object:RenderObject):void {
var ang:Number = getRotationRadians(object);
var angDiff:Number = ang - (_rotation / RADIANS);
(angDiff > Math.PI) ? angDiff -= PI2 : 0;
(angDiff < -Math.PI) ? angDiff += PI2 : 0;
Math.abs(angDiff) > MAX_ROTATION ? angDiff *= MAX_ROTATION / Math.abs(angDiff) : 0;
rotation += (angDiff * RADIANS);
}
public function getRotation(object:RenderObject):Number {
return Math.atan2(object.y - y, object.x - x) * RADIANS;
}
public function getRotationRadians(object:RenderObject):Number {
return Math.atan2(object.y - y, object.x - x);
}
any ideas?