views:

47

answers:

1

Essentially what I'm trying to do is nearly the same as this: http://www.senocular.com/flash/source/?id=0.16

However I'd like to ease/tween the movieclip position and rotation to the mouse position.

The main issue I see is trying to get the current movieclip rotation and the target rotation, then tween it cockwise or counterclockwise from current to target. The way a movieclips rotation uses positive and negative numbers throws it off. If you just want to lock the mc rotation to the mouse rotation its fine, but once you try to tween it you run into difficulties.

So the end effect would be like if you were to draw imaginary clockwise circles around the object, it should just keep rotating clockwise towards the mouse. Then if you started going counter clockwise it should just keep easing counter clockwise to the mouse.

Thanks for any help.

+2  A: 

So there are two parts. One, you want to find the shortest angle between two angles. Two, you need to ease between the angles and position.

Finding shortest angle.

angleDelta will be the shortest rotation angle between your two angles.

var angleDiff:Number = angleTo - currentAngle;
var angleDelta:Number = Math.atan2(Math.sin(angleDiff), Math.cos(angleDiff));
angleDelta *= (180/Math.PI); // Change angle from Radians to Degrees.

Ease between values

I usually use a simple equation in some kind of loop (usually enterFrame) to ease between simple values.

addEventListener(Event.ENTER_FRAME, enterFrameHandler);
private function enterFrameHandler(e:Event):void 
{
    rotation += (targetRotation - currentRotation) * 0.2;
    x += (targetX - currentX) * 0.4;
    y += (targetY - currentY) * 0.4;
}

EDIT*

This should work for you.

const TO_DEGREES:Number = 180 / Math.PI;
const TO_RADIANS:Number = Math.PI / 180;

addEventListener(Event.ENTER_FRAME, enterFrameHandler);

function enterFrameHandler(e:Event):void
{
    var angleTo:Number = Math.atan2(stage.mouseY - y, stage.mouseX - x) * TO_DEGREES;
    if (angleTo > rotation+180) angleTo -= 360;
    if (angleTo < rotation-180) angleTo += 360;

    rotation += (angleTo - rotation) * 0.2;
    x += (stage.mouseX - x) * 0.2;
    y += (stage.mouseY - y) * 0.2;
}
TandemAdam
Ok I partially follow you, but as you'll see with my result I'm floundering a bit:http://dl.dropbox.com/u/1914/car/mouseCar3.htmlsource: http://dl.dropbox.com/u/1914/car/mouseCar3.flaCan you pinpoint where the math is off causing it to be erratic?
Can't open that file as I only have CS4
TandemAdam
sorry about that. I updated it with a CS4 one... same link.dl.dropbox.com/u/1914/car/mouseCar3.fla
Just updated my answer.
TandemAdam
Hey Adam. Thanks so much for taking the time to help me with this. It looks like you've gotten this to where I had hoped! I really appreciate your help.
No problem. Make sure you make the answer as "correct" if you do think it is :)
TandemAdam