I would like to rotate an object along a curve. I managed to move the object along the curve using bezier curve algorithm. The hard part is to rotate the object constantly(no limit on maximum angle), and have to land at the end position. The angle have to be multiply of 360 when it land on the end position. A good example would be Maplestory, the item is dropped when a monster is killed. The item is rotated constantly and it will always landed at the correct angle. Start position and End position will be provided, as is used by the bezier curve algorithm, is there any algorithm for the rotation part?
-
\$\begingroup\$ This sounds like just interpolation. What have you tried so far and what went wrong? \$\endgroup\$– DMGregory ♦Commented Jan 3, 2019 at 13:18
-
\$\begingroup\$ i don't really know what kind of algorithm to use for the rotation part \$\endgroup\$– Lim Ta ShengCommented Jan 3, 2019 at 13:25
-
\$\begingroup\$ How do you store or represent your object's rotation currently? As an angle? A matrix? A quaternion? Something else? \$\endgroup\$– DMGregory ♦Commented Jan 3, 2019 at 13:28
-
\$\begingroup\$ Currently store as an angle \$\endgroup\$– Lim Ta ShengCommented Jan 3, 2019 at 13:30
1 Answer
First, choose your starting and ending angles by any method you choose (they could be fixed constants, or one or both could be randomized or computed from the other...)
Next, compute the angular difference between them, wrapped to +- 180 degrees. eg...
float angleDifference = fmod(endAngle - startAngle + 900.0f, 360.0f) - 180.0f;
Next, when you compute your new position using a parameter 0 <= t <= 1
passed as an argument to your Bezier curve function, use the same parameter to interpolate your angle:
position = EvaluateBezier(startPosition, controlPoint, endPosition, t);
angle = fmod(startAngle + t * angleDifference + 360.0f, 360.0f);
This ensures you rotate at a constant rate over the course of the motion, and end with the angle exactly at your desired endAngle
.
If you prefer to have the motion ease out, you can do so by applying the easing function of your choice to the parameter t
before using it to interpolate your angle.
-
\$\begingroup\$ alright i will try it out, will get back to you later! \$\endgroup\$ Commented Jan 5, 2019 at 5:46