Matrix4 values

Is there a proper way to get the Matrix4 values? It seems like normal way should be

xx, xy, xz, xw,
yx, yy, yz, yw,
zx, zy, zz, zw,
tx, ty, tz, tw

But says

vmath.matrix4 only has fields c0, ..., c3 and m00, m01, ..., m10, ..., m33.
stack traceback:
	[C]: in function '__index'
m = vmath.matrix4() --> identity matrix
print(m.m00) --> 1
print(m.m11) --> 1
pprint(m.c0) --> vmath.vector4(1, 0, 0, 0)

So you can either get columns with c0 to c3 or individual values with mRC where R is row and C is column.

4 Likes

And this should, of course, be added to the documentation… :slight_smile:

6 Likes

This look right?

vmath.matrix4(m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33)
vmath.matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)
c0 vmath.vector4(1, 0, 0, 0)
c1 vmath.vector4(0, 1, 0, 0)
c2 vmath.vector4(0, 0, 1, 0)
c3 vmath.vector4(0, 0, 0, 1)

1000
0100
0010
0001

m00, m10, m20, m30
m01, m11, m21, m31
m02, m12, m22, m32
m03, m13, m23, m33

c0(m00, m01, m02, m03)
c1(m10, m11, m12, m13)
c2(m20, m21, m22, m23)
c3(m30, m31, m32, m33)

No, it’s the other way around:

-- print order, you can't construct like this...
vmath.matrix4(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33)

m00, m01, m02, m03
m10, m11, m12, m13
m20, m21, m22, m23
m30, m31, m32, m33

c0(m00, m10, m20, m30)
c1(m01, m11, m21, m31)
c2(m02, m12, m22, m32)
c3(m03, m13, m23, m33)
1 Like

Rounding like this normal?

local eye = vmath.vector3(0, 0, 0)
local look_at = vmath.vector3(25, 25, 0)
local up = vmath.vector3(0, 1, 0)
local view = vmath.matrix4_look_at(eye, look_at, up)
print(view)
print("c0 " .. view.c0)
print("c1 " .. view.c1)
print("c2 " .. view.c2)
print("c3 " .. view.c2)
DEBUG:SCRIPT: vmath.matrix4(0, -0, 0.99999994039536, -0, -0.70710676908493, 0.70710676908493, 0, -0, -0.70710682868958, -0.70710682868958, 0, -0, 0, 0, 0, 1)
DEBUG:SCRIPT: c0 [0.000000, -0.707107, -0.707107, 0.000000]
DEBUG:SCRIPT: c1 [-0.000000, 0.707107, -0.707107, 0.000000]
DEBUG:SCRIPT: c2 [1.000000, 0.000000, 0.000000, 0.000000]
DEBUG:SCRIPT: c3 [1.000000, 0.000000, 0.000000, 0.000000]
local eye = vmath.vector3(10, 10, 0)
local look_at = vmath.vector3(25, 25, 0)
local up = vmath.vector3(0, 1, 0)
local view = vmath.matrix4_look_at(eye, look_at, up)
print(view)
print("c0 " .. view.c0)
print("c1 " .. view.c1)
print("c2 " .. view.c2)
print("c3 " .. view.c2)
DEBUG:SCRIPT: vmath.matrix4(0, -0, 0.99999994039536, -0, -0.70710670948029, 0.70710670948029, 0, -0, -0.70710676908493, -0.70710676908493, 0, 14.142135620117, 0, 0, 0, 1)
DEBUG:SCRIPT: c0 [0.000000, -0.707107, -0.707107, 0.000000]
DEBUG:SCRIPT: c1 [-0.000000, 0.707107, -0.707107, 0.000000]
DEBUG:SCRIPT: c2 [1.000000, 0.000000, 0.000000, 0.000000]
DEBUG:SCRIPT: c3 [1.000000, 0.000000, 0.000000, 0.000000]
1 Like

Oh yes :slight_smile:
Note that your first line is more “honest”, printing more decimals, while the second is probably printed with “%0.6f” which also makes it round off in the print, while the value is something more specific.

EDIT: That’s why you rarely want to use the “==” or “!=” operators on floating point values (unless you know what you’re doing)

1 Like
local eye = vmath.vector3(25, 125, 0)
local look_at = vmath.vector3(122, 1411, 0)
local up = vmath.vector3(0, 1, 0)
local view = vmath.matrix4_look_at(eye, look_at, up)
print(view)
print("c0 " .. view.c0)
print("c1 " .. view.c1)
print("c2 " .. view.c2)
print("c3 " .. view.c3)
print(view.m23)
DEBUG:SCRIPT: vmath.matrix4(0, -0, 1, -0, -0.99716740846634, 0.075214028358459, 0, 15.527432441711, -0.075214028358459, -0.99716740846634, 0, 126.52627563477, 0, 0, 0, 1)
DEBUG:SCRIPT: c0 [0.000000, -0.997167, -0.075214, 0.000000]
DEBUG:SCRIPT: c1 [-0.000000, 0.075214, -0.997167, 0.000000]
DEBUG:SCRIPT: c2 [1.000000, 0.000000, 0.000000, 0.000000]
DEBUG:SCRIPT: c3 [-0.000000, 15.527432, 126.526276, 1.000000]
DEBUG:SCRIPT: 126.52627563477

Edit: I keep making dumb mistakes from being tired.

I’ll be adding some more functions for matrix4 and quaternions soon to DefMath which will really help with future 3d focused devs. Thank you for the help and info. :smile:

1 Like

There are a bunch of fast C++ functions available in our underlying vector math library that we don’t expose. If you have a list of functions you find useful, please send it over and we might be able to make them part of the built in.

3 Likes