Skip to content

Vectors and Matrices

Narta Xaymar Dirks edited this page Nov 23, 2026 · 1 revision

Vectors and matrices

A vector is a row of numbers of one type, a matrix is a row of vectors. They are values like a Float: copying one gives a second one, there is nothing to free, and they work as a Global, a Local, a parameter, a return type and a field of a Type. They are not arrays: a Dim array can not hold them yet, and an array declared with a size in brackets (Local a[3] or Local a:Float[3]) is still an array that counts up to the highest index, as in the original.

Writing the type

The size is part of the name of the type: the type, then the width, then for a matrix an x and the height.

Written Meaning
FloatVector3 3 Float numbers
IntVector4 4 Int numbers
FloatMatrix4x3 4 columns of 3 numbers (4 wide, 3 high)
DoubleMatrix2x2 a 2 by 2 matrix of Double
  • Every number type has them: Byte, UByte, Short, UShort, Int, UInt, Long, ULong, Half, Float and Double.
  • The sizes are width first, then height. FloatMatrix4x3 is 4 vectors of FloatVector3, and m[column][row] is one number.
  • A size is from 1 to 1024, and a matrix has two sizes at most.
  • The name of the number type can have a size in it: Int8Vector3, Float32Vector2 and UInt16Matrix2x2 are vectors and matrices of those number types.
  • A function of your program with the name of a vector (a function called FloatVector3) is called instead of making one.

Making them

A type name is a function. Give it all numbers, one number for all of them, or vectors and numbers that add up to the size.

Local a:FloatVector3 = FloatVector3(1, 2, 3)
Local b:FloatVector4 = FloatVector4(a, 4)
Local c:FloatVector3 = FloatVector3(7)
Local d:DoubleVector3 = DoubleVector3(a)
Local m:FloatMatrix2x2 = FloatMatrix2x2(FloatVector2(1, 2), FloatVector2(3, 4))
Print b\w
Print c\z
Print d\y
Print m[1][0]
4.0
7.0
2.0
3.0

With no numbers the result is all zeros. A vector that is declared without a value is all zeros too.

Using them

\x \y \z \w (or \r \g \b \a) are the numbers of a vector with 2 to 4 numbers, and can be changed. Several letters read several numbers as a vector: v\xy, v\zyx. Letters can repeat, and the result can not be assigned to. v[2] is the third number, m[1] is the second column of a matrix, and an index outside of the size is a runtime error.

Local position:FloatVector3 = FloatVector3(1, 2, 3)
Local velocity:FloatVector3 = FloatVector3(0.5, 0, -1)
position = position + velocity * 2
position\y = 10
Print position\x
Print position\y
Print position\z
Print position\zx\x
2.0
10.0
1.0
1.0
  • + - * /, Mod, ^ and the bit operators work on every number of two vectors (or two matrices) of the same size, and between a vector or matrix and a number. v * 2 and 2 * v double every number.
  • * between two matrices is the matrix product, and between a matrix and a vector it is the matrix times a column: a matrix of K columns and M rows times one of N columns and K rows has N columns and M rows, and times a vector of K numbers it gives a vector of M numbers. A vector on the left is a row: a vector of K numbers times a matrix of N columns and K rows gives a vector of N numbers. For FloatVector3 times FloatVector3 it is the product of every number, because that is what vectors do.
  • = and <> compare all numbers. <, > and the others, and Not, are errors.
  • The result has the number type with more bits: Int16Vector2 + Int8Vector2 is an Int16Vector2, FloatVector2 + DoubleVector2 is a DoubleVector2. Two vectors of the same number type stay that type, even a small one (Int8Vector2 + Int8Vector2 is an Int8Vector2 and wraps around, HalfVector2 + HalfVector2 is a HalfVector2). An integer vector with a real one (or a real number) follows the rules of the single numbers in Types and numbers. The result goes back to the size of the variable it is assigned to (with the same warnings for losing digits).
  • Assigning a vector to a vector of another number type converts every number. A different size is an error.
  • A vector can not be printed, used as a condition or given where a number is wanted. Convert nothing by accident: write the number you mean (v\x).

Functions

A function can take and return them. It works with a copy of what it was given, so the caller's vector never changes behind its back.

Function Cross:FloatVector3(a:FloatVector3, b:FloatVector3)
	Return FloatVector3(a\y * b\z - a\z * b\y, a\z * b\x - a\x * b\z, a\x * b\y - a\y * b\x)
End Function
Local up:FloatVector3 = Cross(FloatVector3(1, 0, 0), FloatVector3(0, 1, 0))
Print up\z
Print Cross(FloatVector3(0, 1, 0), FloatVector3(0, 0, 1))[0]
1.0
1.0
  • Out and InOut parameters can not be vectors or matrices yet.
  • A big result (above 64 KB) is made in memory that all calls of a recursive function share. Do not hold on to one across a recursive call.
  • A Const can not be a vector.

Limits

  • A vector or matrix of more than 4096 bytes in a function is made on every call, and above 64 KB it is on the heap. The warning large-local-array points at them.
  • The math functions (Dot, Cross, Normalize, Transpose and so on), the commands for banks and streams, and quaternions are not there yet.

Home · Command Index


Getting started

Language

Tools


Language Features
BlitzBasic
Blitz3D
BlitzPlus
Differences

More

Clone this wiki locally