Page 1 of 1

question re: getchildren() and for loops

Posted: 28.10.2005, 22:49
by Malenfant
OK. My next project is to write a script that allows you to go to a barycentre and stop at a distance where you can see both stars that orbit it on the screen.

Now, the first thing I need for this I think is to determine the vectors from the observer to each star around the barycentre and then find the angle between them (like the phase angle calculation). The calculation should be fairly straightfoward, but I'm having trouble figuring out how to get the script to do this.

I think what I need is the getchildren command, but that seems to get EVERYTHING that is a 'child' of the barycentre, which would be stars, planets etc around the stars, and moons around the planets. How do I just get the stars out of this - and then 'isolate' them so that the script can get the vectors to each star from the observer?

I found this code on the CELX wiki that uses getchildren. I kinda get what it's doing - it's finding the 'children' of Saturn that have type "moon", but I don't understand syntax of the For statement (what's index? Why "index,body"? What's ipairs?. Harald's CelX page just shows the syntax, doesn't really explain what's going on there...

Code: Select all

-- tour the moons of Saturn
 obs = celestia:getobserver()
 planet = celestia:find("Saturn")
 obs:goto(planet)
 wait(5)
 flash("Welcome to "..planet:name())
 moons = planet:getchildren()
 for index,body in ipairs(moons) do
    if body:type() == "moon" then
        obs:follow(body)
        obs:goto(body)
        wait(5)
        flash("Welcome to "..body:name())
        wait(1)
    end
  end

Re: question re: getchildren() and for loops

Posted: 29.10.2005, 03:39
by hank
Malenfant wrote:I think what I need is the getchildren command, but that seems to get EVERYTHING that is a 'child' of the barycentre, which would be stars, planets etc around the stars, and moons around the planets. How do I just get the stars out of this - and then 'isolate' them so that the script can get the vectors to each star from the observer?

I found this code on the CELX wiki that uses getchildren. I kinda get what it's doing - it's finding the 'children' of Saturn that have type "moon", but I don't understand syntax of the For statement (what's index? Why "index,body"? What's ipairs?. Harald's CelX page just shows the syntax, doesn't really explain what's going on there...


The ipairs function called in the for statement here is a Lua built-in function that returns in succession each index/value pair in an array (a table with integer indicies). In this case the index is not used, but is specified because that's what ipairs returns. The getchildren method returns an array containing all the associated objects (not just moons). The if statement inside the loop selects the objects in the array which are moons based on the results of calling the type method. You would test for the type "star" to select only stars.

- Hank

Posted: 29.10.2005, 03:53
by Malenfant
So basically I can just copy that code as is and replace the relevant variables? OK... I'll give that a go.

So what does a 'table' look like anyway? It sounds like it's a two column table with an index number and the data next to it. Is it possible to 'print out' a whole table somehow?

Posted: 29.10.2005, 04:28
by Malenfant
Crap.

Code for a binary orbiting a barycentre:

Code: Select all

71683 "ALF Cen A" # component A
{
OrbitBarycenter "ALF Cen"
...



Code for a planet orbiting a star:

Code: Select all

"Earth" "Sol"



To be considered a child of the barycenter, wouldn't the star have to be listed as:

Code: Select all

"Alf Cen A" "ALF Cen"


If so does this means it's impossible to use getchildren to find the stars around a barycentre? How would you do this, if this isn't how you do it? Would getinfo on the barycentre provide this information?

Posted: 29.10.2005, 05:56
by hank
Malenfant wrote:So basically I can just copy that code as is and replace the relevant variables? OK... I'll give that a go.

So what does a 'table' look like anyway? It sounds like it's a two column table with an index number and the data next to it. Is it possible to 'print out' a whole table somehow?

Just use a for loop.

- Hank