CubicBezier

Parametric cubic Bézier curve evaluator for custom easing.

CubicBezier

CubicBezier(a, b, c, d)

Located in ursina/curve.py

Overview

CubicBezier implements a cubic Bézier easing function defined by four control points (0,0), (a,b), (c,d), (1,1). Use it whenever you need a custom easing curve in Entity.animate_* calls.

Properties

NameDescription
.aX coordinate of first control point
.bY coordinate of first control point
.cX coordinate of second control point
.dY coordinate of second control point
.axPrecomputed coefficient for term in X polynomial
.bxPrecomputed coefficient for term in X polynomial
.cxPrecomputed coefficient for term in X polynomial
.ayPrecomputed coefficient for term in Y polynomial
.byPrecomputed coefficient for term in Y polynomial
.cyPrecomputed coefficient for term in Y polynomial

Methods

MethodDescription
sample_curve_x(t)Return the X value of the Bézier at parameter t.
sample_curve_y(t)Return the Y value of the Bézier at parameter t.
sample_curve_derivative_x(t)Return the derivative dX/dt at parameter t.
solve_curve_x(t, epsilon)Numerically solve for parameter t that yields sample_curve_x(t) ≈ x.
calculate(x, epsilon)Given an input progress x (0–1), return the eased output by solving inverse X.

Example

from ursina import *
from ursina.curve import CubicBezier, combine, linear, reverse, in_expo

app = Ursina()
camera.orthographic = True
camera.fov = 16
window.color = color.black

# render the curve as a line
def render_curve(fn, name):
    verts = [Vec3(i/31, fn(i/31), 0) for i in range(32)]
    c = Entity(model=Mesh(vertices=verts, mode='line', thickness=2), color=color.light_gray)
    Text(parent=c, text=name, scale=8, color=color.gray, y=-0.1)
    return c

# simple Bézier: start slow, speed up, slow again
bez = CubicBezier(0, .5, 1, .5)
print('Value at x=0.23 →', bez.calculate(.23))

# combine two curves for a custom effect
custom = combine(linear, reverse(in_expo), 0.25)
render_curve(custom, 'custom_combine')

# Bézier example in animation
e = Entity(model='cube')
e.animate_y(2, duration=1, curve=bez)

EditorCamera()
app.run()