简体   繁体   English

2D轨道物理模型显示出奇怪的行为

[英]2D orbital Physics model showing strange behaviour

I'm trying to create a 2D environment which simulates basic orbital physics using pygame. 我正在尝试创建一个2D环境,以使用pygame模拟基本的轨道物理。 However when I run it, it exhibits some weird behaviour (edit: and some weirder behaviour )such as the satellite bouncing off the x and y axes, and the satellite moving in a wave like pattern along the axis toward the sun. 但是,当我运行它时,它表现出一些怪异的行为 (编辑:和一些怪异的行为 ),例如卫星从x和y轴上弹起,并且卫星以类似波状的模式沿着轴向太阳移动。 Below is the code that calculates the acceleration of a body caused by the central sun. 以下是计算由中心太阳引起的物体加速度的代码。 BM is body mass, BP is Body position [x,y], SM is sun mass and SP is sun position [x,y]. BM是体重,BP是身体位置[x,y],SM是太阳质量,SP是太阳位置[x,y]。

def CalcGrav(BM,BP,SM,SP):  
    Dist = SubList(BP,SP)  
    Mass = BM*SM/100  
    for i in range(0,2):  
        if(Dist[i] == 0):  
            Dist[i] += 0.01  
    AV = [-(Mass/Dist[0]),-(Mass/Dist[1])]  
return AV  

The function is then used in the following context: 然后在以下上下文中使用该函数:

TraceAcc = CalcGrav(BallMass,TracePosition,SunMass,SunPosition)
TraceVector = SumList(TraceVector, TraceAcc)
TracePosition = SumList(TracePosition,TraceVector)

SumList(A,B) simply adds the items of a list together: (1,3) + (2,6) = (3,9) SumList(A,B)只需将列表中的各项加在一起:(1,3)+(2,6)=(3,9)
The current velocity vector and acceleration vector are stored as lists: [x,y] 当前速度矢量和加速度矢量存储为以下列表:[x,y]

Can anyone explain the strange behavior or show me what I've done wrong? 谁能解释这个奇怪的行为或告诉我我做错了什么?
Thanks. 谢谢。

The problem is that you are not constructing a realistic physics model of orbital motion. 问题在于您没有构建逼真的轨道运动物理模型。

What you need to do for a somewhat realistic simulation, is construct a simplified Kepler orbit model. 要进行某种程度逼真的仿真,您需要做的是构造一个简化的开普勒轨道模型。 You also will need to work in polar coordinates (radius, angle) and convert to x, y coordinates. 您还需要处理极坐标(半径,角度)并将其转换为x,y坐标。

What you will need to do is construct a Kepler orbit equation for an elliptical orbit in code. 您需要做的是为代码中的椭圆轨道构造开普勒轨道方程。

The Kepler equation looks like (see the Wikipedia article on orbit modeling for more information) 开普勒方程看起来像(有关更多信息,请参见Wikipedia上有关轨道建模的文章

在此处输入图片说明

This describes the radius of the orbit r in terms of the angle of the orbit v (actually Greek nu), where the angle is zero at "periapsis" which means the point of closest approach of orbiting body and central body. 这以轨道v(实际上是希腊语nu)的角度来描述半径r,其中在“ periapsis”处的角度为零,这意味着轨道体和中心体最接近的点。

You will need to define your own eccentricity (e) as some value between 0 and 1 - I suggest something close to the Earth's orbital eccentricity, approximately 0.02. 您需要将自己的偏心率(e)定义为介于0到1之间的某个值-我建议您设置一些接近地球轨道偏心率的值,即0.02。 That will give you a nearly circular orbit. 那会给你一个接近圆形的轨道。 If you want a more elliptical one, increase the eccentricity, but not more than 1.0. 如果您想要一个更椭圆的椭圆,请增加偏心率,但不要超过1.0。

Similarly a is the semimajor axis, ie half the longest axis of the ellipse. 类似地,a是半长轴,即椭圆的最长轴的一半。 Either define a value that makes sense for your game coordinates or if you want to compute it using the body masses, you will need to look up some references on Kepler orbits, start with the Wikipedia page above. 定义一个对您的游戏坐标有意义的值,或者如果您想使用人体质量来计算它,则需要从上述Wikipedia页面开始,在开普勒轨道上查找一些参考。

Even the Kepler model is vastly simplified from real orbital mechanics, but it will at least produce a semi realistic game simulation. 即使开普勒模型也从真实的轨道力学中大大简化了,但至少会产生半现实的游戏模拟。

Turns out it was much simpler than that. 事实证明,这比这简单得多。 I was dividing by individual x,y distances, when what I needed to do was work out the vector towards the sun, calculate the straight line distance between the two objects, then divide both parts of the vector by that distance squared: 我要除以各个x,y距离,当我需要做的是朝着太阳算出向量时,计算两个对象之间的直线距离,然后将向量的两部分除以该距离的平方:

def CalcGrav(MM,MP,SM,SP):
    Vect = SubList(SP,MP)
    D = math.sqrt(Vect[0]**2 + Vect[1]**2)**2
    Vect = TimesList(Vect,[1/D,1/D])
    return Vect

Try a smaller step (smaller distance increment). 尝试较小的步长(较小的距离增量)。 0.002 rather than 0.01 for instance. 例如0.002,而不是0.01。 I've had a similar problem in my own orbital physics lab and this worked. 我在自己的轨道物理实验室中遇到了类似的问题,并且这种方法行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM