3
votes

So I have something that half works right now, I say it half works because it seems to round the angle somehow to the nearest 45 degrees or so, here is the code:

public function drive(e:Event)
{
    speedX =Math.sin(carObj.rotation*(Math.PI/180))*2;
    speedY = Math.cos(carObj.rotation*(Math.PI/180))*2*-1;
    carObj.x +=  speedX * speed;
    carObj.y +=  speedY * speed;
}

Does anyone know a better way which will get the exact (visibly at least so the eye can't tell the difference) angle of rotation and translate the object in that direction at a given speed.

2

2 Answers

8
votes

nevermind solved:

var carAngle:Number = carObj.rotation * Math.PI / 180;
carObj.x = carObj.x + speed * Math.cos(carAngle);
carObj.y = carObj.y + speed * Math.sin(carAngle);
0
votes

//rotation after hitting something and then moving at some angle with the face infront...

var speedX:Number = randomRange(-10,10); // function generating random numbers
var speedY:Number  = randomRange(-10,10);

carObj.x += speedX;
carObj.y += speedY;

function calculateAngle()
{
 var radians:Number = 180 / Math.PI;
 //Calculate rotation
 var walkdirection = - (Math.atan2(tempAlien_mc.oldX - tempAlien_mc.x, tempAlien_mc.oldY - tempAlien_mc.y))*radians;
 //Rotating Car
 tempAlien_mc.rotation = walkdirection;
}

function randomRange(minNum:Number, maxNum:Number):Number   
{  
  return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);  
}