Antra

Standardbibliothek

Antra::Raylib::Math

import Antra::Raylib::Math

raylib's math helpers (raymath) for Antra: matrices for model transforms and shader uniforms, and vector math on [x, y] and [x, y, z] arrays.

Cheatsheet: raymath, https://www.raylib.com/cheatsheet/raymath_cheatsheet.html.

A matrix is raylib's Matrix struct as a map %{m: [...]} with its 16 floats in memory order (row by row: m0 m4 m8 m12 m1 ...). Angles are in radians.

import Antra::Raylib::Math without as binds the name Math, the same as Antra::Math - import it under another name when you need both:

import Antra::Raylib::Core
import Antra::Raylib::Models
import Antra::Raylib::Math as RMath
import Antra::Math

val app = Core.window("Matrices", 320, 200).!
val cube = Models.model_from_mesh(app, Models.mesh_cube(app, [1, 1, 1]))
val cam = Core.camera_3d([position: [3, 3, 3]])
Core.run(app, 0) |app, angle, dt| -> {
    val m = RMath.matrix_multiply(RMath.matrix_rotate_y(angle), RMath.matrix_translate([0, 0.5, 0]))
    Core.clear(app, :raywhite)
    Core.with_camera_3d(app, cam) |app| -> Models.draw_model(app, Models.model_transform(cube, m), [0, 0, 0])
    angle + dt * Math.pi()
}
Core.close(app)

Funktionen#

fn matrix_identity()

Returns the identity matrix (raymath: MatrixIdentity).

Example:

RMath.matrix_identity().m[0 : 4]   # => [1, 0, 0, 0]
fn matrix_invert(mat)

The inverse transform: matrix_multiply(m, matrix_invert(m)) is the identity (raymath: MatrixInvert). A matrix without an inverse (all zero scale) gives non-finite numbers, as in raymath.

Example:

val back = RMath.matrix_invert(RMath.matrix_translate([1, 2, 3]))
RMath.v3_transform([1, 2, 3], back)   # => [0, 0, 0]
fn matrix_look_at(eye, target, up)

A view matrix: a camera at eye looking at target, up pointing up (raymath: MatrixLookAt).

Example:

val view = RMath.matrix_look_at([0, 0, 5], [0, 0, 0], [0, 1, 0])
RMath.v3_transform([0, 0, 0], view)   # => [0, 0, -5]
fn matrix_multiply(a, b)

Combines two transforms: first a, then b (raymath: MatrixMultiply(a, b)).

Example:

val m = RMath.matrix_multiply(RMath.matrix_scale(2), RMath.matrix_translate([1, 0, 0]))
[m.m[0], m.m[3]]                   # => [2, 1]
fn matrix_ortho(left, right, bottom, top, near, far)

An orthographic projection of the box left..right, bottom..top, near..far (raymath: MatrixOrtho).

Example:

val proj = RMath.matrix_ortho(-10, 10, -10, 10, 0.1, 100)
fn matrix_perspective(fovy, aspect, near, far)

A perspective projection with a vertical field of view of fovy radians (raymath: MatrixPerspective).

Example:

val proj = RMath.matrix_perspective(Math.pi() / 3, 16 / 9, 0.1, 100)
fn matrix_rotate(axis, angle)

Returns a rotation by angle radians around axis (right-hand rule; raymath: MatrixRotate).

Example:

val m = RMath.matrix_rotate([0, 1, 0], Math.pi() / 2)
RMath.v3_transform([1, 0, 0], m)    # => about [0, 0, -1]
fn matrix_rotate_x(a)

Returns a rotation by a radians around the X axis (raymath: MatrixRotateX).

Example:

val tilt = RMath.matrix_rotate_x(0.5)
fn matrix_rotate_y(a)

Returns a rotation by a radians around the Y axis (raymath: MatrixRotateY).

fn matrix_rotate_z(a)

Returns a rotation by a radians around the Z axis (raymath: MatrixRotateZ).

fn matrix_scale([x, y, z])
fn matrix_scale(s)

Returns a scale by s on every axis, or by [x, y, z] per axis (raymath: MatrixScale).

Example:

RMath.matrix_scale(2).m[0]                # => 2
RMath.matrix_scale([1, 3, 1]).m[5]        # => 3
fn matrix_translate([x, y, z])

Returns a translation by [x, y, z] (raymath: MatrixTranslate).

Example:

val up = RMath.matrix_translate([0, 2, 0])
up.m[7]                            # => 2
fn matrix_transpose(mat)

Swaps rows and columns (raymath: MatrixTranspose).

Example:

RMath.matrix_transpose(RMath.matrix_translate([1, 2, 3])).m[12 : 15]   # => [1, 2, 3]
fn quat_from_axis_angle(axis, angle)

A rotation by angle radians around axis (raymath: QuaternionFromAxisAngle).

Example:

val q = RMath.quat_from_axis_angle([0, 1, 0], Math.pi() / 2)
RMath.v3_rotate_by_quat([1, 0, 0], q)   # => about [0, 0, -1]
fn quat_from_euler(pitch, yaw, roll)

A rotation from Euler angles in radians: pitch around X, yaw around Y, roll around Z (raymath: QuaternionFromEuler).

fn quat_from_matrix(mat)

The rotation part of a matrix as quaternion (raymath: QuaternionFromMatrix).

fn quat_identity()

No rotation: [0, 0, 0, 1] (raymath: QuaternionIdentity).

fn quat_invert([x, y, z, w])

The opposite rotation (raymath: QuaternionInvert).

fn quat_multiply([ax, ay, az, aw], [bx, by, bz, bw])

The Hamilton product a * b: the rotation b, then a (raymath: QuaternionMultiply).

fn quat_normalize([x, y, z, w])

q scaled to length 1 (raymath: QuaternionNormalize).

fn quat_slerp(a, b0, t)

The rotation t (0..1) of the way from a to b along the shortest arc (raymath: QuaternionSlerp).

Example:

val half = RMath.quat_slerp(RMath.quat_identity(), RMath.quat_from_axis_angle([0, 0, 1], 1), 0.5)
fn quat_to_euler([x, y, z, w])

The Euler angles [pitch, yaw, roll] of a rotation, in radians (raymath: QuaternionToEuler).

fn quat_to_matrix([x, y, z, w])

The rotation as a matrix, e.g. for matrix_multiply or Models.model_transform (raymath: QuaternionToMatrix).

fn v2_add([ax, ay], [bx, by])

a + b (raymath: Vector2Add).

Example:

RMath.v2_add([1, 2], [10, 20])   # => [11, 22]
fn v2_angle([ax, ay], [bx, by])

The signed angle from a to b in radians, -pi..pi (raymath: Vector2Angle).

Example:

RMath.v2_angle([1, 0], [0, 1])    # => about 1.5708
RMath.v2_angle([0, 1], [1, 0])    # => about -1.5708
fn v2_distance(a, b)

The distance between two points (raymath: Vector2Distance).

Example:

RMath.v2_distance([1, 1], [4, 5])   # => 5
fn v2_dot([ax, ay], [bx, by])

The dot product (raymath: Vector2DotProduct).

Example:

RMath.v2_dot([1, 2], [3, 4])   # => 11
fn v2_length(v)

The length (raymath: Vector2Length).

Example:

RMath.v2_length([3, 4])   # => 5
fn v2_lerp(a, b, t)

The point a fraction t of the way from a to b (raymath: Vector2Lerp).

Example:

RMath.v2_lerp([0, 0], [10, 20], 0.5)   # => [5, 10]
fn v2_line_angle([fx, fy], [tx, ty])

The angle of the line from from to to against the horizontal, in radians; like raymath's Vector2LineAngle it is negated, so a line going up on screen (y down) has a positive angle.

Example:

RMath.v2_line_angle([0, 0], [10, -10])   # => about 0.7854 (pi / 4)
fn v2_normalize(v)

v scaled to length 1; the zero vector stays as it is (raymath: Vector2Normalize).

Example:

RMath.v2_normalize([0, 5])   # => [0, 1]
fn v2_rotate([x, y], angle)

v rotated by angle radians (raymath: Vector2Rotate); with y pointing down on screen a positive angle turns clockwise.

Example:

RMath.v2_rotate([1, 0], Math.PI / 2)   # => about [0, 1]
fn v2_scale([x, y], s)

v * s (raymath: Vector2Scale).

Example:

RMath.v2_scale([1, 2], 3)   # => [3, 6]
fn v2_subtract([ax, ay], [bx, by])

a - b (raymath: Vector2Subtract).

Example:

RMath.v2_subtract([5, 5], [1, 2])   # => [4, 3]
fn v3_add([ax, ay, az], [bx, by, bz])

a + b (raymath: Vector3Add).

Example:

RMath.v3_add([1, 2, 3], [10, 20, 30])   # => [11, 22, 33]
fn v3_angle(a, b)

The angle between two vectors in radians, 0..pi (raymath: Vector3Angle).

Example:

RMath.v3_angle([1, 0, 0], [0, 1, 0])   # => about 1.5708 (pi / 2)
fn v3_cross([ax, ay, az], [bx, by, bz])

The cross product (raymath: Vector3CrossProduct).

Example:

RMath.v3_cross([1, 0, 0], [0, 1, 0])   # => [0, 0, 1]
fn v3_distance(a, b)

The distance between two points (raymath: Vector3Distance).

Example:

RMath.v3_distance([1, 1, 1], [3, 4, 7])   # => 7
fn v3_dot([ax, ay, az], [bx, by, bz])

The dot product (raymath: Vector3DotProduct).

Example:

RMath.v3_dot([1, 0, 0], [0, 1, 0])   # => 0
fn v3_length(v)

The length (raymath: Vector3Length).

Example:

RMath.v3_length([2, 3, 6])   # => 7
fn v3_lerp(a, b, t)

The point a fraction t of the way from a to b (raymath: Vector3Lerp).

Example:

RMath.v3_lerp([0, 0, 0], [10, 20, 30], 0.5)   # => [5, 10, 15]
fn v3_normalize(v)

v scaled to length 1; the zero vector stays as it is (raymath: Vector3Normalize).

Example:

RMath.v3_normalize([0, 0, 5])   # => [0, 0, 1]
fn v3_rotate(v, axis, angle)

v rotated by angle radians around axis (right-hand rule; raymath: Vector3RotateByAxisAngle).

Example:

# a quarter turn around Y takes the X axis to -Z
RMath.v3_rotate([1, 0, 0], [0, 1, 0], Math.PI / 2)   # => about [0, 0, -1]
fn v3_rotate_by_quat([vx, vy, vz], [x, y, z, w])

v turned by the rotation q (raymath: Vector3RotateByQuaternion).

Example:

RMath.v3_rotate_by_quat([0, 1, 0], RMath.quat_from_axis_angle([1, 0, 0], Math.pi() / 2))   # => about [0, 0, 1]
fn v3_scale([x, y, z], s)

v * s (raymath: Vector3Scale).

Example:

RMath.v3_scale([1, 2, 3], 2)   # => [2, 4, 6]
fn v3_subtract([ax, ay, az], [bx, by, bz])

a - b (raymath: Vector3Subtract).

Example:

RMath.v3_subtract([5, 5, 5], [1, 2, 3])   # => [4, 3, 2]
fn v3_transform([x, y, z], mat)

The point v moved by the transform mat (raymath: Vector3Transform).

Example:

RMath.v3_transform([1, 1, 1], RMath.matrix_translate([0, 2, 0]))   # => [1, 3, 1]

Quelle dieser Seite