user2295471
user2295471

Reputation: 87

Gradually rotate towards a direction

I have a car object, and I want it to gradually rotate to the direction where the user clicked.

Every frame I did math to calculate the new direction it needs, and it's stored at car.direction. The actual direction is of course in car.rotation.

Now I want to update the rotation every frame until it's equal to the direction. However I tried everything and can't figure out how to do that.

By the way, I'm using FireFly, that is a gameengine built on top of Starling Framework, But I don't think it's relevant.

Upvotes: 0

Views: 228

Answers (1)

shaunhusain
shaunhusain

Reputation: 19748

I would go with Marty's suggestion, use the smallestAngle function to determine which direction you should be rotating. Basically you can move some percentage of the smallestAngle during every frame update until the percentage of that smallestAngle is below some threshold (and have it "snap" the rest of the way).

Something like

//each time this is called it will get 1/4 closer, but never get to 0, hence the need for a threshold avoid paradoxes
//http://en.wikipedia.org/wiki/Zeno's_paradoxes#Dichotomy_paradox
var angleToMove:Number = smallestAngle()/4; //the divide by 4 here means get 1/4 of the angle gap closer each time this function is called, I assume this is on a timer or frame handler, making the 4 greater will make it follow more slowly, making it lower will make it follow more quickly, never reduce to or below 0 unless you want wonkiness
if(angleToMove<someRadianThreshold)
    angleToMove = smallestAngle();
//use angleToMove to adjust the current heading/rotation

Upvotes: 1

Related Questions