Rotate PVector to a goal PVector at a linear speed
in
Programming Questions
•
4 months ago
I'm having trouble figuring out the math to do this. I have a two directional (normalized) PVectors, and I need one to rotate towards the other.
I can make eased rotation work (
like in this Flash demo), but I need the rotation to happen at a fixed rate, no matter how much the angles differ.
The code that rotates with easing:
- // get the angle distance between the goal and curent direction
- float goalAngle = goal.heading();
- float dirAngle = direction.heading();
- float distance = abs(goalAngle - dirAngle);
- // convert distance to a percentage to lerp (0~TWO_PI to 0~1)
- float percentage = map(distance, 0, TWO_PI, 0, 1);
- direction.lerp(goal, percentage);
1