Difference between revisions of "IC Python API:3D Look At"

From Reallusion Wiki!
Jump to: navigation, search
m
m (Look At Math)
Line 33: Line 33:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Notice that the forward vector is the view position minus the target position normalized, which simply means that the magnitude of the vector will always equal to 1.
+
The result of this formula is a rotational 3x3 matrix.
 +
 
 +
=== Important Takeaways ===
 +
 
 +
* The forward vector is the view position minus the target position normalized
 +
* Right vector is the cross product of the up vector and the forward vector normalized.
 +
* Up vector is the cross product of the forward vector and the right vector.
 +
* "Normalized" simply means that the magnitude of the vector will always equal to 1 unless it is too small, then a zero vector will be returned.

Revision as of 01:24, 23 April 2019

Main article: RL Python Samples.

This article will discuss one of the pillars of 3D mathematics for solving a very common problem. Specifically, how to face one item in the scene towards another similar to how Look at (O) is implemented for cameras in iClone. Keep in mind that the position of an item is set by its pivot point and will need to be adjusted if it is not in the desired location.

Necessary Modules

The only required module for this code is:

import RLPy

Look At Math

It is a good idea to tuck away complex math algorithms in easy to understand, reusable functions. Better still is to tuck all complex math functions under a unified class like Math3D for example. 3D math is not an easy subject matter but it's not a field you'll need to master in entirety. In fact, many resources exist online which just need some diligence and patience to uncover such as this one: http://www.technologicalutopia.com/sourcecode/xnageometry/matrix.cs.htm

So let's dive into the mathematical function we'll need to pull of a right-handed look at that is compatible with iClone's coordinate system:

def look_at_right_handed(view_position, view_target, view_up_vector):
    # Look at takes two positional vectors and calculates the facing direction
    forward = view_position - view_target
    forward.Normalize()
    right = view_up_vector.Cross(forward)
    right.Normalize()
    up = forward.Cross(right)

    # Retun a right-handed look-at rotational matrix
    return RLPy.RMatrix3(right.x, right.y, right.z,         # X vector
                         up.x, up.y, up.z,                  # Y vector
                         forward.x, forward.y, forward.z)   # Z vector

The result of this formula is a rotational 3x3 matrix.

Important Takeaways

  • The forward vector is the view position minus the target position normalized.
  • Right vector is the cross product of the up vector and the forward vector normalized.
  • Up vector is the cross product of the forward vector and the right vector.
  • "Normalized" simply means that the magnitude of the vector will always equal to 1 unless it is too small, then a zero vector will be returned.