@@ -45,13 +45,14 @@ class RaylibJs {
45
45
this . images = [ ] ;
46
46
this . quit = false ;
47
47
48
- this . cameraOffset2D = undefined ;
48
+ this . camera2D = undefined ;
49
49
}
50
50
51
51
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 ) ;
55
56
}
56
57
return [ x , y ] ;
57
58
}
@@ -369,11 +370,13 @@ class RaylibJs {
369
370
if ( rotation !== 0 ) throw Error ( "Rotation not yet supported" ) ;
370
371
if ( zoom !== 1 ) throw Error ( "Zoom not yet supported" ) ;
371
372
372
- this . cameraOffset2D = { x : offsetX - targetX , y : offsetY - targetY } ;
373
+ this . camera2D = [ offsetX , offsetY , targetX , targetY , rotation , zoom ] ;
373
374
}
375
+
374
376
EndMode2D ( ) {
375
- this . cameraOffset2D = undefined ;
377
+ this . camera2D = undefined ;
376
378
}
379
+
377
380
DrawCircle ( posX , posY , radius , color_ptr )
378
381
{
379
382
const buffer = this . wasm . instance . exports . memory . buffer ;
@@ -386,6 +389,7 @@ class RaylibJs {
386
389
this . ctx . fill ( ) ;
387
390
388
391
}
392
+
389
393
GetWorldToScreen2D ( result_ptr , position_ptr , camera_ptr ) { //COPY PASTE TO BELOW
390
394
const buffer = this . wasm . instance . exports . memory . buffer ;
391
395
let [ posX , posY ] = new Float32Array ( buffer , position_ptr , 2 ) ;
@@ -584,20 +588,28 @@ function getColorFromMemory(buffer, color_ptr) {
584
588
function getCameraMatrix2D ( offsetX , offsetY , targetX , targetY , rotation , zoom )
585
589
{
586
590
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) ;
589
593
const matTranslation = matrixTranslate ( offsetX , offsetY , 0.0 ) ;
590
594
591
595
const matCamera = matrixMultiply ( matrixMultiply ( matOrigin , matrixMultiply ( matScale , matRotation ) ) , matTranslation ) ;
592
596
return matCamera ;
593
597
}
594
598
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
+
595
607
function matrixTranslate ( x , y , z )
596
608
{
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 ]
601
613
}
602
614
603
615
function matrixMultiply ( left , right ) {
@@ -666,6 +678,48 @@ function matrixInvert(mat) {
666
678
return result ;
667
679
}
668
680
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
+
669
723
function vector3Transform ( v , mat ) {
670
724
671
725
const x = v [ 0 ] ;
0 commit comments