[Maths] Get the rotation values to the sun

The place to discuss scripting and game modifications for X³: Terran Conflict and X³: Albion Prelude.

Moderators: Moderators for English X Forum, Scripting / Modding Moderators

Post Reply
User avatar
Joubarbe
Posts: 4796
Joined: Tue, 31. Oct 06, 12:11
xr

[Maths] Get the rotation values to the sun

Post by Joubarbe » Sat, 18. May 19, 18:26

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)

Mokunen
Posts: 31
Joined: Fri, 24. May 13, 01:42
x4

Re: [Maths] Get the rotation values to the sun

Post by Mokunen » Sun, 19. May 19, 22:21

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:
  • 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.
If these aren't obstacles to you, I can explain the math required and leave you to adapt the calculations to the X3 script language and coordinate system... because I don't think I'm capable of providing a ready-to-use script for this.

User avatar
Joubarbe
Posts: 4796
Joined: Tue, 31. Oct 06, 12:11
xr

Re: [Maths] Get the rotation values to the sun

Post by Joubarbe » Mon, 20. May 19, 03:43

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.

Mokunen
Posts: 31
Joined: Fri, 24. May 13, 01:42
x4

Re: [Maths] Get the rotation values to the sun

Post by Mokunen » Tue, 21. May 19, 00:09

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:

Image

Image

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":

Image

And finally, correct for the same issue relative to the ecliptic plane for the beta angle:

Image

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:

Image

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!

User avatar
Joubarbe
Posts: 4796
Joined: Tue, 31. Oct 06, 12:11
xr

Re: [Maths] Get the rotation values to the sun

Post by Joubarbe » Tue, 21. May 19, 09:49

Wow... First let me thank you for this great answer! Very good explanations.

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
(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:

Image

Awesome! Thank you again, this will be helpful to other projects as well :)

Mokunen
Posts: 31
Joined: Fri, 24. May 13, 01:42
x4

Re: [Maths] Get the rotation values to the sun

Post by Mokunen » Tue, 21. May 19, 21:02

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 :)

BlackArchon
Posts: 1016
Joined: Wed, 4. Feb 04, 17:37
xr

Re: [Maths] Get the rotation values to the sun

Post by BlackArchon » Tue, 21. May 19, 21:10

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?

User avatar
Joubarbe
Posts: 4796
Joined: Tue, 31. Oct 06, 12:11
xr

Re: [Maths] Get the rotation values to the sun

Post by Joubarbe » Tue, 21. May 19, 21:17

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
Posts: 31
Joined: Fri, 24. May 13, 01:42
x4

Re: [Maths] Get the rotation values to the sun

Post by Mokunen » Wed, 22. May 19, 02:09

@Joubarbe Oh no I didn't mean to take credit or anything, I just meant that you're really fast haha :oops: 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.

User avatar
Litcube
Posts: 4254
Joined: Fri, 20. Oct 06, 19:02
xr

Re: [Maths] Get the rotation values to the sun

Post by Litcube » Fri, 2. Aug 19, 09:52

Joubarbe wrote:
Tue, 21. May 19, 21:17
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!
I wrote a script for it. All SPPs should point exactly toward the sun. In all sectors.

Deniskos
Posts: 153
Joined: Wed, 11. Jun 08, 21:40
x4

Re: [Maths] Get the rotation values to the sun

Post by Deniskos » Sun, 4. Aug 19, 08:19

Joubarbe wrote:
Sat, 18. May 19, 18:26
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.
"Elementary Watson" :D :)

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
:x3:

User avatar
Joubarbe
Posts: 4796
Joined: Tue, 31. Oct 06, 12:11
xr

Re: [Maths] Get the rotation values to the sun

Post by Joubarbe » Tue, 13. Aug 19, 12:15

Code: Select all

= get suns array
doesn't return anything. That's my problem :)
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).

Deniskos
Posts: 153
Joined: Wed, 11. Jun 08, 21:40
x4

Re: [Maths] Get the rotation values to the sun

Post by Deniskos » Mon, 30. Sep 19, 16:46

Joubarbe wrote:
Tue, 13. Aug 19, 12:15
doesn't return anything. That's my problem :)
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 ---------------
right:

Code: Select all

1886
NONE
$1 $0 get suns array
RefObj
RetVar/If
-------------- END DEFINITION ---------------

Post Reply

Return to “X³: Terran Conflict / Albion Prelude - Scripts and Modding”