Canvas moves an object in a circle.

I have a rectangle in the canvas, and I know how to move it sideways. I want him to move in circular motions.

So my objects (rectangle) x and y will go in a circle.

Now I assume that I need a radius for how far and some formula for speed (1 pixel) is to make it rotate along the axis.

Any idea?

+7
source share
1 answer

The parametric equation for moving around the circle is as follows:

x=r*cos(theta) y=r*sin(theta) 

theta is the angle, and r is the radius.

If you want to know the change in theta , in order to get the desired speed, deciding on the distance d , you get that change in theta : arccos(1-(d/r)^2/2)

JavaScript functions: Math.cos , Math.sin, and Math.acos , respectively. All of them relate to radians.

+13
source

All Articles