@@ -45,13 +45,14 @@ class RaylibJs {
4545 this . images = [ ] ;
4646 this . quit = false ;
4747
48- this . cameraOffset2D = undefined ;
48+ this . camera2D = undefined ;
4949 }
5050
5151 applyCameraOffset ( x , y ) {
52- if ( this . cameraOffset2D ) {
53- x += this . cameraOffset2D . x ;
54- y += this . cameraOffset2D . y ;
52+ if ( this . camera2D ) {
53+ const [ offsetX , offsetY , targetX , targetY , rotation , zoom ] = this . camera2D ;
54+ x += ( offsetX - targetX ) ;
55+ y += ( offsetY - targetY ) ;
5556 }
5657 return [ x , y ] ;
5758 }
@@ -369,11 +370,13 @@ class RaylibJs {
369370 if ( rotation !== 0 ) throw Error ( "Rotation not yet supported" ) ;
370371 if ( zoom !== 1 ) throw Error ( "Zoom not yet supported" ) ;
371372
372- this . cameraOffset2D = { x : offsetX - targetX , y : offsetY - targetY } ;
373+ this . camera2D = [ offsetX , offsetY , targetX , targetY , rotation , zoom ] ;
373374 }
375+
374376 EndMode2D ( ) {
375- this . cameraOffset2D = undefined ;
377+ this . camera2D = undefined ;
376378 }
379+
377380 DrawCircle ( posX , posY , radius , color_ptr )
378381 {
379382 const buffer = this . wasm . instance . exports . memory . buffer ;
@@ -386,6 +389,7 @@ class RaylibJs {
386389 this . ctx . fill ( ) ;
387390
388391 }
392+
389393 GetWorldToScreen2D ( result_ptr , position_ptr , camera_ptr ) { //COPY PASTE TO BELOW
390394 const buffer = this . wasm . instance . exports . memory . buffer ;
391395 let [ posX , posY ] = new Float32Array ( buffer , position_ptr , 2 ) ;
@@ -584,20 +588,28 @@ function getColorFromMemory(buffer, color_ptr) {
584588function getCameraMatrix2D ( offsetX , offsetY , targetX , targetY , rotation , zoom )
585589{
586590 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
591+ const matRotation = matrixRotate ( 0.0 , 0.0 , 1.0 , rotation * ( Math . PI / 180.0 ) ) ;
592+ const matScale = matrixScale ( zoom , zoom , 1 .0) ;
589593 const matTranslation = matrixTranslate ( offsetX , offsetY , 0.0 ) ;
590594
591595 const matCamera = matrixMultiply ( matrixMultiply ( matOrigin , matrixMultiply ( matScale , matRotation ) ) , matTranslation ) ;
592596 return matCamera ;
593597}
594598
599+ function matrixScale ( x , y , z )
600+ {
601+ return [ x , 0.0 , 0.0 , 0.0 ,
602+ 0.0 , y , 0.0 , 0.0 ,
603+ 0.0 , 0.0 , z , 0.0 ,
604+ 0.0 , 0.0 , 0.0 , 1.0 ] ;
605+ }
606+
595607function matrixTranslate ( x , y , z )
596608{
597- return [ 1.0 , 0.0 , 0.0 , x ,
598- 0.0 , 1.0 , 0.0 , y ,
599- 0.0 , 0.0 , 1.0 , z ,
600- 0.0 , 0.0 , 0.0 , 1.0 ]
609+ return [ 1.0 , 0.0 , 0.0 , 0.0 ,
610+ 0.0 , 1.0 , 0.0 , 0.0 ,
611+ 0.0 , 0.0 , 1.0 , 0.0 ,
612+ x , y , z , 1.0 ]
601613}
602614
603615function matrixMultiply ( left , right ) {
@@ -666,6 +678,48 @@ function matrixInvert(mat) {
666678 return result ;
667679}
668680
681+ function matrixRotate ( x , y , z , angle )
682+ {
683+ const result = [ ] ;
684+
685+ const lengthSquared = x * x + y * y + z * z ;
686+
687+ if ( ( lengthSquared != 1.0 ) && ( lengthSquared != 0.0 ) )
688+ {
689+ const ilength = 1.0 / Math . sqrt ( lengthSquared ) ;
690+ x *= ilength ;
691+ y *= ilength ;
692+ z *= ilength ;
693+ }
694+
695+ const sinres = Math . sin ( angle ) ;
696+ const cosres = Math . cos ( angle ) ;
697+ const t = 1.0 - cosres ;
698+
699+ result [ 0 ] = x * x * t + cosres ;
700+ result [ 1 ] = y * x * t + z * sinres ;
701+ result [ 2 ] = z * x * t - y * sinres ;
702+ result [ 3 ] = 0.0 ;
703+
704+ result [ 4 ] = x * y * t - z * sinres ;
705+ result [ 5 ] = y * y * t + cosres ;
706+ result [ 6 ] = z * y * t + x * sinres ;
707+ result [ 7 ] = 0.0 ;
708+
709+ result [ 8 ] = x * z * t + y * sinres ;
710+ result [ 9 ] = y * z * t - x * sinres ;
711+ result [ 10 ] = z * z * t + cosres ;
712+ result [ 11 ] = 0.0 ;
713+
714+ result [ 12 ] = 0.0 ;
715+ result [ 13 ] = 0.0 ;
716+ result [ 14 ] = 0.0 ;
717+ result [ 15 ] = 1.0 ;
718+
719+ return result ;
720+ }
721+
722+
669723function vector3Transform ( v , mat ) {
670724
671725 const x = v [ 0 ] ;
0 commit comments