velocity calculation algorithm

前端 未结 2 803
遥遥无期
遥遥无期 2021-01-23 19:16

This is probably a very stupid math question, but i can\'t seem to figure it out. What i have is a circle at point A that i can click on and drag the mouse oway from it. When th

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-23 20:07

    Just normalize the vector from the center of the circle to the point and then multiply by the speed you want. In any goodl vector library there is such a function, but just to clarify:

    length=square_root((b.x - a.x)^2+(b.y - a.y)^2)
    velocityX = (b.x - a.x) / length * speed
    velocityY = (b.y - a.y) / length * speed
    

提交回复
热议问题