C++ Question Problems with determining sun angle - Solved

CigDriver

Donator
Donator
Joined
Mar 27, 2008
Messages
194
Reaction score
0
Points
0
I'm working on a bit of code to determine sun angle to a set of solar arrays. Overall the code works but there is some weirdness. I'm sure it something simple, but I haven't been able to figure it out yet :)

Here is my code:
Code:
//Check to see what the angle between the sun and solar arrays
    VECTOR3 rspos, lspos;//holder for relative sun position, local sun position
    VECTOR3 lppos = {0,0,1};//solar panels always point along z axis
    VESSEL * v = oapiGetVesselInterface(vesselObject);//identify our vessel
    OBJHANDLE h1 = oapiGetObjectByName("Sun");//identify the sun
    v->GetRelativePos(h1, rspos); //relative sun position
    Global2Local(rspos,lspos);//convert relative postion to local position
    lspos = lspos/length(lspos);//normalize local position
    sunAngle = (((acos(dotp(lspos,lppos)))*(180/PI))+6.2);//calculate angle "+6.2" to get 90 degrees when directly facing sun
It is loosely based on tblaxland's planet shadow code that I found here on the board, that code works great, I've got a nice little indicator on the hud (for testing) that tells me if I'm in the shadow of the planet I'm orbiting. My code has a few weird things, and an unxepcted output. this is most likely due to my lack of knowledge of vectors ;)

The thing that really mystifies me is that the output is off by ~6.2 degrees. Once I added that in I get 90 when I'm looking at the sun. Anyone know what makes that happen? the second thing that I'm unclear about is why I get 90 degrees when looking at the sun, I expected 0. I'm getting the angle between the z axis of the ship and the position of the sun in normalized local coordinates. It seems to me that should 0 when they are aligned, but I am vectors noob :lol: It is also possible that you can't do what I'm doing and it just happens to look like it is working.

Any feedback is greatly appreciated!
 
Last edited:

martins

Orbiter Founder
Orbiter Founder
Joined
Mar 31, 2008
Messages
2,448
Reaction score
462
Points
83
Website
orbit.medphys.ucl.ac.uk
solar panels always point along z axis
I presume you mean "the normals of the solar panels always point along z axis"?

v->GetRelativePos(h1, rspos); //relative sun position
This does not return the sun's relative position. It returns your own position relative to the sun (which is the negative of the sun's position relative to you) - so you have to change the sign. Note that the sun's global position is approximately (0,0,0). so your position relative to the sun is nearly the same as your absolute position, i.e. the mapping from global to local is not really required.

The "6.2" is suspiciously close to 2pi, but since you are adding it to the degrees, not the radians, this is probably a coincidence.

Edit:
An alternative approach is this:

  • take the negative of your global position (=sun's relative position)
  • normalise to 1 (=sun's relative direction)
  • perform an inverse rotation (tmul) using the rotation matrix you get from GetRotationMatrix
  • then get the angle from the direction cosine as before.
 

CigDriver

Donator
Donator
Joined
Mar 27, 2008
Messages
194
Reaction score
0
Points
0
Thanks Martins. Now it is time to go back to school and learn vectors :)
 

martins

Orbiter Founder
Orbiter Founder
Joined
Mar 31, 2008
Messages
2,448
Reaction score
462
Points
83
Website
orbit.medphys.ucl.ac.uk
Ah well, it's all just linear algebra - no difficulties involved. It just takes a bit of getting used to the different reference frames and transforming between them.
 

CigDriver

Donator
Donator
Joined
Mar 27, 2008
Messages
194
Reaction score
0
Points
0
After a bit of reading and Martins suggestion I got it figured out. It is dead on now :)

For those interested here is the code:
Code:
    //Check to see what the angle between the sun and solar arrays
    VECTOR3 lspos, rspos;//holder for relative position from sun
    VECTOR3 lppos = {0,0,1};//solar panels tangent always point along z axis
    VESSEL * v = oapiGetVesselInterface(vesselObject);//identify our vessel
    v->GetGlobalPos(rspos); //relative sun position
    rspos = -rspos/length(rspos);//invert and normalize sun relative position
    MATRIX3 R;//holder for rotation matrix
    v->GetRotationMatrix(R);//get rotation matrix
    lspos = tmul(R,rspos);//inverse rotation
    sunAngle = (((acos(dotp(lspos,lppos)))*(180/PI)));//calculate angle

thanks again for pointing me in the right direction!
 
Top