【问题标题】:Objects are created but there is no movement in OOP Version对象已创建,但 OOP 版本中没有移动
【发布时间】:2018-10-12 22:28:42
【问题描述】:

我正在编写一个 OOP 版本的太阳系。当我运行它时,对象被创建,距离和半径也是正确的。但是行星不会移动。但我有一个功能,所以他们应该绕着太阳转。我收到一个名称错误:NameError:name 'planetSphere' is not defined。我尝试删除 def setspeed 部分。然后我可以通过打印看到它的工作,因为轴的数量发生了变化,但我看不到模拟本身中行星的任何变化。完整代码:https://trinket.io/python/e2b520c570

 planets = []


class Sphere(object):
    def __init__(self, pos, radius, make_trail):
        self.pos = pos
        self.radius = radius
        self.make_trail = make_trail



class planet(Sphere):

    def __init__(self, pos, radius, make_trail, mass, velocity):
        super().__init__(pos, radius, make_trail)
        self.mass = mass
        self.velocity = velocity
        planetSphere = sphere (pos = self.pos, radius = self.radius, make_trail = self.make_trail, mass = self.mass, velocity = self.velocity)

    def setPosition(self, newPos ):
        planetSphere.pos = newPos
        print(planetSphere.pos)


sun = planet(pos=vec(0,0,0),radius=s_rad1*1.5, make_trail=True, mass=2e30, velocity=vec(0,0,0))

#Other objects

planets.extend((mercury,venus,earth,mars,jupiter,saturn,uranus,neptun,pluto))

dt = 10000
time = 0.1


while True:

    rate(framerate) 
    print(earth.pos)



    #for-Schlaufe für Berechnung jedes einzelnen Planeten


    for planet2 in planets:
        g_force = vec(0,0,0)
        for planet1 in planets:
            if planet2 != planet1:
                g_force += g * planet1.mass * planet2.mass * (planet1.pos - planet2.pos).norm()  / (planet1.pos - planet2.pos).mag2
        #print((sun.pos - planet.pos).mag2)




        #print(sun.pos)

        #Änderung des Velocity Vektor wird zum alten addiert
        #Da a=F/m // V = a*t(a*dt) 2 Geschw. vektoriell durch F/m ausgedrückt.
            planet2.velocity = planet2.velocity + ( (g_force) / planet2.mass) * 1000 #Richtungsänderung

        #Diese Änderung wird zur alten Position addiert = neue Position
            planet2.pos += planet2.velocity * 1000
            newPos = planet2.pos
            planet2.setPosition(newPos)

【问题讨论】:

    标签: python arrays oop physics vpython


    【解决方案1】:
    def setPosition():
        #planetSphere.pos = self.pos
        print(planetSphere.pos)
    

    在行星类内部定义。所以方法期望的第一个参数是“self”。

    此外,按照思路,这可能是您正在寻找的:

    def setPosition(self,newPos):
        planetSphere.pos = newPos
        print(planetSphere.pos)
    

    【讨论】:

    • 我已将您的建议纳入代码。我不再收到此错误,但它仍然无法正常工作。我已经在上面的代码中更改了它并给出了进一步的解释
    • 不要在运行中更新问题。未来的人可能需要与您相同的信息,并遇到一个不再存在的问题的答案。这是一个为明确的问题提供明确答案的网站,而不是提供持续的支持。
    【解决方案2】:

    你试过在plane类的init方法中制作planetSphere => self.planetSphere吗?这就是我的意思:

    class planet(Sphere):
    
    def __init__(self, pos, radius, make_trail, mass, velocity):
        super().__init__(pos, radius, make_trail)
        self.mass = mass
        self.velocity = velocity
        self.planetSphere = sphere (pos = self.pos, radius = self.radius, make_trail = self.make_trail, mass = self.mass, velocity = self.velocity)
    
    def setPosition(self, newPos ):
        self.planetSphere.pos = newPos
        print(planetSphere.pos)
    

    【讨论】:

    • 是的,但行星仍然没有移动。在这里,我可以给你我的整个代码。也许那时你会更好地看到问题。 trinket.io/python/e2b520c570
    • 嘿,它不会让我运行你的代码,但尝试打印你的速度,因为它可能是 0,而你真的根本没有更新你的位置。
    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 2011-01-01
    • 2017-11-29
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多