translation matrix to translate by xt/yt/zt
T = 1 0 0 xt 0 1 0 yt 0 0 1 zt 0 0 0 1
rotation matrix into coordinate system given by 3 vectors x=xx/yx/zx, y=xy/yy/zy, z=xz/yz/zz
R = xx xy xz 0 yx yy yz 0 zx zy zz 0 0 0 0 1
scaling matrix to scale by xs/ys/zs
S = xs 0 0 0 0 ys 0 0 0 0 zs 0 0 0 0 1 inv(T) = 1 0 0 -xt 0 1 0 -yt 0 0 1 -zt 0 0 0 1 inv(R) = xx yx zx 0 xy yy zy 0 xz yz zz 0 0 0 0 1 T x = x + xt y = y + yt z = z + zt 1 = 1 R x = xx x + xy y + xz z y = yx x + yy y + yz z z = zx x + zy y + zz z 1 = 1
rotate first then translate
T R = xx xy xz xt yx yy yz yt zx zy zz zt 0 0 0 1 T R x = xx x + xy y + xz z + xt y yx x + yy y + yz z + yt z zx x + zy y + zz z + zt 1 1
translate first then rotate
R T = xx xy xz xx xt + xy yt + xz zt yx yy yz yx xt + yy yt + yz zt zx zy zz zx xt + zy yt + zz zt 0 0 0 1
scale first then translate
T S = 1 0 0 xt xs 0 0 0 xs 0 0 xt 0 1 0 yt * 0 ys 0 0 = 0 ys 0 yt 0 0 1 zt 0 0 zs 0 0 0 zs zt 0 0 0 1 0 0 0 1 0 0 0 1 T S x = xs * x + xt y ys * y + yt z zs * z + zt 1 1
translate first then scale
S T = xs 0 0 0 1 0 0 xt xs 0 0 xs*xt 0 ys 0 0 * 0 1 0 yt = 0 ys 0 ys*yt 0 0 zs 0 0 0 1 zt 0 0 zs zs*zt 0 0 0 1 0 0 0 1 0 0 0 1 S T x = xs * x + xs * xt y ys * y + ys * yt z zs * z + zs * zt 1 1
translate first then arbitrary affine matrix
A T = A0 A1 A2 A3 1 0 0 xt A0 A1 A2 A0*xt+A1*yt+A2*zt+A3 A4 A5 A6 A7 * 0 1 0 yt = A4 A5 A6 A4*xt+A5*yt+A7*zt+A7 A8 A9 Aa Ab 0 0 1 zt A8 A9 Aa A8*xt+A9*yt+Aa*zt+Ab 0 0 0 1 0 0 0 1 0 0 0 1
scale first then arbitrary affine matrix
A S = A0 A1 A2 A3 xs 0 0 0 A0*xs A1*ys A2*zs A3 A4 A5 A6 A7 * 0 ys 0 0 = A4*xs A5*ys A6*zs A7 A8 A9 Aa Ab 0 0 zs 0 A8*xs A9*ys Aa*zs Ab 0 0 0 1 0 0 0 1 0 0 0 1
rotate by an angle around vector 0/0/1
Ra = +cos -sin 0 0 +sin +cos 0 0 0 0 1 0 0 0 0 1
same but arbitrary affine matrix afterwards
A Ra = A0*c+A1*s -A0*s+A1*c A2 A3 A4*c+A5*s -A4*s+A5*c A6 A7 A8*c+A9*s -A8*s+A9*c Aa Ab 0 0 0 1
A simple 3D vector, with some extra useful methods for quaternions, transformations and rotation.
A quaternion can be useful to hold rotations, as it is much easier to use for physics code. For example a 3x3 matrix has too much redundancy (9 values instead of 3), and so usually will be hard to deal with or lead to wrong results. A quaternion instead will hold not much more than just the orientation (4 values instead of 3).
Orientation basically is a direction, with an additional rotation. For example, a 3d vector plus an angle would describe it. Since the length of the 3d vector does not matter, just 3 angles also are enough (pitch, yaw, roll). Obviously, we can convert from any form to any other, quaternions just have properties which make them easy to use in physics code.
Parameters: LandFloat x, LandFloat y, LandFloat z
Returns: LandVector
Parameters: LandFloat * a
Parameters: LandVector * v, LandVector w
Parameters: LandVector * v, LandFloat s
Parameters: LandVector v
Parameters: LandVector v, LandFloat s
Parameters: LandVector v, LandVector w
Parameters: LandVector v, LandVector w, LandVector a, LandVector b
Parameters: LandVector v, LandVector w, LandVector a, LandVector b, LandVector c, LandVector d, LandVector e, LandVector f
Parameters: LandVector v, LandVector w, LandFloat t
Returns: LandFloat
The dot product is a number. The number corresponds to the cosine between the two vectors times their lengths. So the angle between the vectors would be: angle = acos(v . w / (|v| * |w|)). If the dot product is 0, the two vectors conversely are orthogonal. The sign can be used to determine which side of a plane a point is on.
The cross product results in a vector orthogonal to both v and w. The length of the resulting vector corresponds to the sine of the angle between the two vectors and their lengths. So the angle between the vectors would be: angle = asin(|v x w| / (|v| * |w|)). If the cross product is the 0 vector, the two input vectors are parallel.
Return the norm of the vector.
Return a normalized version of the vector.
Parameters: LandVector v, LandQuaternion q
Returns: LandQuaternion
Multiply the vector with a quaternion. The result is a quaternion. For example if your vector is a rotation, the resulting quaternion will be a quaternion who rotates whatever it did plus this additional rotation.
Parameters: LandVector v, LandVector p, LandVector r, LandVector u, LandVector b
Return a new vector obtained by transforming this vector by a coordinate system with the given origin and given right/up/back vectors. This is used if the vector is in world coordinates, and you want to transform it to camera coordinates, where p/r/u/b define camera position and orientation.
Parameters: LandVector v, Land4x4Matrix * m
Do the inverse of transform, i.e. you can use it to transform from camera back to world coordinates.
Parameters: LandVector v, LandVector a, double angle
Rotate the vector around axis a by angle in counter clockwise direction. If this vector is a point in world space, then the axis of rotation is defined by the origin and the a vector.
Parameters: LandVector v, LandVector n
Given the normal of a plane, reflect the vector off the plane. If the vector is a point in 3D space, and the plane goes through the origin, the result is a point reflected by the plane.
Parameters: LandFloat w, LandFloat x, LandFloat y, LandFloat z
Parameters: LandFloat * f
Parameters: LandQuaternion * q, LandFloat * f
Parameters: LandQuaternion * q, LandQuaternion p
Parameters: LandQuaternion * q, LandFloat s
Parameters: LandQuaternion qa, LandQuaternion qb
Parameters: LandQuaternion q, LandVector * r, LandVector * u, LandVector * b
Output three orientation vectors for the quaternion. That is, if the quaternion is used as a 3D orientation, return right/up/back vectors representing the same orientation.
Parameters: LandQuaternion q
Returns: Land4x4Matrix
Parameters: Land4x4Matrix a, Land4x4Matrix b
This multiplies two matrices:
result = a b When used with 3D transformations, the result has the same effect as first applying b, then a. For example: v = land_vector(1, 0, 0) a = land_4x4_matrix_scale(10, 1, 1) b = land_4x4_matrix_translate(10, 0, 0) # This means first b then a, so v is first translated to 11, then # scaled to 110. land_vector_matmul(v, land_4x4_matrix_mul(a, b)) # This means v is first scaled to 10, then translated to 20. land_vector_matmul(v, land_4x4_matrix_mul(b, a)) In words, result[row,column] = a[row,...] * b[...,column].
Parameters: LandFloat xy, LandFloat xz, LandFloat yx, LandFloat yz, LandFloat zx, LandFloat zy
Parameters: LandFloat x, LandFloat y, LandFloat z, LandFloat angle
no parameters
Parameters: LandFloat left, LandFloat bottom, LandFloat nearz, LandFloat right, LandFloat top, LandFloat farz
Parameters: LandFloat left, LandFloat top, LandFloat nearz, LandFloat right, LandFloat bottom, LandFloat farz
Orthographic means no projection so this would be just an identity matrix. But as convenience this scales and translates to fit into the left/top/right/bottom rectangle and also scales depth.
The point at (left, top, near) will end up at (-1, -1, -1) and the point at (right, bottom, far) will end up at (1, 1, 1). O = S(2/w, 2/h, 2/d) T(-cx, -cy, -cz) O = 2/w 0 0 2/w*-cx 0 2/h 0 2/h*-cy 0 0 2/d 2/d*-cz 0 0 0 1 O x = 2/w*(x-cx) y 2/h*(y-cy) z 2/d*(z-cz) 1 1 inv(O) = inv(T) inv(S) = w/2 0 0 cx 0 h/2 0 cy 0 0 d/2 cz 0 0 0 1 O inv(O) = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Parameters: LandVector * p, LandVector * r, LandVector * u, LandVector * b
Parameters: Land4x4Matrix * m
Parameters: LandQuaternion * q
Normalize the quaternion. This may be useful to prevent deteriorating the quaternion if it is used for a long time, due to floating point inaccuracies.
Parameters: LandQuaternion qa, LandQuaternion qb, double t
Given two quaternions, interpolate a quaternion in between. If t is 0 this will return qa, if t is 1 it will return qb.
The rotation will be along the shortest path (not necessarily the shorter direction though) and the rotation angle will linearly correspond to t.
Returns: LandBuffer*