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

From Reallusion Wiki!
Jump to: navigation, search
m
m
Line 3: Line 3:
  
 
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.
 +
 +
== Radians to Degrees ==
 +
 +
Converting angle radians to degrees is as simple as multiplying the radians value to '''RLPy.RMath.CONST_RAD_TO_DEG'''.  However constant values in Python can not be protected.  If you are frequently accessing '''RLPy.RMath.CONST_RAD_TO_DEG''' then there is a real danger that the value will be over-written by accident.  You can use the following function instead to convert your
 +
 +
 +
== Degrees to Radians ==
 +
 +
 +
  
 
== Matrix3 to Euler Angles ==
 
== Matrix3 to Euler Angles ==

Revision as of 19:42, 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.

Radians to Degrees

Converting angle radians to degrees is as simple as multiplying the radians value to RLPy.RMath.CONST_RAD_TO_DEG. However constant values in Python can not be protected. If you are frequently accessing RLPy.RMath.CONST_RAD_TO_DEG then there is a real danger that the value will be over-written by accident. You can use the following function instead to convert your


Degrees to Radians

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>