Calculating angles

All about writing scripts for Celestia in Lua and the .cel system
Topic author
Malenfant
Posts: 1412
Joined: 24.08.2005
With us: 19 years 3 months

Calculating angles

Post #1by Malenfant » 26.10.2005, 18:44

For my planetary magnitudes script that I'm working on, I need to find the phase angle - the angle between the Sun-Body and Body-Observer lines.

It's not remotely obvious to me how to do this in a script though. Probably something to do with vectors I guess?

(this'd be one for the wikibook if we can figure it out, definitely :)).

cpotting
Posts: 164
Joined: 18.03.2004
Age: 63
With us: 20 years 8 months
Location: Victoria, BC Canada

Re: Calculating angles

Post #2by cpotting » 26.10.2005, 22:19

Malenfant wrote:For my planetary magnitudes script that I'm working on, I need to find the phase angle - the angle between the Sun-Body and Body-Observer lines.

It's not remotely obvious to me how to do this in a script though. Probably something to do with vectors I guess?

(this'd be one for the wikibook if we can figure it out, definitely :)).


I'm not currently near a copy of Celestia, so I can't say that this is correct, but it should be:

Code: Select all

sun_planet_vector = planet:getposition() - sun:getposition()
obs_planet_vector = planet:getposition() - celestia:getobserver():getposition()
phase_angle = math.arcos(sun_planet_vector:normalize() ^ obs_planet_vector:normalize())


where phase_angle will be expressed in radians. Essentially, the cross-product (v1^v2 in Celx) of two normalized vectors is the cosine of the angle between the vectors.
Clive Pottinger
Victoria, BC Canada

Topic author
Malenfant
Posts: 1412
Joined: 24.08.2005
With us: 19 years 3 months

Post #3by Malenfant » 26.10.2005, 22:32

Doesn't seem to work (it's acos, btw, not arcos). Keeps saying it expects a number in the phase_angle line but gets userdata instead.

I'll keep fiddling with it, see if I can come up with something... Are you sure that ^ means Cross Product? I thought it was 'to the power of'. Oh, and doesn't cross product find the vector that's perpendicular to both the other vectors you put into it? Don't we need the dot product here?

cpotting
Posts: 164
Joined: 18.03.2004
Age: 63
With us: 20 years 8 months
Location: Victoria, BC Canada

Post #4by cpotting » 26.10.2005, 22:49

Malenfant wrote:Doesn't seem to work (it's acos, btw, not arcos). Keeps saying it expects a number in the phase_angle line but gets userdata instead. [\quote]

I'll keep fiddling with it, see if I can come up with something... Are you sure that ^ means Cross Product? I thought it was 'to the power of'. Oh, and doesn't cross product find the vector that's perpendicular to both the other vectors you put into it? Don't we need the dot product here?


Oops. Try this correction:

Code: Select all

phase_angle = math.acos(sun_planet_vector:normalize() * obs_planet_vector:normalize())


You were right - it is the dot product you need, not the cross-product (I'm trying to do this from memory and I have a lousy memory). In Celx v1*v2 gives the dot product.

^ does mean to the power of, except when used between two vectors, in which case it has been defined in Celx to mean the cross-product.

And finally, the reason you got the error message is that the cross-product returns a vector (as you correctly pointed out). A vector is not a number, and is represented in Lua using a structure called a userdata. Hence theerror message.
Clive Pottinger
Victoria, BC Canada

Topic author
Malenfant
Posts: 1412
Joined: 24.08.2005
With us: 19 years 3 months

Post #5by Malenfant » 26.10.2005, 23:06

Aha, that did it (I think - gotta convert the display of phase_angle to degrees for it to make any sense to me but that's easy enough :)). Thanks muchly!

I should add that none of this is remotely clear to me from any of the CELX documentation that I'm using :( ... I can see why Hank is so keen on the idea of getting stuff in the wikibook...

cpotting
Posts: 164
Joined: 18.03.2004
Age: 63
With us: 20 years 8 months
Location: Victoria, BC Canada

Post #6by cpotting » 26.10.2005, 23:39

Malenfant wrote:Aha, that did it (I think - gotta convert the display of phase_angle to degrees for it to make any sense to me but that's easy enough :)). Thanks muchly!

I should add that none of this is remotely clear to me from any of the CELX documentation that I'm using :( ... I can see why Hank is so keen on the idea of getting stuff in the wikibook...


Well, like the documentation for almost all languages, they tell you what the language will do, not how to make it do something :wink:

You may want to try this site. If you look around, you will find explanations of vectors, quaternions, rotations, and how to use them. That's how I finally figured Celx out. In fact, I just stumbled across a set of equations on the page which may point me in the right direction to get my travel_to() routine to avoid going through planets and stars.
Clive Pottinger
Victoria, BC Canada


Return to “Scripting”