Difference between revisions of "IC Python API:Rotation Math"

From Reallusion Wiki!
Jump to: navigation, search
(Created page with "This article will go some recipes for common 3D rotational math. You'll find this helpful if you need to perform spacial calculations. == Matrix3 to Euler Angles == Converts...")
 
m
Line 1: Line 1:
 +
{{TOC}}
 +
{{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}}
 +
 
This article will go some recipes for common 3D rotational math. You'll find this helpful if you need to perform spacial calculations.
 
This article will go some recipes for common 3D rotational math. You'll find this helpful if you need to perform spacial calculations.
  

Revision as of 18:35, 18 February 2020

Main article: RL Python Samples.

This article will go some recipes for common 3D rotational math. You'll find this helpful if you need to perform spacial calculations.

Matrix3 to Euler Angles

Converts matrix rotational data (3x3 matrix) to euler rotational data (radians).

def matrix3_to_eulerAngle(matrix3):
    x = y = z = 0
    a = matrix3.ToEulerAngle(RLPy.EEulerOrder_XYZ, x, y, z)
    return RLPy.RVector3(a[0], a[1], a[2])

Quaternion to Matrix

Converts quaternion rotational data to matrix rotational data (3x3 matrix).

def quaternion_to_matrix(quaterion):
    matrix4 = RLPy.RMatrix4()
    matrix4.MakeIdentity()
    matrix3 = quaterion.ToRotationMatrix()
    matrix4.SetSR(matrix3)
    return matrix4

From to Rotation

Creates a rotation which rotates from one direction to another direction. Usually you use this to rotate a transform so that one of its axes e.g. the y-axis follows a target direction in world-space.

def from_to_rotation(from_vector, to_vector):
    # Points the from axis towards the to vector, returns a Quaternion
    result = RLPy.RQuaternion()
    from_vector.Normalize()
    to_vector.Normalize()
    up_axis = RLPy.RVector3(RLPy.RVector3.UNIT_Z)
    angle = RLPy.RMath_ACos(from_vector.Dot(to_vector))
    if RLPy.RMath.AlmostZero(angle - RLPy.RMath.CONST_PI) or RLPy.RMath.AlmostZero(angle):
        result.FromAxisAngle(up_axis, angle)
    else:
        normal = from_vector.Cross(to_vector)
        normal.Normalize()
        result.FromAxisAngle(normal, angle)
    return result
</syntaxhighligh>