IC Python API:Basic Math

From Reallusion Wiki!
Jump to: navigation, search
Main article: RL Python Samples.

This article will go over some basic math formulae for common numerical operations.

Required Modules

We'll need the standard Reallusion Python API to access the RMath class to perform certain types of calculations.

import RLPy

Lerp

Linearly interpolates between a and b by t.

def lerp(a, b, t):
    return a + (b-a) * RLPy.RMath.Clamp(t, 0, 1)

Examples

print(lerp(25, 100, 0.5))  # 62.5
print(lerp(0, 50, 0.25))  # 12.5

Inverse Lerp

Calculates the linear parameter t that produces the interpolant value within the range [a, b].

def inverse_lerp(a, b, value):
    if a != b:
        return RLPy.RMath.Clamp((value - a) / (b - a), 0, 1)
    return 0

Examples

print(inverse_lerp(0, 100, 50))  # 0.5
print(inverse_lerp(0, 3, 0.345))  # 0.11500000208616257

Repeat

Loops the value t, so that it is never larger than length and never smaller than 0.

def repeat(t, length):
    return RLPy.RMath.Clamp(t - RLPy.RMath.Floor(t / length) * length, 0, length)

Examples

print(repeat(24, 10))  # 4
print(repeat(3, 12))  # 3

Ping Pong

PingPongs the value t, so that it is never larger than length and never smaller than 0.

def pingpong(t, length):
    t = repeat(t, length * 2)
    return length - abs(t-length)

Examples

print(pingpong(-150, 10))  # 10
print(pingpong(4, 1))  # 0
print(pingpong(30, 20))  # 10