Canvas move object in circle

前端 未结 1 978
死守一世寂寞
死守一世寂寞 2021-01-12 02:07

I have a rectangle in canvas, and I know how to move it up and sideways. What I want to do is have it move in a circular motion.

So my objects(rectangle) x and y wou

1条回答
  •  一整个雨季
    2021-01-12 03:08

    The parametric equation for moving in a circle is this:

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

    theta is the angle, and r the radius.

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

    The JavaScript functions are Math.cos, Math.sin, and Math.acos, respsectively. They all deal with radians.

    0 讨论(0)
提交回复
热议问题