Adobe Flash cs4 rotation math
i am currently having trouble with my object rotating to face another object.currently it works fine as in this picture, my object at the origin will be able to rotate and move to the object in the 3 quadrants except the quadrant with positive 90degrees to 180 degrees, my object will rotate a few full rotation for some time while moving to the object does anyone know why? the sentence that i use to rotate is rotation += (foodLocationDegrees - (rotation - 90)) * .2;
whereby food is calculated using
var angle:Number = Math.atan2(foodTarget.y - y, foodTarget.x - x);
foodLocationDegrees =Math.floor((angle * 180 / Math.PI));
y and x being the object which is a fish, and foodtarget being a object of a food.
public function moveToFood():void
{
var dx:Number = x - _destinationX;
var dy:Number = y - _destinationY;
trace("Food location degree relative to fish mouth "+foodLocationDegrees);
var targetRotation:Number = 0;
if (foodLocationDegrees > 0 && foodLocationDegrees < 90)
{
trace("food is on 1st quadrant of the fish mount");
this.x -= dx / 18;
this.y -= dy / 18;
}else if (foodLocationDegrees > 90 && foodLocationDegrees < 180)
{
trace("food is on 2nd quadrant of the fish mount");
this.x -= dx / 18;
this.y -= dy / 18;
}else if (foodLocationDegrees > -180 && foodLocationDegrees < -90)
{
trace("food is on 3nd quadrant of the fish mount");
this.x -= dx / 18;
this.y -= dy / 18;
}else if (foodLocationDegrees < 0 && foodLocationDegrees > -90)
{
trace("food is on 4nd quadrant of the fish mount");
this.x -= dx / 18;
this.y -= dy / 18;
}
trace("Before adding Rotation " + rotation);
var number:Number = (foodLocationDegrees - (rotation - 90)) * .1;
trace("rotation to add "+number);
rotation += (foodLocationDegrees - (rotation - 90)) * .2;
trace("After adding Rotation " + rotation);
//removing food when both hit boxes hit
if (hit.hitTestObject(foodTarget.hit))
{
foodInPond--;
foodTarget.removeSelf();
}
}
public function moveToFood(food:Food):void
{
var speed:Number = 6.5;
var a:Number = food.x - x;
var b:Number = food.y - y;
var ang:Number = Math.atan2(b, a);
rotation = ang * 180 / Math.PI;
x += Math.cos(ang) * speed;
y += Math.sin(ang) * speed;
}
难道你不能只用sin
和cos
来朝着你所面对的方向前进吗?
上一篇: 如何使用CS4编译Flash 11
下一篇: Adobe Flash CS4旋转数学