引力
有谁知道可以处理两个物体的引力的教程?例如。一颗卫星被吸引到月球(并且可能被弹射穿过它)。
我正在开发一个小型 Java 游戏,我想在其中实现他的功能。
我有两个物体之间的引力公式,但是当我尝试在游戏中使用它时,什么也没有发生?
屏幕上有两个物体,其中一个始终静止,另一个以匀速直线运动,直到进入静止物体的检测范围内。此时它应该被吸引到静止的物体上。 首先,我计算两个物体之间的距离,并根据它们的质量和这个距离,更新 x 和 y 坐标。
但就像我说的,什么也没有发生。我没有正确执行公式吗?
我已经包含了一些代码来展示我到目前为止所拥有的内容。 这是粒子与门检测范围碰撞时的实例,并且应该开始被拉向它
for (int i = 0; i < particle.length; i++)
{
// **************************************************************************************************
// GATE COLLISION
// **************************************************************************************************
// Getting the instance when a Particle collides with a Gate
if (getDistanceBetweenObjects(gate.getX(), particle[i].getX(), gate.getY(), particle[i].getY()) <=
sumOfRadii(particle[i].getRadius(), barrier.getRadius()))
{
particle[i].calcGravPull(particle[i].getMass(), barrier.getMass(),
getDistanceBetweenObjects(gate.getX(), particle[i].getX(), gate.getY(), particle[i].getY()));
}
以及我的 Particle 类中进行移动的方法
// Calculate the gravitational pull between objects
public void calcGravPull(int mass1, int mass2, double distBetweenObjects)
{
double gravityPull;
gravityPull = GRAV_CONSTANT * ((mass1 * mass2) / (distBetweenObjects * distBetweenObjects));
x += gravityPull;
y += gravityPull;
}
Does anyone know of a tutorial that would deal with gravitational pull of two objects? Eg. a satellite being drawn to the moon (and possibly sling shot past it).
I have a small Java game that I am working on and I would like to implement his feature in it.
I have the formula for gravitational attraction between two bodies, but when I try to use it in my game, nothing happens?
There are two object on the screen, one of which will always be stationary while the other one moves in a straight line at a constant speed until it comes within the detection range of the stationary object. At which point it should be drawn to the stationary object.
First I calculate the distance between the two objects, and depending on their mass and this distance, I update the x and y coordinates.
But like I said, nothing happens. Am I not implementing the formula correctly?
I have included some code to show what I have so far.
This is the instance when the particle collides with the gates detection range, and should start being pulled towards it
for (int i = 0; i < particle.length; i++)
{
// **************************************************************************************************
// GATE COLLISION
// **************************************************************************************************
// Getting the instance when a Particle collides with a Gate
if (getDistanceBetweenObjects(gate.getX(), particle[i].getX(), gate.getY(), particle[i].getY()) <=
sumOfRadii(particle[i].getRadius(), barrier.getRadius()))
{
particle[i].calcGravPull(particle[i].getMass(), barrier.getMass(),
getDistanceBetweenObjects(gate.getX(), particle[i].getX(), gate.getY(), particle[i].getY()));
}
And the method in my Particle class to do the movement
// Calculate the gravitational pull between objects
public void calcGravPull(int mass1, int mass2, double distBetweenObjects)
{
double gravityPull;
gravityPull = GRAV_CONSTANT * ((mass1 * mass2) / (distBetweenObjects * distBetweenObjects));
x += gravityPull;
y += gravityPull;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的公式有问题。您正在计算重力,然后像加速度一样应用它。加速度是力除以质量,因此您需要将力除以小物体的质量。因此,GRAV_CONSTANT * ((mass1) / (distBetweenObjects * distBetweenObjects)) 是mass2 加速度的公式。
然后,您将其视为位置调整,而不是速度调整(即加速度)。跟踪移动质量的速度,使用它来调整其位置,并使用加速度来改变该速度。
最后,当加速度实际上是矢量时,您将其用作标量。计算从移动质量到静止质量的角度,如果将其表示为与正 x 轴的角度,请将 x 加速度乘以角度的余弦,将 y 加速度乘以角度的正弦。
这将为您提供正确的重力表示。
如果它什么也没做,请检查坐标以查看发生了什么。确保静止质量足够大以产生效果。重力是一种非常弱的力,即使比行星质量小得多,也不会产生显着的影响。
另外,请确保您使用的单位正确的重力常数。您在书中找到的常数是 MKS 系统的常数 - 米、千克和秒。如果您使用公里作为长度单位,则需要将常数乘以一百万,或者将长度乘以一千,然后将其代入公式中。
Your formula has problems. You're calculating the gravitational force, and then applying it as if it were an acceleration. Acceleration is force divided by mass, so you need to divide the force by the small object's mass. Therefore,
GRAV_CONSTANT * ((mass1) / (distBetweenObjects * distBetweenObjects))
is the formula for acceleration of mass2.Then you're using it as if it were a positional adjustment, not a velocity adjustment (which an acceleration is). Keep track of the velocity of the moving mass, use that to adjust its position, and use the acceleration to change that velocity.
Finally, you're using acceleration as a scalar when it's really a vector. Calculate the angle from the moving mass to the stationary mass, and if you're representing it as angle from the positive x-axis multiply the x acceleration by the cosine of the angle, and the y acceleration by the sine of the angle.
That will give you a correct representation of gravity.
If it does nothing, check the coordinates to see what is happening. Make sure the stationary mass is large enough to have an effect. Gravity is a very weak force, and you'll have no significant effect with much smaller than a planetary mass.
Also, make sure you're using the correct gravitational constant for the units you're using. The constant you find in the books is for the MKS system - meters, kilograms, and seconds. If you're using kilometers as units of length, you need to multiply the constant by a million, or alternately multiply the length by a thousand before plugging it into the formula.
你的算法是正确的。您计算的引力可能太小而无法看到。我会删除
GRAV_CONSTANT
并重试。顺便说一句,如果您可以加快在临时变量中移动
getDistanceBetweenObjects()
的结果的速度。Your algorithm is correct. Probably the gravitational pull you compute is too small to be seen. I'd remove
GRAV_CONSTANT
and try again.BTW if you can gain a bit of speed moving the result of
getDistanceBetweenObjects()
in a temporary variable.