Home > Software design >  How to use an exponential easing function while using Transform.RotateAround?
How to use an exponential easing function while using Transform.RotateAround?

Time:03-10

I have a scene where a cube-shaped object "rolls" on a flat surface. Each time, it completes a 90-degree rotation around pivotPoint (the edge making contact with the surface) over the pivotAxis (perpendicular to the direction of movement). For this, the Transform.RotateAround function seemed appropriate, as it allows me to supply those parameters and apply the necessary position change that comes with this kind of off-center rotation. The movement has a time constraint of totalDuration.

This is inside the coroutine where movement is applied:

timePassed = 0f;
while (timePassed < totalDuration)
        {
            timePassed  = Time.deltaTime;
            rotationAmount = (Time.deltaTime / totalDuration) * 90f;
            cube.RotateAround(pivotPoint, pivotAxis, rotationAmount);

            // Track rotation progress in case the function is interrupted
            offBalanceDegrees  = rotationAmount;

            yield return null;
        }
// snap position and rotation of cube to final value, then repeat

This works well enough and gives me linear movement, as the rotationAmount in each iteratio is the same. However, I'd like this to feature an easing function to start off slowly and finish quickly. I must still be able to specify a pivotPoint, pivotAxis and totalDuration, and I'm a bit stuck as to how I could accomplish this.

Other examples I've seen applied rotation around the object's centre, which is a simpler case than what I have.

CodePudding user response:

If you have a linear t between 0 and 1, you can use tnew = t * t * t; to get a cubic tnew. Then you just need to reconfigure your equation to use a t that goes from 0 to 1.

Altogether, this might look like this:

timePassed = 0f;
offBalanceDegrees = 0f;
while (timePassed < totalDuration)
{
    timePassed  = Time.deltaTime;

    float t = timePassed / totalDuration
    t = t * t * t;

    // Track rotation progress in case the function is interrupted
    oldRotationAmount = offBalanceDegrees;
    offBalanceDegrees = t * 90f;

    cube.RotateAround(pivotPoint, pivotAxis, offBalanceDegrees - oldRotationAmount);

    yield return null;
}
  • Related