Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java小球从光标处跑开_Java_Algorithm - Fatal编程技术网

Java小球从光标处跑开

Java小球从光标处跑开,java,algorithm,Java,Algorithm,我试图制作一个图形项目,其中一个球从我的光标处跑开,我已经用另一种方式绕着球寻找我的光标,当她到达时,她会失去速度,所以就像她跑得很快,直到她达到10像素的范围,然后她会失去速度,直到她碰到光标 问题是,我找不到让球从光标处跑开的方法,当我输入一个直径(从球开始)时,她跑得很慢,如果我靠近得更多,她开始跑得更快以便跑开,但当我的光标离开直径时,她跑得很慢,直到她再次停止 我希望我说得很清楚,我考虑过一个解决方案,但我不知道是否有Java中的库或内置函数可供我使用: -有一个从0到100的百分比,

我试图制作一个图形项目,其中一个球从我的光标处跑开,我已经用另一种方式绕着球寻找我的光标,当她到达时,她会失去速度,所以就像她跑得很快,直到她达到10像素的范围,然后她会失去速度,直到她碰到光标

问题是,我找不到让球从光标处跑开的方法,当我输入一个直径(从球开始)时,她跑得很慢,如果我靠近得更多,她开始跑得更快以便跑开,但当我的光标离开直径时,她跑得很慢,直到她再次停止

我希望我说得很清楚,我考虑过一个解决方案,但我不知道是否有Java中的库或内置函数可供我使用: -有一个从0到100的百分比,其中我的光标和球之间的距离适合内部,0%是速度=0,100%是速度=4例如,你知道我是否可以实现这样的事情吗

提前谢谢你

我制作了一个向量类,在这里我改变它,访问X和Y坐标,使球移动,我使用基本的三角学使向量始终保持相同的长度

我的球(追逐者)等级代码:


}

如果你想把球推过来,你可以做一些简单的事情。让我们使用向量使其更容易理解。假设
ball
保持球的中心(x,y),而
mouse
包含鼠标位置(x,y)

您可以计算球和鼠标之间的距离,即
(鼠标-球).length()
,以获得鼠标与球的距离

如果距离>球半径,则鼠标位于外侧

否则,您可以执行以下操作:

tmp =  ball - mouse  // get the vector from mouse to the ball.
tmp = tmp / tmp.length() * ball_radious  // change the vector's length to match the radious of the ball.
ball = mouse + tmp  // Move the ball such that the mouse will be on the edge.
当你移动鼠标时,球会被鼠标推动。 如果你想要一点惯性,当你不再推球时,球不会停下来,那么你需要保持一个额外的矢量速度,并使用tmp来获得加速度。 大概是这样的:

tmp =  ball - mouse  // get the vector from mouse to the ball.
force = max(0, ball_radious - tmp.length())  // how strong we push the ball.
acceleration = tmp / tmp.legnth() * f(force)  // compute the acceleration vector. f(force) is some function based on force, try k*f or k*f*f and see what looks better for your setup.
speed = speed * kDrag + acceleration  // update the speed, kDrag should be between 0 and 1, start with something like 0.8 and try different values.
ball = ball + speed * time_delta // Update the ball's position.
您可以使用常量来获得所需的正确感觉。time_delta旨在使FRAM之间的速度正常化,因此,如果FRAM之间存在一些不一致,则无需太过担心。您也可以使用常量,但运动有时可能会变得急促


以上所有操作都是向量操作

您的
setDirection
方法末尾有一个分号,不应该在那里,请删除它<代码>公共无效设置方向(点m){…}@Zabuca谢谢你没注意到!“我无法找到一种方法使球从光标处跑开,当我输入一个直径(从球处)时,她跑得很慢,如果我靠近得更多,她开始跑得更快以离开,但当我的光标离开直径时,她跑得很慢,直到她再次停止。”好吧,这里有两个问题。根据球的直径计算速度的公式是什么。然后球和光标之间的距离如何影响速度。第一个是你的选择,第二个是根据距离与我们的比例。
tmp =  ball - mouse  // get the vector from mouse to the ball.
force = max(0, ball_radious - tmp.length())  // how strong we push the ball.
acceleration = tmp / tmp.legnth() * f(force)  // compute the acceleration vector. f(force) is some function based on force, try k*f or k*f*f and see what looks better for your setup.
speed = speed * kDrag + acceleration  // update the speed, kDrag should be between 0 and 1, start with something like 0.8 and try different values.
ball = ball + speed * time_delta // Update the ball's position.