summaryrefslogtreecommitdiff
path: root/frontend/_shared/math
diff options
context:
space:
mode:
authorMatthew Kosarek <matthew.kosarek@vention.cc>2021-03-02 20:03:02 -0500
committerMatthew Kosarek <matthew.kosarek@vention.cc>2021-03-02 20:03:02 -0500
commita315509189073cdaf4172bcfd4e4a54e5c8c87eb (patch)
tree2fc57ae4c86d430a3478e012ae10bacee0b7ba41 /frontend/_shared/math
parentd1b528b01796601c2bfea7b1a9813e5907e1c728 (diff)
Updated stylings and very nearly have square collisions complete and proper
Diffstat (limited to 'frontend/_shared/math')
-rw-r--r--frontend/_shared/math/line2.js10
-rw-r--r--frontend/_shared/math/mat4.js20
-rw-r--r--frontend/_shared/math/rectangle.js14
-rw-r--r--frontend/_shared/math/vec2.js8
4 files changed, 44 insertions, 8 deletions
diff --git a/frontend/_shared/math/line2.js b/frontend/_shared/math/line2.js
index a9e72be..8b30c1c 100644
--- a/frontend/_shared/math/line2.js
+++ b/frontend/_shared/math/line2.js
@@ -32,7 +32,8 @@ function line2(pStart, pEnd, pGl, pColor, pMass) {
length: length2(lDiffVector),
mass: pMass,
direction: normalize2(lDiffVector),
- velocity: vec2(0, 0)
+ velocity: vec2(0, 0),
+ position: vec2()
};
}
@@ -40,6 +41,13 @@ function getLine2MomentOfInertia(pLine) {
return (1.0 / 12.0) * pLine.mass * Math.pow(pLine.length, 2);
}
+function getLine2MidPoint(pLine) {
+ return {
+ x: (pLine.end.x + pLine.start.x) / 2.0,
+ y: (pLine.end.y + pLine.start.y) / 2.0
+ }
+}
+
function renderLine2(pGl, pProgramInfo, pLine) {
pGl.uniformMatrix4fv(pProgramInfo.uniformLocations.model, false, mat4()); // Model on a line is always default matrix
pGl.bindBuffer(pGl.ARRAY_BUFFER, pLine.buffer);
diff --git a/frontend/_shared/math/mat4.js b/frontend/_shared/math/mat4.js
index 6ab29e2..d31a20e 100644
--- a/frontend/_shared/math/mat4.js
+++ b/frontend/_shared/math/mat4.js
@@ -40,4 +40,24 @@ function scaleMatrix(m, x, y, z) {
m[5] = m[5] * y;
m[10] = m[10] * z;
return m;
+}
+
+function multMat4ByVec2(matrix, vec) {
+ var lInnerVec = {...vec};
+ lInnerVec.z = 0.0;
+ lInnerVec.w = 1.0;
+
+ return {
+ x: lInnerVec.x * matrix[0] + lInnerVec.y * matrix[4] + lInnerVec.z * matrix[8] + lInnerVec.w * matrix[12],
+ y: lInnerVec.x * matrix[1] + lInnerVec.y * matrix[5] + lInnerVec.z * matrix[9] + lInnerVec.w * matrix[13]
+ };
+}
+
+function multMat4ByVec4(matrix, vec) {
+ return {
+ x: vec.x * matrix[0] + vec.y * matrix[4] + vec.z * matrix[8] + vec.w * matrix[12],
+ y: vec.x * matrix[1] + vec.y * matrix[5] + vec.z * matrix[9] + vec.w * matrix[13],
+ z: vec.x * matrix[2] + vec.y * matrix[6] + vec.z * matrix[10] + vec.w * matrix[14],
+ w: vec.x * matrix[3] + vec.y * matrix[7] + vec.z * matrix[11] + vec.w * matrix[15]
+ };
} \ No newline at end of file
diff --git a/frontend/_shared/math/rectangle.js b/frontend/_shared/math/rectangle.js
index c24fa12..012c460 100644
--- a/frontend/_shared/math/rectangle.js
+++ b/frontend/_shared/math/rectangle.js
@@ -7,19 +7,19 @@ function rectangle(pGl, pData) {
pGl.bindBuffer(pGl.ARRAY_BUFFER, lBuffer);
var lBufferedData = [
- 0, 0, lColor.x, lColor.y, lColor.z, lColor.w,
- 0, pData.height, lColor.x, lColor.y, lColor.z, lColor.w,
- pData.width, pData.height, lColor.x, lColor.y, lColor.z, lColor.w,
- pData.width, pData.height, lColor.x, lColor.y, lColor.z, lColor.w,
- pData.width, 0, lColor.x, lColor.y, lColor.z, lColor.w,
- 0, 0, lColor.x, lColor.y, lColor.z, lColor.w
+ -pData.width / 2.0, -pData.height / 2.0, lColor.x, lColor.y, lColor.z, lColor.w,
+ -pData.width / 2.0, pData.height / 2.0, lColor.x, lColor.y, lColor.z, lColor.w,
+ pData.width / 2.0, pData.height / 2.0, lColor.x, lColor.y, lColor.z, lColor.w,
+ pData.width / 2.0, pData.height /2.0, lColor.x, lColor.y, lColor.z, lColor.w,
+ pData.width / 2.0, -pData.height / 2.0, lColor.x, lColor.y, lColor.z, lColor.w,
+ -pData.width / 2.0, -pData.height / 2.0, lColor.x, lColor.y, lColor.z, lColor.w
];
pGl.bufferData(pGl.ARRAY_BUFFER, new Float32Array(lBufferedData), pGl.STATIC_DRAW)
pGl.bindBuffer(pGl.ARRAY_BUFFER, undefined);
pData.getMomentOfInertia = function() {
- return Math.pow(pData.width * pData.height, 3.0) / 12.0;
+ return (1.0 / 12.0) * pData.mass * (pData.height * pData.height + pData.width * pData.width);
};
return makeRigidBody2({
diff --git a/frontend/_shared/math/vec2.js b/frontend/_shared/math/vec2.js
index cff38ce..11e71cd 100644
--- a/frontend/_shared/math/vec2.js
+++ b/frontend/_shared/math/vec2.js
@@ -53,4 +53,12 @@ function negate2(v) {
x: -v.x,
y: -v.y
};
+}
+
+// Algorithm plucked from: https://matthew-brett.github.io/teaching/rotation_2d.html
+function rotateAboutOrigin2(v, angle) {
+ return {
+ x: v.x * Math.cos(angle) - v.y * Math.sin(angle),
+ y: v.x * Math.sin(angle) + v.y * Math.cos(angle),
+ };
} \ No newline at end of file