# Predicting a stopping point. (Maths question)

Hi folks. I have an issue with a rotating sprite, it’s rotation accelerates and slows down to a radian that’s already known. (Or it will when I work it out!)

So, the acceleration code will be something like

``````Do
TurnDamping += -0.001
If TurnDamping < -0.04 Then TurnDamping = 0.04
Turn += TurnDamping
Sprite.Rotation += Turn
Loop
``````

and at some predetermined point I need to start slowing the rotation again. Of course the code is going to be more or less the exact opposite of that above, but how can I quickly calculate when I need to start the slowing process?

I could run an inner Do Loop using a couple of static variables and check if the value meets the radian I want, but that seems really messy.

So, is there a quick way to check when xxx, xxx, xxx, .016 + .008 + .004 + .002 + .001 = PredeterminedRadian?

Thanks.

Just to let you know, I’m having awful problems when pasting code into this text box, I click the code icon, and paste the code while the highlight is there, but only the first line appears in the block, I then have to manually Return/Delete all the other lines into the right place.

No bother, just thought I’d let you know.

say you have the series x = .016 + .008 + .004 + .002 + .001
multiply by 2 and you get
2x = .032 + .016 + .008 + .004 + .002

now subtract x:
2x - x = x
or:

``````     .032 + .016 + .008 + .004 + .002
-  (       .016 + .008 + .004 + .002 + .001)
=   .032                             - .001
``````

see?
x = .032 - .001 = 0.031 = 2*.016 - .001

Now, say you have the series x = .006 + .005 +.004 + .003 + .002 + .001
you can say it’s the sum of x’+x’’ where

``````x' =  .006        +.004       + .002
x''=       + .005       +.003        + .001
``````

now if you rearange the x’’ you get
x’ = .006 + .004 + .002
x’’= .001 + .003 + .005
by grouping the first and last number it’s easy to get x= x’+x’’ which is:
x = .007 + .007 + .007 = (.007)*3

Depending on the form of the series, you have to apply different tricks.
I hope you get the general idea.

Another way to see at this problem is to plot the values of TurnDamping on the XY plane, in your simple case it will be a line.
The final sum would the the area below that line, in your case it would be the area of a triangle (trivial staff!) or if you are not stopping at zero you might have to subtract or add two triangles.

BTW I suggest to rename ‘Turn’ into ‘AngularVelocity’ and ‘TurnDamping’ into ‘AngularAcceleration’ because from the code above that’s what it seems they are.

If you know the starting and ending Angle AND the duration, let’s say it starts from rest at angle 0, start accelerating and then stop at angle 90 after 2 second you can use a tweening function. Monogame has a buildin method to do just that, MathHelper.SmoothStep().
try something like SmoothStep (0,90, gameTime.TotalGameTime.TotalSeconds/2).

You’re right about the variable names, they could be more expressive.

I do know the starting and ending angle, but these will be changing slightly with each gameUpdate, so the calculations are very unlikely to ever be perfectly accurate, but I can cope with that.

This was meant to be a really easy 2D game with simple physics, but it’s now testing my poor fuddled brain.

The SmoothStep sounds interesting, but I’d have to see it working to see if it fits. Sadly I haven’t been able to get Mono working with VB yet, so I’m still building this in XNA and hoping to transfer it soon.

Assume your current Rate is in radians / second and the Acceleration is in radians / second^2
Rate / Acceleration will give you the number of seconds to decelerate to 0.
During the deceleration time the average turn rate, since it’s linear, will be 1/2 your starting rate.
We can then put this together to calculate how many radians you will turn while stopping:

TimeToStop = Rate / Acceleration
AveRate = Rate / 2