velocity calculation algorithm

前端 未结 2 801
遥遥无期
遥遥无期 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:00

    I can think of two ways to do it.

    lets say the angle from a to b is T. Then:

    T is equal to atan((b.y-a.y)/(b.x-a.x))

    knowing T you can calculate the x and y velocities:

    Vx = cos(T)V Vy = sin(T)V

    That should work.

    To make things quicker you could calculate cos(T) and sin(T) directly.

    sin(T) gives the proportion y/h, where h is the length of the line between a and b.

    We can calculate h using the Pythagorean theorem:

    h = sqrt((b.y-a.y)^2 + (b.x-a.x)^2)

    from this we can derive formulas for Vx and Vy

    Vx = V * (b.x-a.x)/sqrt((b.y-a.y)^2 + (b.x-a.x)^2)

    Vy = V * (b.x-a.x)/sqrt((b.y-a.y)^2 + (b.x-a.x)^2)

    That's probably be faster, particularly if you have a built Pythagorean theorem function.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题