[Maths] Get the rotation values to the sun
Moderators: Moderators for English X Forum, Scripting / Modding Moderators
[Maths] Get the rotation values to the sun
Hey,
5-billion dollar question: how to get alpha, beta and gamma rotation values to make my SPP rotate towards the sun, from which I only know x, y and z.
I'll drink a glass of scotch for you if you find the answer!
(Pandas drink scotch, that's a known fact)
5-billion dollar question: how to get alpha, beta and gamma rotation values to make my SPP rotate towards the sun, from which I only know x, y and z.
I'll drink a glass of scotch for you if you find the answer!
(Pandas drink scotch, that's a known fact)
Re: [Maths] Get the rotation values to the sun
Disclaimer: my X3 scripting knowledge is limited as I have mostly only tinkered/tweaked/bugfixed scripts and MDs made by others. Do correct me if I say something blatantly wrong.
Assuming you can know both the XYZ positions of the station and the sun, I would use trigonometry to first find the alpha (ecliptic) angle, and then the beta (pitch) angle. I'm not sure gamma is needed at all. The main issues are:
Assuming you can know both the XYZ positions of the station and the sun, I would use trigonometry to first find the alpha (ecliptic) angle, and then the beta (pitch) angle. I'm not sure gamma is needed at all. The main issues are:
- that I have no knowledge of a function to get the sector's sun's position in X3 script language,
- that trigonometry involves a bunch of divisions and square roots and the X3 script engine doesn't do float variables, discarding the fractionary part of a number after every operation thus effectively rounding down into an int,
- that in my knowledge there are no inverse trigonometric functions such as arcsine in the in-game functions library. This is needed to get an angle from the coordinates. However, arcsine can be very closely approximated with a formula using only basic operations, pi, and square root.
Re: [Maths] Get the rotation values to the sun
From my past experience, gamma is indeed not necessary.
1/ I have the sun position through an injection from an external program.
2/ Ints are limited to 2.147 billion, maybe we can multiply and divide... That's the hard part, because I suppose we want precision...
3/ I read some formulas using arctan2 and arcsine, and there are indeed no built-in functions for those (the Maths library is very poor).
If that's not too much hassle, it would be interesting to know the answer. I'm terrible at maths, and I'm not even sure to understand what these rotation values mean, but I can try!
EDIT: let's ignore 2 and 3, because I can probably do the same technique as I used in 1 to inject the result into X3 (it's late, I didn't think of that when first writing my post ).
EDIT2: meaning I have an ASin() function and floats.
1/ I have the sun position through an injection from an external program.
2/ Ints are limited to 2.147 billion, maybe we can multiply and divide... That's the hard part, because I suppose we want precision...
3/ I read some formulas using arctan2 and arcsine, and there are indeed no built-in functions for those (the Maths library is very poor).
If that's not too much hassle, it would be interesting to know the answer. I'm terrible at maths, and I'm not even sure to understand what these rotation values mean, but I can try!
EDIT: let's ignore 2 and 3, because I can probably do the same technique as I used in 1 to inject the result into X3 (it's late, I didn't think of that when first writing my post ).
EDIT2: meaning I have an ASin() function and floats.
Re: [Maths] Get the rotation values to the sun
Great then! I made some diagrams since explaining this only on text would be an exercise in migraines.
About what the rotation values mean: from my personal testing, alpha and beta are a pretty much universal coordinate system in the game. Alpha goes from 0 to 65536 (16 bits) counter-clockwise from the ecliptic "north", which is +X. Beta then pitches upwards from the ecliptic, that is, if you're perfectly level, increasing beta will point you closer to +Y (until you pass it, then it goes on towards -Y and back again). For example, if you set your ship's alpha to 32768 and beta to 16384, no matter what your original bearing was, you'll end up facing straight "up" with your ship's underside facing south (-X). Also, from my testing, you can also use negative alpha and beta values to rotate in the opposite direction, and the engine doesn't seem to have any issues with that.
And then the relation of gamma to the other two is weird; for example, setting alpha to 0, beta to 16384, and gamma to 32768, would leave you pointing exactly the same as my previous example. But luckily we can ignore gamma for this task.
First, to get the alpha and beta angles:
Essentially, by subtracting each of the sun's X2;Y2;Z2 values from the station's X1;Y1;Z1 values, you get the "distance" on each of the three axes. The absolute value of X2-X1 (A) and the absolute value of Z2-Z1 (B) are the lengths of the shorter sides of an ecliptic-aligned right triangle; from that you can calculate the hypotenuse (H), and with that plus the absolute value of Y2-Y1 (C) you have the shorter sides of the other right triangle that is perpendicular to the ecliptic. Then you can calculate the hypotenuse of this second vertical triangle (V), and then you have all you need to calculate angles D and E by using arcsin or other inverse trigonometric functions (I went with arcsin because you need to calculate the first hypotenuse anyway).
This is not enough though, because you need to convert these angles to a value you can use in-game, and then apply some corrections. Let's call the actually usable values Dx for D and Ex for E.
If the tool you'll be using to calculate ASin returns a result in degrees, then Dx = D * 65536 / 360 (same for E to Ex).
If the result is given in radians, then Dx = D * 65536 / 1.5707963 (same for E to Ex).
Then you'll need to correct for the issue that this process is dumb to the fact that the sun can be in either of four "quadrants" relative to your station's "north":
And finally, correct for the same issue relative to the ecliptic plane for the beta angle:
Note: after this, you need to subtract 16384 from beta because SPPs plates face "up" relative to their actual "forward".
While I can't test all of this myself, I made a tiny script that lets me copy my ship's facing onto a targeted SPP with said correction, and after pointing my ship towards the sun, it works as expected leaving the power plant's "plates" facing the sun:
On the technical side, by setting alpha and beta you don't even need to worry about the initial rotation of your SPP. I referenced some info from this post on the MSCI reference subforum. Given that you set the angle in a single command, since the rotation is applied alpha then beta then gamma, there should be no further issues or corrections needed.
Do let me know if any part of this was confusing, if you have any further doubts, or if you see I made a mistake somewhere. Otherwise, best of lucks with your implementation!
About what the rotation values mean: from my personal testing, alpha and beta are a pretty much universal coordinate system in the game. Alpha goes from 0 to 65536 (16 bits) counter-clockwise from the ecliptic "north", which is +X. Beta then pitches upwards from the ecliptic, that is, if you're perfectly level, increasing beta will point you closer to +Y (until you pass it, then it goes on towards -Y and back again). For example, if you set your ship's alpha to 32768 and beta to 16384, no matter what your original bearing was, you'll end up facing straight "up" with your ship's underside facing south (-X). Also, from my testing, you can also use negative alpha and beta values to rotate in the opposite direction, and the engine doesn't seem to have any issues with that.
And then the relation of gamma to the other two is weird; for example, setting alpha to 0, beta to 16384, and gamma to 32768, would leave you pointing exactly the same as my previous example. But luckily we can ignore gamma for this task.
First, to get the alpha and beta angles:
Essentially, by subtracting each of the sun's X2;Y2;Z2 values from the station's X1;Y1;Z1 values, you get the "distance" on each of the three axes. The absolute value of X2-X1 (A) and the absolute value of Z2-Z1 (B) are the lengths of the shorter sides of an ecliptic-aligned right triangle; from that you can calculate the hypotenuse (H), and with that plus the absolute value of Y2-Y1 (C) you have the shorter sides of the other right triangle that is perpendicular to the ecliptic. Then you can calculate the hypotenuse of this second vertical triangle (V), and then you have all you need to calculate angles D and E by using arcsin or other inverse trigonometric functions (I went with arcsin because you need to calculate the first hypotenuse anyway).
This is not enough though, because you need to convert these angles to a value you can use in-game, and then apply some corrections. Let's call the actually usable values Dx for D and Ex for E.
If the tool you'll be using to calculate ASin returns a result in degrees, then Dx = D * 65536 / 360 (same for E to Ex).
If the result is given in radians, then Dx = D * 65536 / 1.5707963 (same for E to Ex).
Then you'll need to correct for the issue that this process is dumb to the fact that the sun can be in either of four "quadrants" relative to your station's "north":
And finally, correct for the same issue relative to the ecliptic plane for the beta angle:
Note: after this, you need to subtract 16384 from beta because SPPs plates face "up" relative to their actual "forward".
While I can't test all of this myself, I made a tiny script that lets me copy my ship's facing onto a targeted SPP with said correction, and after pointing my ship towards the sun, it works as expected leaving the power plant's "plates" facing the sun:
On the technical side, by setting alpha and beta you don't even need to worry about the initial rotation of your SPP. I referenced some info from this post on the MSCI reference subforum. Given that you set the angle in a single command, since the rotation is applied alpha then beta then gamma, there should be no further issues or corrections needed.
Do let me know if any part of this was confusing, if you have any further doubts, or if you see I made a mistake somewhere. Otherwise, best of lucks with your implementation!
Re: [Maths] Get the rotation values to the sun
Wow... First let me thank you for this great answer! Very good explanations.
The whole code if someone is interested (I doubt it ):
(The language here is PureBasic)
Because I cannot get (x, y, z) of SPP outside of the game, I assume it to be (0, 0, 0).
Pow() function arguments are Pow(Number, Exponent).
Result:
Awesome! Thank you again, this will be helpful to other projects as well
The whole code if someone is interested (I doubt it ):
Code: Select all
a = x
b = z
h = Sqr(Pow(a, 2) + Pow(b, 2))
d = ASin(b / h)
c = y
v = Sqr(Pow(h, 2) + Pow(c, 2))
v = Sqr(Pow(a, 2) + Pow(b, 2) + Pow(c, 2))
e = ASin(c / v)
dx = Degree(d) * 65536 / 360
ex = Degree(e) * 65536 / 360
If x > 0
If z > 0
alpha = 49152 + dx
Else
alpha = 49152 - dx
EndIf
Else
If z > 0
alpha = 16384 - dx
Else
alpha = 16384 + dx
EndIf
EndIf
If y > 0
beta = ex
Else
beta = 65536 - ex
EndIf
beta - 16384
Because I cannot get (x, y, z) of SPP outside of the game, I assume it to be (0, 0, 0).
Pow() function arguments are Pow(Number, Exponent).
Result:
Awesome! Thank you again, this will be helpful to other projects as well
Re: [Maths] Get the rotation values to the sun
I think it took you less time to implement the solution than it took me to make those graphs hahaha! Thinking about it, 0,0,0 is a smart approximation as long as the sun is relatively far, which should be the case for most orthodox sectors, and it also has the side-effect of reducing the number of operations quite a bit, I see.
Glad to have been of help
Glad to have been of help
-
- Posts: 1016
- Joined: Wed, 4. Feb 04, 17:37
Re: [Maths] Get the rotation values to the sun
I just wanted to say: you guys are great!
However, one question comes to my mind: Litcube states that all SPPs in LU are facing the sun, so how has he done it? Doing the calculations outside of the game?
However, one question comes to my mind: Litcube states that all SPPs in LU are facing the sun, so how has he done it? Doing the calculations outside of the game?
Re: [Maths] Get the rotation values to the sun
You just need to orient them in game with the galaxy editor, I guess. The challenge here was to generate them; all SPPs are random (in random sectors). LU has a fixed universe, manually designed.
@Mokunen: you're right, you did all the work But the graphs are awesome!
@Mokunen: you're right, you did all the work But the graphs are awesome!
-
- Posts: 1016
- Joined: Wed, 4. Feb 04, 17:37
Re: [Maths] Get the rotation values to the sun
I see, may I ask what do you need this for? An addition to Mayhem?
Re: [Maths] Get the rotation values to the sun
@Joubarbe Oh no I didn't mean to take credit or anything, I just meant that you're really fast haha Besides, it would have never occured to me to use an external program to deal with the thorny math issues arising from the script engine's limitations... and even if I had, I don't have anywhere near enough knowledge to actually do it; someone says "injector" and my mind hears "black computer magic". Your solution was pretty inspired and out-of-the-box, in my opinion.
Re: [Maths] Get the rotation values to the sun
I wrote a script for it. All SPPs should point exactly toward the sun. In all sectors.Joubarbe wrote: ↑Tue, 21. May 19, 21:17You just need to orient them in game with the galaxy editor, I guess. The challenge here was to generate them; all SPPs are random (in random sectors). LU has a fixed universe, manually designed.
@Mokunen: you're right, you did all the work But the graphs are awesome!
Re: [Maths] Get the rotation values to the sun
"Elementary Watson"
Code: Select all
$suns = $sector-> get suns array
if $suns
$sun = $suns [0]
$SPP-> turn to face object: $sun
$SPP.A = $SPP-> get rot alpha
$SPP.B = $SPP-> get rot beta
$SPP.B = $SPP.B - 16384
$SPP.G = 0
$SPP-> ignore collisions: [TRUE]
$SPP-> set rotation: alpha=$SPP.A beta=$SPP.B gamma=$SPP.G
$SPP-> ignore collisions: [FALSE]
end
Re: [Maths] Get the rotation values to the sun
Code: Select all
= get suns array
And Litcube, when you say you wrote a script for that, I suppose you used this same command. I don't seem to find this script, but I think there may be a problem somewhere with that command (or my universe.xml).
Re: [Maths] Get the rotation values to the sun
The command does not work after X-Studio.
In the game editor, this command works well.
To work correctly in X-Studio, this command needs to be fixed in Custom.Syntax.txt.
wrong:
Code: Select all
1886
NONE
$0 $1 get suns array
RetVar
Var/Sector
-------------- END DEFINITION ---------------
Code: Select all
1886
NONE
$1 $0 get suns array
RefObj
RetVar/If
-------------- END DEFINITION ---------------