本文介绍了速度计算算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常愚蠢的数学问题,但我似乎无法弄清楚。我所拥有的是在A点的一个圆圈,我可以点击并从中拖拽鼠标。释放鼠标时 - 释放点B被认为是目标点,球必须沿该方向移动。我现在正在做的是这样的:

  velocityX =(bx  -  ax)/ somenumber 
velocityY = (by - ay)/ somenumber

这允许我使用不同的拍摄速度,鼠标从圆圈释放。但是现在我意识到我不喜欢这个想法,而是想通过以下方式来实现:




  • 最大速度(每个动画帧的像素数)

  • 可在拍摄前从此间隔中选择速度
  • 仅使用B点即可轻松定位。射击速度是预先选定的,它不应该取决于鼠标的释放距离



我知道它应该很简单,但

解决方案

只需将从圆心到点的矢量标准化,然后乘以所需的速度即可。在任何goodl矢量库中都有这样一个函数,但只是为了澄清:

  length = square_root((bx  -  ax)^ 2+(by  -  ay)^ 2)
velocityX =(bx - ax)/ length * speed
velocityY =(by - ay)/ length * speed


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 the mouse is released - the release point B is considered a target point and the ball has to move in that direction. What i'm doing now is something like this:

velocityX = (b.x - a.x) / somenumber
velocityY = (b.y - a.y) / somenumber

This allows me to use different "shot" speeds the further away the mouse is released from the circle. But now i realised that i don't like this idea and instead i want to do it the following way:

  • to have a minimum and maximum speed (pixels per animation frame)
  • to select the speed from this interval prior to the shot
  • to use the point B simply for easier targeting. The shot speed is preselected and it should't depend on how far the mouse is released

I know it should be dead simple, but how do i (knowing point A and B coordinates, min, max and selected velocity) set x and y velocities to the circle taking into account the direction of the shot?

解决方案

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

这篇关于速度计算算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:47