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

From Reallusion Wiki!
Jump to: navigation, search
(Created page with "{{TOC}} {{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}} This article will discuss one of the pillars of 3D mathematics for solving a very common problem. Specif...")
 
m
Line 2: Line 2:
 
{{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}}
 
{{Parent|IC_Python_API:RL_Python_Samples|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.  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.
+
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 ==
 
== Necessary Modules ==

Revision as of 01:18, 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

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.