raylib: [raymath] Matrix multiplication is possibly incorrect

  • I tested it on latest raylib version from master branch
  • I checked there is no similar issue already reported
  • My code has no errors or misuse of raylib

Issue description

I’m writing my own matrix implementation and checking it agains raylib’s and I found inconsistency when trying to “unproject” vector. I narrowed it down to MatrixMultiply and did some small tests.

D (own implementation):

mat4 view = mat4.lookAt(vec3(0, 2, 5), vec3(1, 20, 1), vec3(0.5f, 1, 0));
mat4 proj = mat4.perspective(45, 9.0f / 16.0f, 2.0f, 125.0f);
mat4 vpnorm = (view * proj);

Outputs

[1.39, -0.39, -3.90, 3.55, 2.86, 0.08, 1.83, -1.78, -0.17, -1.75, -1.09, -0.88, 0.00, 0.00, -1.00, 0.00]

D raylib:

rl.Matrix view = rm.MatrixLookAt(rl.Vector3(0, 2, 5), rl.Vector3(1, 20, 1), rl.Vector3(0.5f, 1, 0));
rl.Matrix proj = rm.MatrixPerspective(45, 9.0f / 16.0f, 2.0f, 125.0f);
rl.Matrix vpnorm = rm.MatrixMultiply(view, proj);

Outputs

Matrix(1.39085, -0.695423, -2.78169, 15.2993, 1.60993, 0.0847334, 0.783783, -4.08838, 0.0559141, 1.00645, -0.223656, -4.95967, 0.054153, 0.974755, -0.216612, -0.866449)

C:

struct Matrix view = MatrixLookAt((Vector3) {0.0f, 2.0f, 5.0f}, (Vector3) {1.0f, 20.0f, 1.0f}, (Vector3) {0.5f, 1.0f, 0.0f});
struct Matrix proj = MatrixPerspective(45.0, 9.0f / 16.0f, 2.0f, 125.0f);
struct Matrix vpnorm = MatrixMultiply(view, proj);

Outputs

1.39 -0.70 -2.78 15.30
1.61 0.08 0.78 -4.09
0.06 1.01 -0.22 -4.96
0.05 0.97 -0.22 -0.87

My implementation output is different from Raylib’s (both in D and C), but I’ve also tested it with online tools (by printing proj and view matrices and taking their values) and output is

	C1	C2	C3	C4
1	1.4036	-0.3938	-3.9039	3.5409
2	2.871	0.0895	1.8268	-1.7908
3	-0.1595	-1.7363	-1.0966	-0.8954
4	0	0	-1	0

Which is my lib’s output with some float inconsistency.

I’m not sure if it’s me doing something incorrectly or is it something else.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 36 (20 by maintainers)

Most upvoted comments

@diwalkerdev no, raylib’s matrix initialised by rows, not by columns

And I’m passing data by rows into it

But due to naming scheme you access it by columns order

@orcmid I can assure you that bindings are correct and raylib.matrix defined same as in raylib Plus earlier in discussion I’ve posted tests where I used C with raylib which still produced wrong multiplication