0

I want to move an object in a circular path but I want it to complete its movement in that circle when the time ends.

I tried doing this:

p.tmy = p.tmy + p.duration/p.radius
p.x0 = p.radius * math.cos(p.tmy + 0.025)
p.x1 = p.x0 * math.cos(p.tmy) -  0 * math.sin(p.tmy)
p.y1 = p.x0 * math.sin(p.tmy) +  0 * math.cos(p.tmy)

The issue is that it's too slow or too fast & doesn't complete the circle on time.

Pikalek
  • 12,372
  • 5
  • 43
  • 51
Achaibo
  • 3
  • 1

1 Answers1

0

First, you need to calculate how much progress you should have made as a function of time.

Let's say your total amount of time you have to complete the rotation is \$T\$ & the amount of time travelled so far is \$t\$.

Then \$\frac{t}{T}\$ gives you a number between 0.0 & 1.0 indicating how much progress you should have made. For instance, if your time limit is 10 second & you have been moving for 8 seconds, then you know you need to have covered 80% of the rotation.

Next, you need use the relative progress to determine how far you've rotated. There are \$360^\circ\$ in a circle which is is equivalent to \$2\pi\$ radians. Since the math functions use radians, we'll use that for our math as well.

\$\frac{2\pi t}{T}\$ is the relative amount of the rotation. This is literally just multiplying your progress % by the total possible radians required for rotating. For instance, using the 8 of 10 seconds example from before, you would have travelled about 5.0265 radians or \$288^\circ\$.

Plugging that math into your cos & sin functions will give you the X & Y coords on the unit circle. From there you can scale & translate as needed.

For instance, if your circle is centered on cx,cy and has a radius of r, you would have something like the following:

x = math.cos(2*math.pi*t/T) * r + cx
y = math.sin(2*math.pi*t/T) * r + cy
Pikalek
  • 12,372
  • 5
  • 43
  • 51