试着让两个物体互相环绕…变得很奇怪

2024-10-01 11:33:49 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我已经创造了质量,给了它们一些速度和动量,我试着利用重力使它们相互环绕(围绕着它们的质心)。在

from visual import *

earth = sphere(radius = 100000000)
newPlanet = sphere(pos = (3.84403*10**8, 0, 0), radius = 10000000)

earth.velocity = vector(0, 100, 0)
newPlanet.velocity = vector(0, 100, 0)

earth.mass = 2*10**30
newPlanet.mass = 1*10**30

earth.p = vector(0, earth.mass*earth.velocity, 0)
newPlanet.p = vector(0, newPlanet.mass*newPlanet.velocity, 0)

dt = 1000
r = newPlanet.pos.x
T = 1.296*10**6
G = 6.673*10**-11

while 1:
    Fnet = G*((earth.mass*newPlanet.mass)/r**2)

    earth.p += Fnet*dt
    newPlanet.p += Fnet*dt

    earth.velocity += (earth.p/earth.mass)*dt
    newPlanet.velocity += (newPlanet.p/newPlanet.mass)*dt

    earth.pos += earth.velocity*dt
    newPlanet.pos += newPlanet.velocity*dt

    t += 1

    rate(100)

这是我得到的错误:

^{pr2}$

Tags: pos利用dt质量动量速度mass重力
1条回答
网友
1楼 · 发布于 2024-10-01 11:33:49

vector接受三个数字作为参数,如vpython文档here所示

{{cd2>中的{cd2}不是你的赋值。在

因此出现错误消息,您确定您不是指

earth.p = vector(0, earth.mass*mag(earth.velocity), 0)

或者改为earth.p = vector(0, earth.mass*earth.velocity.y, 0)。在

相关问题 更多 >