matrix

(module)

A module for setting the current matrices used with various graphics functions in Codea

General

matrix.push()

Push the current matrix onto the stack, saving the current transform

matrix.pop()

Pop the matrix from the stack, restoring the previous transform

matrix.reset()

Reset the current matrix to the identity matrix

matrix.model()

Gets the model matrix used to transform from local to world space

matrix.model(matrix)

Sets the model matrix used to transform from local to world space

matrix.view()

Gets the view matrix used to transform from world to view space

matrix.view(matrix)

Sets the view matrix used to transform from world to view space (generally controlled by cameras)

matrix.projection()

Gets the projection matrix used for final projection

matrix.projection(matrix)

Sets the projection matrix, used for final projection (i.e. ortho and perspective projection)

Transform

matrix.transform2d(x, y[, sx = 1, sy = 1, r = 0])

Apply a generic 2D transform to the model matrix

matrix.transform3d(x, y, z, sx, sy, sz, rx, ry, rz)

Apply a generic 3D transform to the model matrix

matrix.translate(x, y[, z])

Translate the current model matrix

matrix.rotate(quat)
matrix.rotate(angle, x, y, z)

Rotate the current model matrix

matrix.scale(x, y[, z])

Scale the current model matrix

Projection

Note

Codea 4.x uses a left-handed, +Z-forward coordinate system for 3D rendering. When matrix.perspective() is active, the camera is at the origin looking toward +Z. Objects must be translated to positive Z to appear in front of the camera. This is the opposite of the -Z-forward convention used in OpenGL and Codea 3.x Craft.

-- Minimal 3D draw loop
matrix.push()
    matrix.perspective(60)          -- 60° FOV, camera at origin, +Z forward
    matrix.translate(0, 0, 4)       -- object 4 units in front
    matrix.rotate(angle, 0, 1, 0)
    myMesh:draw()
matrix.pop()
matrix.ortho()

Set the projection matrix to ortho using the device screen settings (i.e. coordinates range [0, 0] to [WIDTH, HEIGHT])

matrix.ortho(height)

Sets an orthographic projection matrix height units high, using the aspect ratio of the screen to set the width

Parameters:

height – The height of the orthographic projection

matrix.ortho(left, right, bottom, top[, near, far])

Sets an orthographic projection matrix using the supplied edge values

matrix.perspective(fov, aspect, near, far])

Sets a perspective projection matrix