1

I am trying to create a class in python for a planet. It simply want to attach an osgb file to this class.

Currently I am adding my models to the program like this...

    import viz
import vizact
import vizmat
import vizcam
import Planet

viz.setMultiSample(10)
viz.fov(80)
viz.go()
viz.collision(viz.ON)
viz.phys.enable()

#mercury
mercury = viz.addChild('planets/mercury.osgb')
mercury.setPosition([20,0,10])
mercury.setAxisAngle([1,0,0,90])

#venus
venus = viz.addChild('planets/venus.osgb')
venus.setPosition([20,0,20])
venus.setAxisAngle([1,0,0,90])

#earth
earth = viz.addChild('planets/earth.osgb')
earth.setPosition([20,0,30])
earth.setAxisAngle([1,0,0,90])

#mars
mars = viz.addChild('planets/mars.osgb')
mars.setPosition([20,0,40])
mars.setAxisAngle([1,0,0,90])

#jupiter
jupiter = viz.addChild('planets/jupiter.osgb')
jupiter.setPosition([20,0,50])
jupiter.setAxisAngle([1,0,0,90])

#saturn
saturn = viz.addChild('planets/saturn.osgb')
saturn.setPosition([100,0,60])
saturn.setAxisAngle([1,0,0,0])

#uranus
uranus = viz.addChild('planets/uranus.osgb')
uranus.setPosition([-50,0,70])
uranus.setAxisAngle([0,1,0,90])

#neptune
neptune = viz.addChild('planets/neptune.osgb')
neptune.setPosition([20,0,80])
neptune.setAxisAngle([1,0,0,90])

#pluto
pluto = viz.addChild('planets/pluto.osgb')
pluto.setPosition([20,0,90])
pluto.setAxisAngle([1,0,0,90])

BirdEyeWindow = viz.addWindow()
BirdEyeWindow.fov(60)
BirdEyeWindow.visible(0,viz.SCREEN)
BirdEyeView = viz.addView()
BirdEyeWindow.setView(BirdEyeView)
BirdEyeView.setPosition([0,250,0])
BirdEyeView.setEuler([0,90,0]) 


MOVE_SPEED = 50;
TURN_SPEED = 60;

viz.mouse(viz.OFF)

view = viz.MainView;

#Keyboard interaction to move the view point around
def updateCam():
    if viz.key.isDown(viz.KEY_UP):
            view.move([0,0,MOVE_SPEED*viz.elapsed()],viz.BODY_ORI)
    elif viz.key.isDown(viz.KEY_DOWN):
            view.move([0,0,-MOVE_SPEED*viz.elapsed()],viz.BODY_ORI)
    elif viz.key.isDown(viz.KEY_RIGHT):
            view.setEuler([TURN_SPEED*viz.elapsed(),0,0],viz.BODY_ORI,viz.REL_PARENT)
    elif viz.key.isDown(viz.KEY_LEFT):
            view.setEuler([-TURN_SPEED*viz.elapsed(),0,0],viz.BODY_ORI,viz.REL_PARENT) 

vizact.ontimer(0,updateCam)



#selecting Details of planets
#Called when user clicks on an object
def pickEarth():
    print 'Mouse Clicked'
    object = viz.pick() #detects object mouse is currently over
    if object.valid(): 
        print 'Clicked on a planet'
        print object



vizact.onmousedown(viz.MOUSEBUTTON_LEFT, pickEarth)

However I would rather have a class called planet and have each class linked to an osgb model.

I have a looked around the web and cannot seem to find any resources on how to do this. I'd appreciate any help on how I might modify this code above so that is uses different instances of a Planet object.

Javacadabra
  • 5,578
  • 15
  • 84
  • 152

1 Answers1

0

I'm not sure if this is what you want, but you could do the following:

class MyPlanet:
    def __init__(self, osgb_path):
        self.viz_ref = viz.addChild(osgb_path)
        self.osgb = osgb_path
    def setPosition(self, coordinates):
        self.viz_ref.setPosition(coordinates)
    def setAxisAngle(self, coordinates):
        self.viz_ref.setAxisAngle(coordinates)

mercury = MyPlanet('planets/mercury.osgb')
mercury.setPosition([20, 0, 10])
mercury.setAxisAngle([1, 0, 0, 90])

This is just a wrapper around whatever viz.addChild(...) returns, though, so I don't really see the value of having this.

senshin
  • 10,022
  • 7
  • 46
  • 59
  • Looks like a good option. Ideally in the end each class will then have a osgb path model and a name associated with it. – Javacadabra Jan 20 '14 at 16:09
  • @Javacadabra Do you mean instance? `MyClass` is a class, `mercury` is an instance of this class. There is no point (I suppose) in having a different class with for each `osgb` file. Also maybe a simple dictionary like `planets = {'planet_name' : viz.addChild(osgb_path) }` would do good if the problem is having a nice collection for the planetes. – luk32 Jan 20 '14 at 16:11