1
\$\begingroup\$

I'm attempting to have a sprite rotate in a way where its rotation speed increases until it has reached the halfway point in its rotation, to which it starts to slow down. I'm currently calculating the halfway point by calculating the amount of time it should take to rotate the sprite, and then checking if a current time variable is larger.

The code works rather well except for the fact that I can't figure out how current time should increment each update. Currently I'm using delta time, but that doesn't work. I'm thinking it needs to be some combination of the calculated time to rotate and delta, but I haven't gotten it yet (clearly).

EDIT: Specifically, currentTime shouldn't be incremented by delta solely, but I have no idea what it should be in place of delta.

        if (currentTime <= timeToRotate)
        {
            rotationSpeed += 5f * delta;
            currentTime += delta;
        }

        else
        {
            if (rotationSpeed > 5f)
                rotationSpeed -= 5f * delta;

            else
                rotationSpeed = 5f;

            currentTime += delta;
        }
\$\endgroup\$
0

2 Answers 2

2
\$\begingroup\$

Correct me if I am wrong, but from what I understand, you want to achieve the following:

enter image description here

If so, this can be achieved using trigonometry:

float easing = 0.075f;

float direction = Math.atan2(target.y - sprite.y, target.x - sprite.x) / Math.PI * 180;

if (direction < sprite.rotation - 180) {
    direction += 360;
}

if (direction > sprite.rotation + 180) {
    direction -= 360;
}

sprite.rotation += (direction - sprite.rotation) * easing;
\$\endgroup\$
6
  • \$\begingroup\$ That actually works once I got rid of my code that was checking if the angle should be incremented in a positive or negative direction. My only question is how could I incorporate a delta time so that the rotation would take the same amount of time on two computers? \$\endgroup\$
    – AerospaceP
    Commented Jul 13, 2015 at 16:59
  • \$\begingroup\$ Just multiply sprite.rotation by deltaTime after all previous calculations. \$\endgroup\$
    – driima
    Commented Jul 13, 2015 at 17:07
  • \$\begingroup\$ Ah okay, that didn't exactly work as that just caused the rotation to be minuscule. So I multiplied easing by delta instead and increased it, and it would seem to work but I'll have to test it on a different computer. Thank you. \$\endgroup\$
    – AerospaceP
    Commented Jul 13, 2015 at 17:16
  • \$\begingroup\$ No problem - it's strange that spriterotation *= deltaTime would yield a slower result; have you tried logging deltaTime? It should be around 1, give or take a few decimals. \$\endgroup\$
    – driima
    Commented Jul 13, 2015 at 17:22
  • \$\begingroup\$ It isn't. It is around 0.05 +- 0.02. I'm using LibGDX if that tells you anything. \$\endgroup\$
    – AerospaceP
    Commented Jul 13, 2015 at 17:39
0
\$\begingroup\$

This seems like a real nice fit for using something like bias and gain.

Using those functions, you do a simple linear interpolation, but before using your "percent" value in the lerp, you pass it through a function to make the percent value non linear.

This makes it so it still takes the same amount of time to do, but you can make it faster in the beginning and slower in the end or make it slow near the ends and fast in the middle - or other behaviors as well.

Check this out for more info: http://blog.demofox.org/2012/09/24/bias-and-gain-are-your-friend/

\$\endgroup\$
7
  • \$\begingroup\$ While that seems useful, I don't see how that solves my problem as those functions require a time variable which was my original problem. \$\endgroup\$
    – AerospaceP
    Commented Jul 13, 2015 at 4:17
  • \$\begingroup\$ Oh sorry I missed that. Do you not have an end and start rotation? If not, how come? \$\endgroup\$
    – Alan Wolfe
    Commented Jul 13, 2015 at 4:19
  • \$\begingroup\$ I'm confused as to what you mean. The sprite rotates but with the code I have listed, not correctly. I have a function that determines the quickest way to rotate, and if I leave rotation speed constant, it works fine but with no "acceleration". I wanted it to speed up until it hit halfway and start to slow down after that. I made a function to combine with what you linked that returns the percentage of the rotation completed, but I still had no luck. \$\endgroup\$
    – AerospaceP
    Commented Jul 13, 2015 at 4:42
  • \$\begingroup\$ Ah. What I'm getting at is that if you store starting and ending rotation, you can calculate how long it should take total (such as say 2 seconds) and then track how much time has elapsed. Each frame you figure out what percentage of the time has elapsed and put that percentage through bias or gain to make speed non linear, then use that adjusted "percentage" to set the rotation at that percentage from the start rotation to the end rotation. Hope that makes sense. \$\endgroup\$
    – Alan Wolfe
    Commented Jul 13, 2015 at 4:53
  • \$\begingroup\$ Ah okay. When I'm setting the angle I calculate the time, and each update I use a method that I made that checks the time elapsed and the total time and returns the two divided. I tried plugging in the divided times into the gain function but the rotation speed never started to decrease after the halfway point. \$\endgroup\$
    – AerospaceP
    Commented Jul 13, 2015 at 5:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .