Объекты созданы, но в версии ООП нет движения - PullRequest
0 голосов
/ 13 октября 2018

Я кодирую ООП версию солнечной системы.Когда я запускаю его, объекты создаются, также расстояния и радиус верны.Но планеты не двигаются.Но у меня есть функция, поэтому они должны ходить вокруг солнца.Я получаю ошибку имени: 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)

Ответы [ 2 ]

0 голосов
/ 13 октября 2018

Вы пытались в методе 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)
0 голосов
/ 13 октября 2018
def setPosition():
    #planetSphere.pos = self.pos
    print(planetSphere.pos)

Определяется внутри класса планеты.Итак, первый аргумент, который ожидают методы - это «я».

Кроме того, следуя мысли, это может быть то, что вы ищете:

def setPosition(self,newPos):
    planetSphere.pos = newPos
    print(planetSphere.pos)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...