Difference between revisions of "IC Python API:Rotation Math"
Chuck (RL) (Talk | contribs) m (→Radians to Degrees) |
Chuck (RL) (Talk | contribs) m (→Radians to Degrees) |
||
Line 17: | Line 17: | ||
print(math.degrees(0.5)) # 28.64788975654116 | print(math.degrees(0.5)) # 28.64788975654116 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
=== Custom Function === | === Custom Function === |
Revision as of 01:38, 6 March 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. This will cause all subsequent calculations dealing with this value to become increasingly unstable. You can use the following conversion methods instead:
Pythonic Way
This is the preferred way because the math module is always available; However, you'll still have to import it.
import math
print(math.degrees(0.5)) # 28.64788975654116
Custom Function
def radians_to_degrees(radians):
return radians * 57.295779513082320876798154814105
Degrees to Radians
Converting angle degrees to radians is as simple as multiplying the degrees value to RLPy.RMath.CONST_DEG_TO_RAD. However constant values in Python can not be protected. If you are frequently accessing RLPy.RMath.CONST_DEG_TO_RAD then there is a real danger that the value will be over-written by accident. This will cause all subsequent calculations dealing with this value to become increasingly unstable. You can use the following conversion function instead:
def degrees_to_radians(degrees):
return degrees * 0.017453292519943295769236907684886
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