Skip to content

Commit 00aa97c

Browse files
committed
basic math stuff
1 parent 0c4b6b7 commit 00aa97c

File tree

1 file changed

+129
-7
lines changed

1 file changed

+129
-7
lines changed

raylib.js

Lines changed: 129 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ class RaylibJs {
356356
const text = cstr_by_ptr(buffer, text_ptr);
357357
let [posX, posY] = new Float32Array(buffer, position_ptr, 2);
358358
const tint = getColorFromMemory(buffer, tint_ptr);
359-
[posX, posY] = this.applyCameraOffset(posX,posY);
359+
[posX, posY] = this.applyCameraOffset(posX, posY);
360360
this.ctx.fillStyle = tint;
361361
this.ctx.font = fontSize+"px myfont";
362362
this.ctx.fillText(text, posX, posY + fontSize);
@@ -366,27 +366,61 @@ class RaylibJs {
366366
BeginMode2D(camera_ptr) {
367367
const buffer = this.wasm.instance.exports.memory.buffer;
368368
const [offsetX, offsetY, targetX, targetY, rotation, zoom] = new Float32Array(buffer, camera_ptr, 6);
369-
//console.log('BeginMode2D', offsetX, offsetY, targetX, targetY, rotation, zoom);
370369
if (rotation !== 0) throw Error("Rotation not yet supported");
371370
if (zoom !== 1) throw Error("Zoom not yet supported");
372371

373-
this.cameraOffset2D = { x: offsetX - targetX, y: offsetY - targetY };
372+
this.cameraOffset2D = { x: offsetX - targetX, y: offsetY - targetY };
373+
}
374+
EndMode2D() {
375+
this.cameraOffset2D = undefined;
374376
}
375-
EndMode2D() {this.cameraOffset2D = undefined;}
376377
DrawCircle(posX, posY, radius, color_ptr)
377378
{
378379
const buffer = this.wasm.instance.exports.memory.buffer;
379380
const [r, g, b, a] = new Uint8Array(buffer, color_ptr, 4);
380381
const color = color_hex_unpacked(r, g, b, a);
381-
[posX, posY] = this.applyCameraOffset(posX,posY);
382+
[posX, posY] = this.applyCameraOffset(posX, posY);
382383
this.ctx.beginPath();
383384
this.ctx.arc(posX, posY, radius, 0, 2*Math.PI, false);
384385
this.ctx.fillStyle = color;
385386
this.ctx.fill();
386387

387388
}
388-
GetWorldToScreen2D() {}
389-
GetScreenToWorld2D() {}
389+
GetWorldToScreen2D(result_ptr, position_ptr, camera_ptr) {//COPY PASTE TO BELOW
390+
const buffer = this.wasm.instance.exports.memory.buffer;
391+
let [posX, posY] = new Float32Array(buffer, position_ptr, 2);
392+
const [offsetX, offsetY, targetX, targetY, rotation, zoom] = new Float32Array(buffer, camera_ptr, 6);
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);
400+
401+
[posX, posY] = vector3Transform([posX, posY, 0.0], matCamera);
402+
403+
//return
404+
new Float32Array(buffer, result_ptr, 2).set([posX, posY]);
405+
}
406+
GetScreenToWorld2D(result_ptr, position_ptr, camera_ptr) { //COPY PASTE FROM ABOVE
407+
const buffer = this.wasm.instance.exports.memory.buffer;
408+
let [posX, posY] = new Float32Array(buffer, position_ptr, 2);
409+
const [offsetX, offsetY, targetX, targetY, rotation, zoom] = new Float32Array(buffer, camera_ptr, 6);
410+
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);
417+
const invertedCamera = matrixInvert(matCamera);
418+
419+
[posX, posY] = vector3Transform([posX, posY, 0.0], invertedCamera);
420+
421+
//return
422+
new Float32Array(buffer, result_ptr, 2).set([posX, posY]);
423+
}
390424
//End newly added
391425

392426
raylib_js_set_entry(entry) {
@@ -554,3 +588,91 @@ function getColorFromMemory(buffer, color_ptr) {
554588
const [r, g, b, a] = new Uint8Array(buffer, color_ptr, 4);
555589
return color_hex_unpacked(r, g, b, a);
556590
}
591+
592+
//matrix functions implementation taken from raylib sourcecode
593+
function matrixTranslate(x, y, z)
594+
{
595+
return [1.0, 0.0, 0.0, x,
596+
0.0, 1.0, 0.0, y,
597+
0.0, 0.0, 1.0, z,
598+
0.0, 0.0, 0.0, 1.0
599+
]
600+
}
601+
function matrixMultiply(left, right) {
602+
const mat = [];
603+
mat[0] = left[0]*right[0] + left[1]*right[4] + left[2]*right[8] + left[3]*right[12];
604+
mat[1] = left[0]*right[1] + left[1]*right[5] + left[2]*right[9] + left[3]*right[13];
605+
mat[2] = left[0]*right[2] + left[1]*right[6] + left[2]*right[10] + left[3]*right[14];
606+
mat[3] = left[0]*right[3] + left[1]*right[7] + left[2]*right[11] + left[3]*right[15];
607+
mat[4] = left[4]*right[0] + left[5]*right[4] + left[6]*right[8] + left[7]*right[12];
608+
mat[5] = left[4]*right[1] + left[5]*right[5] + left[6]*right[9] + left[7]*right[13];
609+
mat[6] = left[4]*right[2] + left[5]*right[6] + left[6]*right[10] + left[7]*right[14];
610+
mat[7] = left[4]*right[3] + left[5]*right[7] + left[6]*right[11] + left[7]*right[15];
611+
mat[8] = left[8]*right[0] + left[9]*right[4] + left[10]*right[8] + left[11]*right[12];
612+
mat[9] = left[8]*right[1] + left[9]*right[5] + left[10]*right[9] + left[11]*right[13];
613+
mat[10] = left[8]*right[2] + left[9]*right[6] + left[10]*right[10] + left[11]*right[14];
614+
mat[11] = left[8]*right[3] + left[9]*right[7] + left[10]*right[11] + left[11]*right[15];
615+
mat[12] = left[12]*right[0] + left[13]*right[4] + left[14]*right[8] + left[15]*right[12];
616+
mat[13] = left[12]*right[1] + left[13]*right[5] + left[14]*right[9] + left[15]*right[13];
617+
mat[14] = left[12]*right[2] + left[13]*right[6] + left[14]*right[10] + left[15]*right[14];
618+
mat[15] = left[12]*right[3] + left[13]*right[7] + left[14]*right[11] + left[15]*right[15];
619+
return mat;
620+
}
621+
622+
function matrixInvert(mat) {
623+
const result = [];
624+
625+
// Cache the matrix values (speed optimization)
626+
const a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3];
627+
const a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7];
628+
const a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11];
629+
const a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15];
630+
631+
const b00 = a00*a11 - a01*a10;
632+
const b01 = a00*a12 - a02*a10;
633+
const b02 = a00*a13 - a03*a10;
634+
const b03 = a01*a12 - a02*a11;
635+
const b04 = a01*a13 - a03*a11;
636+
const b05 = a02*a13 - a03*a12;
637+
const b06 = a20*a31 - a21*a30;
638+
const b07 = a20*a32 - a22*a30;
639+
const b08 = a20*a33 - a23*a30;
640+
const b09 = a21*a32 - a22*a31;
641+
const b10 = a21*a33 - a23*a31;
642+
const b11 = a22*a33 - a23*a32;
643+
644+
// Calculate the invert determinant (inlined to avoid double-caching)
645+
const invDet = 1.0/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
646+
647+
result[0] = (a11*b11 - a12*b10 + a13*b09)*invDet;
648+
result[1] = (-a01*b11 + a02*b10 - a03*b09)*invDet;
649+
result[2] = (a31*b05 - a32*b04 + a33*b03)*invDet;
650+
result[3] = (-a21*b05 + a22*b04 - a23*b03)*invDet;
651+
result[4] = (-a10*b11 + a12*b08 - a13*b07)*invDet;
652+
result[5] = (a00*b11 - a02*b08 + a03*b07)*invDet;
653+
result[6] = (-a30*b05 + a32*b02 - a33*b01)*invDet;
654+
result[7] = (a20*b05 - a22*b02 + a23*b01)*invDet;
655+
result[8] = (a10*b10 - a11*b08 + a13*b06)*invDet;
656+
result[9] = (-a00*b10 + a01*b08 - a03*b06)*invDet;
657+
result[10] = (a30*b04 - a31*b02 + a33*b00)*invDet;
658+
result[11] = (-a20*b04 + a21*b02 - a23*b00)*invDet;
659+
result[12] = (-a10*b09 + a11*b07 - a12*b06)*invDet;
660+
result[13] = (a00*b09 - a01*b07 + a02*b06)*invDet;
661+
result[14] = (-a30*b03 + a31*b01 - a32*b00)*invDet;
662+
result[15] = (a20*b03 - a21*b01 + a22*b00)*invDet;
663+
664+
return result;
665+
}
666+
667+
function vector3Transform(v, mat) {
668+
669+
const x = v[0];
670+
const y = v[1];
671+
const z = v[2];
672+
673+
const posX = mat[0]*x + mat[4]*y + mat[8]*z + mat[12];
674+
const posY = mat[1]*x + mat[5]*y + mat[9]*z + mat[13];
675+
const posZ = mat[2]*x + mat[6]*y + mat[10]*z + mat[14];
676+
677+
return [posX, posY, posZ];
678+
}

0 commit comments

Comments
 (0)