Skip to content

Commit 4a4031f

Browse files
committed
extract getCameraMatrix2D
1 parent e606fe2 commit 4a4031f

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

raylib.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,7 @@ class RaylibJs {
391391
let [posX, posY] = new Float32Array(buffer, position_ptr, 2);
392392
const [offsetX, offsetY, targetX, targetY, rotation, zoom] = new Float32Array(buffer, camera_ptr, 6);
393393

394-
const matOrigin = matrixTranslate(-targetX, -targetY, 0.0);
395-
const matRotation = matrixTranslate(0.0, 0.0, 0.0); //TODO implement this, currently using identity matrix
396-
const matScale = matrixTranslate(0.0, 0.0, 0.0); //TODO implement this, currently using identity matrix
397-
const matTranslation = matrixTranslate(offsetX, offsetY, 0.0);
398-
399-
const matCamera = matrixMultiply(matrixMultiply(matOrigin, matrixMultiply(matScale, matRotation)), matTranslation);
394+
const matCamera = getCameraMatrix2D(offsetX, offsetY, targetX, targetY, rotation, zoom);
400395

401396
[posX, posY] = vector3Transform([posX, posY, 0.0], matCamera);
402397

@@ -408,12 +403,7 @@ class RaylibJs {
408403
let [posX, posY] = new Float32Array(buffer, position_ptr, 2);
409404
const [offsetX, offsetY, targetX, targetY, rotation, zoom] = new Float32Array(buffer, camera_ptr, 6);
410405

411-
const matOrigin = matrixTranslate(-targetX, -targetY, 0.0);
412-
const matRotation = matrixTranslate(0.0, 0.0, 0.0); //TODO implement this, currently using identity matrix
413-
const matScale = matrixTranslate(0.0, 0.0, 0.0); //TODO implement this, currently using identity matrix
414-
const matTranslation = matrixTranslate(offsetX, offsetY, 0.0);
415-
416-
const matCamera = matrixMultiply(matrixMultiply(matOrigin, matrixMultiply(matScale, matRotation)), matTranslation);
406+
const matCamera = getCameraMatrix2D(offsetX, offsetY, targetX, targetY, rotation, zoom);
417407
const invertedCamera = matrixInvert(matCamera);
418408

419409
[posX, posY] = vector3Transform([posX, posY, 0.0], invertedCamera);
@@ -590,14 +580,26 @@ function getColorFromMemory(buffer, color_ptr) {
590580
}
591581

592582
//matrix functions implementation taken from raylib sourcecode
583+
584+
function getCameraMatrix2D(offsetX, offsetY, targetX, targetY, rotation, zoom)
585+
{
586+
const matOrigin = matrixTranslate(-targetX, -targetY, 0.0);
587+
const matRotation = matrixTranslate(0.0, 0.0, 0.0); //TODO implement this, currently using identity matrix
588+
const matScale = matrixTranslate(0.0, 0.0, 0.0); //TODO implement this, currently using identity matrix
589+
const matTranslation = matrixTranslate(offsetX, offsetY, 0.0);
590+
591+
const matCamera = matrixMultiply(matrixMultiply(matOrigin, matrixMultiply(matScale, matRotation)), matTranslation);
592+
return matCamera;
593+
}
594+
593595
function matrixTranslate(x, y, z)
594596
{
595597
return [1.0, 0.0, 0.0, x,
596598
0.0, 1.0, 0.0, y,
597599
0.0, 0.0, 1.0, z,
598-
0.0, 0.0, 0.0, 1.0
599-
]
600+
0.0, 0.0, 0.0, 1.0]
600601
}
602+
601603
function matrixMultiply(left, right) {
602604
const mat = [];
603605
mat[0] = left[0]*right[0] + left[1]*right[4] + left[2]*right[8] + left[3]*right[12];

0 commit comments

Comments
 (0)