I’m am trying to create asteroids that head toward the Earth, but I am having difficulty getting the position of the Earth. Here are some parts of my code:
class SinglePlayer(DirectObject.DirectObject):
# constructor
def __init__(self, name, main):
self.main = main
#Load Earth
earthnode=Planet(image="models/earth_1k_tex.jpg", pos=Vec3(2.0* ORBITSCALE, 0, 0), name = "earth", scale = (20 * SIZESCALE), parent=orbit_root_earth, day=(2*DAYSCALE), year=(YEARSCALE), environment=self)
self.planets.append(earthnode)
class Planet(DirectObject.DirectObject):
def __init__(self, image, pos, name, scale, parent, day, year, environment):
self.environment = environment
self.parent = parent
self.planet = loader.loadModelCopy("models/planet_sphere")
self.planet_tex = loader.loadTexture(image)
self.planet.setTexture(self.planet_tex, 1)
self.planet.reparentTo(self.parent)
self.planet.setPos(pos)
self.planet.setScale(scale)
self.name = name
class Asteroid(DirectObject.DirectObject):
def __init__(self, pos = None, environment=None, speed=0):
model = "models/asteroid1.egg"
self.env = environment
self.asteroid = loader.loadModel(model)
myNormalMapTex = loader.loadTexture('models/craternormal.png')
self.asteroid.setTexture(myNormalMapTex, 0)
self.asteroid.reparentTo(asteroidDummy)
#make asteroid head toward earth
self.loop = self.asteroid.posInterval(20.0,earthnode.getPos()) #Vec3(0,0,0)
self.loop.start()
When I run the program, it crashes and says ‘earthnode’ is undefined.