Menu in Glowscript

161 Views Asked by At

I have already asked a similar question concerning this problem but my colleague and I still weren't able to fix it. For more information on the problem, please see my first question.

We have defined different objects (sun and planets of our solar system) and the forces between them. Now we want to define a menu which gives us the possibility to choose an object: It will then be defined as the currentobject and the simulation user will be able to vary its mass using a slider. The answer that we got to our first question was that a currentobject must be defined before the function "def M(m)" is used, so we defined the sun as the currentobject. Here are the fragments of our code concerning this part of the simulation:

sun = sphere( pos=vector(0,0,0), radius=6.96E+09, texture="https://i.imgur.com/DTm6i8r.jpg",  

               mass = 1.989e30, momentum=vector(0,0,0), make_trail=True, visible=True )   #example of a defined object

currentobject=sun


def M(m):
    global col, sun, mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, currentobject 
    currentobject.visible = True 
    obj = m.selected 
    if obj == "Sonne": 
        currentobject = sun 
    elif obj == "Merkur":
        currentobject = mercury
    elif obj == "Venus": 
        currentobject = venus
    elif obj == "Erde": 
        currentobject = earth
    elif obj == "Mars": 
        currentobject = mars
    elif obj == "Jupiter": 
        currentobject = jupiter
    elif obj == "Saturn": 
        currentobject = saturn
    elif obj == "Uranus": 
        currentobject = uranus
    elif obj == "Neptun": 
        currentobject = neptune
    currentobject.visible=True

menu(choices=['Sonne', 'Merkur', 'Venus', 'Erde', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptun'], bind=M) 
scene.append_to_caption('\n\n')


scene.append_to_caption("Change mass of chosen object: \n\n") 
def setmass(b): 
    wt.text = '{:1.1f}'.format(b.value)
sl = slider(min=1, max=10, value=1, length=1000, bind=setmass)  
wt = wtext(text='{:1.1f}'.format(sl.value))
scene.append_to_caption('\n\n') 


while (True):  

    rate(100) 

    if running:

        if currentobject = sun:
            sun.mass = (1.989e30)*sl.value
        elif currentobject = mercury:
            mercury.mass = (3.285e23)*sl.value
        elif currentobject = venus:
            venus.mass = (4.869e24)*sl.value
        elif currentobject = earth:
            earth.mass = (5.974e24)*sl.value
        elif currentobject = mars:
            mars.mass = (6.4185e23)*sl.value
        elif currentobject = jupiter:
            jupiter.mass = (1.8987e27)*sl.value
        elif currentobject = saturn:
            saturn.mass = (5.683e26)*sl.value
        elif currentobject = uranus:
            uranus.mass = (8.683e25)*sl.value
        elif currentobject = neptune:
            neptune.mass = (1.0241e26)*sl.value

When the program is run, no error message appears. However, we found out that while the currentobject is changed in the function M, it is not changed afterwards. (When you insert "print (currentobject.mass) in "def M(m)", the chosen objects mass appears in the simulation but when you insert it after the function, the suns mass appears). This means that the calculation always uses the sun as its currentobject and not the object that is chosen with the menu.

1

There are 1 best solutions below

3
On

The only 3D object you have created is "sun". When in M() you say currentobject = mercury, you have set currentobject to "undefined" because there is no 3D object named "mercury".