@@ -391,12 +391,7 @@ class RaylibJs {
391
391
let [ posX , posY ] = new Float32Array ( buffer , position_ptr , 2 ) ;
392
392
const [ offsetX , offsetY , targetX , targetY , rotation , zoom ] = new Float32Array ( buffer , camera_ptr , 6 ) ;
393
393
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 ) ;
400
395
401
396
[ posX , posY ] = vector3Transform ( [ posX , posY , 0.0 ] , matCamera ) ;
402
397
@@ -408,12 +403,7 @@ class RaylibJs {
408
403
let [ posX , posY ] = new Float32Array ( buffer , position_ptr , 2 ) ;
409
404
const [ offsetX , offsetY , targetX , targetY , rotation , zoom ] = new Float32Array ( buffer , camera_ptr , 6 ) ;
410
405
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 ) ;
417
407
const invertedCamera = matrixInvert ( matCamera ) ;
418
408
419
409
[ posX , posY ] = vector3Transform ( [ posX , posY , 0.0 ] , invertedCamera ) ;
@@ -590,14 +580,26 @@ function getColorFromMemory(buffer, color_ptr) {
590
580
}
591
581
592
582
//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
+
593
595
function matrixTranslate ( x , y , z )
594
596
{
595
597
return [ 1.0 , 0.0 , 0.0 , x ,
596
598
0.0 , 1.0 , 0.0 , y ,
597
599
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 ]
600
601
}
602
+
601
603
function matrixMultiply ( left , right ) {
602
604
const mat = [ ] ;
603
605
mat [ 0 ] = left [ 0 ] * right [ 0 ] + left [ 1 ] * right [ 4 ] + left [ 2 ] * right [ 8 ] + left [ 3 ] * right [ 12 ] ;
0 commit comments