[SCR] [LIB] Maths scripts, Mult and 3D Trig

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

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

Post Reply
User avatar
Posts: 2011
Joined: Wed, 29. Jun 05, 01:45

[SCR] [LIB] Maths scripts, Mult and 3D Trig

Post by s9ilent » Sun, 11. Jan 09, 01:01

Some basic scripts.
Remakes of Cycrow's fixed point multiplication scripts, and my 3D trigonometry scripts.

Cycrow's multi. scripts are designed to keep 1000 point accuracy for the 2nd multiplier onwards.
My trig scripts project a point to the front/top/side of an object, depending on which way its facing.

29/01/2010 8:30 +10 GMT
Download Zip file

To just look at the individual scripts click them below
Integer Maths for multiplying 2, 3 and 4 digits respectively
http://members.optuszoo.com.au/whyistha ... multi2.xml
http://members.optuszoo.com.au/whyistha ... multi3.xml
http://members.optuszoo.com.au/whyistha ... multi4.xml

Returns the up left and in front of THIS (Also available in fwd, up right versions). Project versions also available.
http://members.optuszoo.com.au/whyistha ... offset.xml
Same as above but for individual directions
http://members.optuszoo.com.au/whyistha ... offset.xml
http://members.optuszoo.com.au/whyistha ... offset.xml
http://members.optuszoo.com.au/whyistha ... offset.xml

How the scripts work

Project Scripts: These take the given inputs, and return a position x y z units infront/to the left/ontop of [THIS].
This script has rather obvious uses, get a position infront of my ship, to the right etc. This script can also be used to check if a target is infront/ to the side/ontop of your ship by doing the following:
Get $dist from me to target, project fwd with $dist, $dist2 get distance from target to projection, $ratio = 100 * dist2 / $dist, if ratio > 30, is not in front

Offset Scripts: These scripts return the offsets of the infront/to the right/ontop of [THIS]. i.e. Offset + This.position = Projection.
This script is useful if you want to apply movements to large fleets of ships. (e.g. get fwd offset x y z, for each ship, shipx = shipx + offset x etc.)

Split script. This a 3x3 matrix (essentially returns a fwd, right and up offset separately). This can then be used to project/offset MULTIPLE directions from the same rotational basis, but by using MUCH less CPU.
E.g. suppose you where projecting 10 subsystems around a single ship at a given point in time.
$dist = 1000
$matrix = [THIS]-> call script '...set' $dist = $dist
Then to project co ordinates you would use
$pos = [THIS] -> call script '...set.project' fwd=.... left=.... up=.... dist=$dist matrix = $matrix
And you would do this for each of the 10 subsystems
Last edited by s9ilent on Fri, 29. Jan 10, 11:31, edited 4 times in total.

User avatar
Posts: 1291
Joined: Tue, 3. Feb 04, 13:11

Post by ThalonMook » Thu, 28. Jan 10, 17:45


at first thx for this libs. I searched for this funktions to use them in one of my projects.

I will use it to get a point in front of a gate and also to get a point right/left and up/down from a ship.

Now I have one problem. The 'lib.maths.trig.3d.offset' will not work.
I changed the script a bit:

Code: Select all

Script lib.maths.trig.fwd.offset 
Version: 0 
for Script Engine Version: 42 

Project Fwd co-ords 
1: up , Var/Number , 'Up /-down dist' 
2: right , Var/Number , 'right /-left dist' 
3: forward , Var/Number , 'Forward /-back distance' 
4: objekt , Var/Gate , 'Objekt' 

Source Text 

002   $a = objekt -> get rot alpha 
003   $b = objekt -> get rot beta 
004   $g = objekt -> get rot gamma 
006   $sina =  = fixed sin $a 
007   $sinb =  = fixed sin $b 
008   $cosa =  = fixed cos $a 
009   $cosb =  = fixed cos $b 
011   $sina = $sina * 1000 / 65536 
012   $sinb = $sinb * 1000 / 65536 
013   $cosa = $cosa * 1000 / 65536 
014   $cosb = $cosb * 1000 / 65536 
017   * Forward projection 
018 @ $n = [THIS] -> call script 'lib.maths.fixed.multi3' :  first=$forward  second=$cosb  third=$sina 
019   $x = - $n 
020 @ $n = [THIS] -> call script 'lib.maths.fixed.multi2' :  first=$forward  second=$sinb 
021   $y = $n 
022 @ $n = [THIS] -> call script 'lib.maths.fixed.multi3' :  first=$forward  second=$cosa  third=$cosb 
023   $z = $n 
027   $ret =  array alloc: size=3 
028   $ret[0] = $x 
029   $ret[1] = $y 
030   $ret[2] = $z 
031   return $ret 
Here I show the forward script but I do the same with the 3d.

I call it so:

Code: Select all

@ |$fw.distance = THIS -> call script 'lib.maths.trig.3d.offset' :  Up /-down dist=0  right /-left dist=0 Forward /-back distance=$dist  Objekt=target.gate 
But it will not work.
I also use a ship as argument for 'objekt'

Also I found something when I compare the 3d script an the right script.

The calculation of n is differend in row 054 one time its '$y = $y+$n' and in the right script row 009 '$y = $n'. What is the right one ?

I hope you can help me

Cu Thalon

User avatar
Posts: 2011
Joined: Wed, 29. Jun 05, 01:45

Post by s9ilent » Thu, 28. Jan 10, 21:37

In the 3d offset, it should be
y = y + n
(Also by default, my scripts use the [THIS] as the object to be turned, instead of a 4th argument that you've added in, e.g. so I call them as $ship -> call script xxxx, instead of ????-> call script x y z $ship)

The reason for it is that rotations are essentially applied onto each other. (And the order is very important). My formulas are essentially a linear representation of the matrix multiplication of the 3 rotation matricies.

P.S the offset script doesn't not return the position infront of the gate, but returns the offset. i.e. to get the actual position in front of the gate you will need gatex + offsetx, etc.
I think your actually after the projection scripts (these add the actual position, THEN return the number)

User avatar
Posts: 1291
Joined: Tue, 3. Feb 04, 13:11

Post by ThalonMook » Fri, 29. Jan 10, 00:05

Oh thx for the fast response.

So I have misunderstood the function of your scripts :-)

But where I can get the 'lib.maths.trig.3d.project' ?

I need this for a script or mod that I'm working on.

For the position in front I have a funktional script now. But It will not work for the right/left and up/down position.

I tested to put +16384 to the alpha from the reference object so that the direction is not forward. This will work if the ref looks to the 0,0,0 position of the sector but not if its truned.

What can I use for this ?

Cu Thalon

User avatar
Posts: 2011
Joined: Wed, 29. Jun 05, 01:45

Post by s9ilent » Fri, 29. Jan 10, 09:35

Ok apparently I never finished making all the project scripts before, so I've made some new ones and uploaded them.

I've found the typo in the .right script
It went $x = + $n instead of $x = $n, it should now work.

As for why the .up scripts aren't working... I can't seem to replicate the problem.

I've also included the new .split .split.offset and .split.project
Essentially, if you wanted to do many projections/offsets, to the same ship (which isn't moving between these projections), rather then doing 10x 3d.projections (which is very CPU intensive), it is better to do 1x .split, then use that split result 10x in the .split.offset/.split.project
Split essentially pre calculates a $dist fwd offset, $dist left offset, $dist up offset, and then .split.project/offset just adds the 3 offsets together (ass adding up numbers is far less intensive then doing all the trig functions again)

User avatar
Posts: 1291
Joined: Tue, 3. Feb 04, 13:11

Post by ThalonMook » Fri, 5. Feb 10, 00:27


thx for your help.

Now it works perfekt. All directions will work fine.

CU Thalon

User avatar
Posts: 2011
Joined: Wed, 29. Jun 05, 01:45

Post by s9ilent » Thu, 10. Mar 11, 07:37

Just found a rather... gaping bug in this script..

(I had never used this particular one and it is almost a direct copy/paste of the one it was derived from)

lib.maths.trig.3d.offset.xml does not actually project forwards...

will fix later

Post Reply

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