Author Topic: Rawbots absolute coordinates  (Read 215 times)

z26

  • Experienced Member
  • ***
  • Posts: 122
  • Likes: +1/-0
    • View Profile
Rawbots absolute coordinates
« on: March 17, 2018, 11:19:26 pm »
I know the forum is quite calm recently, Nevertheless I've been working on scripts for rawbots.  This one is a simple python script.

You know how in rawbots map files (you can open them like a gzip archive to look at the text inside) parts and terrain have a "local_to" line?  Said line specify an origin point the part follows.  This allows cool things, like having a bunch of terrain all grouped together.  By moving/rotating a single point, every linked tile is going to move with it.  Those points are called xform.


What if a part is connected to one of those xform, but you want to know the real, absolute position of the part for whatever reason?  I've made the following python script to calculate that information.

Feel free to report any questions or bugs.  The script requires a single module (numpy) and runs with Python3.


Code: [Select]
from numpy import array, radians, degrees, cos, sin, matmul, arctan2, degrees

#(optional) Enter the name (example: hex1@16451481) and the type of the object (example: small_hex_earth)
name = "name1"
type = "energy_bridge"


#Replace the zeroes of the 4 lines below by the proper values, then run the script.
xyz = array ( ((0),(0),(0)) )  #position of child object
origin_x,origin_y,origin_z = 0,0,0 #position of xform
angle_x, angle_y, angle_z = radians(0),radians(0),radians(0), #euler angle of child object
origin_anglex, origin_angley, origin_anglez = radians(0), radians(0), radians(0) #euler angle of xform


def eulertomatrix(x,y,z):
    matrix_x= array((( (1),(0),(0) ),
                        ( (0),(cos(x)),(-sin(x)) ),
                        ( (0),(sin(x)),(cos(x)) )))

    matrix_y= array((( (cos(y)),(0),(sin(y)) ),
                        ( (0),(1),(0) ),
                        ( (-sin(y)),(0),(cos(y)) )))

    matrix_z= array((( (cos(z)),(-sin(z)),(0) ),
                        ( (sin(z)),(cos(z)),(0) ),
                        ( (0),(0),(1) )))

    return matmul(matrix_y,matmul(matrix_x,matrix_z))


def matrixtoeuler(matrix):

    #I stole these from an excel file.  Strangely, the y-x-z formula is the one that works
    #Also, the order of arctan2 parameters is inversed in libreoffice vs numpy.

    gamma = arctan2(matrix[1,0],matrix[1,1])  #matrix[0,1],matrix[1,1]


    beta = arctan2(matrix[2,1]*sin(gamma) -  matrix[2,0]*cos(gamma),  matrix[0,0]*cos(gamma) -  matrix[0,1]*sin(gamma))

   
    alpha = arctan2(-matrix[1,2],  matrix[1,1]*cos(gamma) +  matrix[1,0]*sin(gamma)) #inverted

   
    return array( ((degrees(alpha)),(degrees(beta)),(degrees(gamma))) )
   
 


origin_anglematrix = eulertomatrix(origin_anglex, origin_angley, origin_anglez)
object_anglematrix = eulertomatrix(angle_x, angle_y, angle_z)


finalpos = matmul(origin_anglematrix, xyz )
finalpos[0] = round(finalpos[0] + origin_x, 4 )
finalpos[1] = round(finalpos[1] + origin_y, 4 )
finalpos[2] = round(finalpos[2] + origin_z, 4 )

finalangle = matrixtoeuler(matmul(origin_anglematrix,object_anglematrix))
finalangle[0] = round(finalangle[0],4)
finalangle[1] = round(finalangle[1],4)
finalangle[2] = round(finalangle[2],4)





print(f'''
################################################
{name} . position [{finalpos[0]},{finalpos[1]},{finalpos[2]}]
{name} . rotation [{finalangle[0]},{finalangle[1]},{finalangle[2]}]
{name} . permissions 31
{name} as {type}
energy_bridge_b local_to gaia''' )
« Last Edit: March 17, 2018, 11:51:52 pm by z26 »