Forum Archive

Show all scene child nodes

imnegan

Hi all,

I am building a model of the solar system using the Scene class. In the model each planet is a child node of the sun, and the moons are child nodes of their respective planets.

Is there a way to show all child nodes of a selected node on screen without adding them as child node of the Scene itself? For example: Jupiter, when selected, is at the centre of the screen as the parent node. All of its moons (Io, Europa, etc) are Jupiter's child nodes and would be shown also.

ccc

It is hard to answer these questions without working code. My sense is that a sprite's children need to be inside its frame while the planets orbit around the outside of the sun's frame. I am no good with scene animations but we need some sample code of a rotating planet in an elliptical orbit around a star... (help needed!)

```python
import scene

'''
Trying to create a rotating earth in an eliptical orbit around the sun.
'''

class SolorSystemScene(scene.Scene):
def setup(self):
center = self.bounds.center()
sun = scene.SpriteNode('plc:Star', position=center, parent=self)
earth = scene.SpriteNode('plc:Tree_Ugly', position=center + (150, 150),
parent=self, speed=0.2)
earth.run_action(scene.Action.rotate_by(10))
earth.run_action(scene.Action.move_to(center.x - 150, center.y - 150))

scene.run(SolorSystemScene())
```

abcabc

Hope this helps


import scene
from math import pi

'''
Trying to create a rotating earth in an eliptical orbit around the sun.
'''

class SolorSystemScene(scene.Scene):
    def setup(self):
        center = self.bounds.center()
        sun = scene.SpriteNode('plc:Star', position=center, parent=self)
        earth_anchor = scene.Node(position=center, parent=self)
        earth = scene.SpriteNode('plc:Tree_Ugly', position=(0, 150))
        earth_anchor.add_child(earth) 
        self_rotate_action = scene.Action.repeat(scene.Action.sequence(scene.Action.rotate_to(20*pi, 5),
            scene.Action.rotate_to(0,0), 0),0)      
        earth.run_action(self_rotate_action)
        rotate_action = scene.Action.repeat(scene.Action.sequence(scene.Action.rotate_to(20*pi, 20),
            scene.Action.rotate_to(0,0), 0),0)
        earth_anchor.run_action(rotate_action)

scene.run(SolorSystemScene())
ccc

Cool... more appropriate icons, clockwise rotation, etc...

Edit: counter clockwise rotation and orbit in alignment with @JonB comments below...

import scene
from math import pi

'''
Trying to create a rotating earth in an eliptical orbit around the sun.
'''


class SolorSystemScene(scene.Scene):
    def setup(self):
        center = self.bounds.center()
        sun = scene.SpriteNode('emj:Sun_1', position=center, parent=self)
        earth_anchor = scene.Node(position=center, parent=self)
        earth = scene.SpriteNode('emj:Moon_5', position=(0, 150))
        earth_anchor.add_child(earth)
        A = scene.Action
        self_rotate_action = A.repeat(A.sequence(A.rotate_to(20 * pi, 5),
                                                 A.rotate_to(0, 0), 0), 0)
        earth.run_action(self_rotate_action)
        rotate_action = A.repeat(A.sequence(A.rotate_to(20 * pi, 20),
                                            A.rotate_to(0, 0), 0), 0)
        earth_anchor.run_action(rotate_action)

scene.run(SolorSystemScene())
JonB

In this solar system, all the planets orbit counterclockwise, and most rotate that way too...

ccc

Thanks @JonB I corrected inline above... Now all is right with the universe ;-). Happy 2017!

ccc

More Planets...

import scene
from math import pi

A = scene.Action

'''
Trying to create a rotating earth in an eliptical orbit around the sun.
'''

def make_action(steps, speed):
    return A.repeat(A.sequence(A.rotate_to(steps * pi, speed),
                               A.rotate_to(0, 0), 0), 0)


class Planet(scene.Node):
    def __init__(self, parent, orbit=150, image_name=''):
        parent.add_child(self)
        self.position = parent.bounds.center()
        self.run_action(make_action(20, orbit / 5))
        image_name = image_name or 'emj:Moon_5'        
        planet = scene.SpriteNode(image_name, parent=self, position=(0, orbit))
        planet.run_action(make_action(20, 5))


class SolorSystemScene(scene.Scene):
    def setup(self):
        center = self.bounds.center()
        sun = scene.SpriteNode('emj:Sun_1', position=center, parent=self)
        earth = Planet(self)
        venus = Planet(self, 75, 'emj:Blue_Circle')
        mars = Planet(self, 225, 'emj:Moon_2')
        saturn = Planet(self, 300, 'emj:Moon_4')
        jupiter = Planet(self, 375, 'emj:Moon_1')

scene.run(SolorSystemScene())