Skip to content

Instantly share code, notes, and snippets.

@RichardEllicott
Created May 27, 2022 15:36
Show Gist options
  • Save RichardEllicott/a6a6fce6b74da12287ad980f2a6a8ad3 to your computer and use it in GitHub Desktop.
Save RichardEllicott/a6a6fce6b74da12287ad980f2a6a8ad3 to your computer and use it in GitHub Desktop.
func calc_final_transform(_mag_vector,_offset,_width):
"""
this calculates a final transform to rotate a cylinder
this cylinder goes from (0,0,0) to (1,0,0)
so it is basicly 1 unit long along the x axis
WARNING:
the maths in this simple project took a while to figure out and had many changes
unfortunatly so some of the logic of the acos
some of the corrections avoids entering 0 into a divion or atan
basicly created with some trial and error so the notes are incomplete
to use this as a to and from:
_mag_vector = to - from
_offset = from
"""
var mag_vector_copy = _mag_vector # correcting backwards bug (UNSURE STILL
mag_vector_copy.x = -mag_vector_copy.x
mag_vector_copy.z = -mag_vector_copy.z
var trans = Transform() # our transform matrix
# stretch to the right length
trans = trans.scaled(Vector3(mag_vector_copy.length(),_width,_width))
var A = sqrt(pow(mag_vector_copy.x, 2.0) + pow(mag_vector_copy.z,2.0))
#only rotate 2nd time if y is not 0, this elimnates a scene destroying bug!
# rotate the on the z axis as the pitch
if mag_vector_copy.y != 0.0: # avoids a bug? (no pitch required if no y on mag_vector)
var H = mag_vector_copy.length()
var theta = acos(A/H)
if mag_vector_copy.y < 0.0: # WEIRD CORRECTION!!
theta = -theta
trans = trans.rotated(Vector3(0.0,0.0,1.0), theta)
# rotate around the horizon as the direction (needs to be second rotation)
trans = trans.rotated(Vector3.UP,atan2(mag_vector_copy.z,-mag_vector_copy.x))
# this moves the offset (ignoring the scale and rotation), done last
trans.origin += _offset
return trans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment