From 026abdb98ad30209df0e88795f25b1f74556585e Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Thu, 25 Feb 2021 20:50:12 -0500 Subject: Updated the sidebar for usability's sake --- frontend/_shared/math/collision.js | 23 ++++++++++++++++++ frontend/_shared/math/line2.js | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 frontend/_shared/math/collision.js create mode 100644 frontend/_shared/math/line2.js (limited to 'frontend/_shared/math') diff --git a/frontend/_shared/math/collision.js b/frontend/_shared/math/collision.js new file mode 100644 index 0000000..74ec5d8 --- /dev/null +++ b/frontend/_shared/math/collision.js @@ -0,0 +1,23 @@ +/// +/// +/// +/// + +/** + * + * @param {circle} pCricle + * @param {line2} pLine + */ +function lineCircleCollision2(pCricle, pLine) { + // We have a triangle formed by circle's position P and the two points of the line, A and B. + var lSlope = (pCricle.position.y - pLine.start.y) / (pCricle.position.x - pLine.start.x), + lAngle = Math.atan(lSlope), + lAngleDiff = lAngle - pLine.angle, + lPositionToStart = subVec2(pCricle.position, pLine.start), + lPosToStartLength = length2(lPositionToStart), + lHeight = lPosToStartLength * Math.sin(lAngleDiff); + + if (Math.abs(lHeight - pCricle.radius) < pCricle.radius) { + console.log('Intersection'); + } +} \ No newline at end of file diff --git a/frontend/_shared/math/line2.js b/frontend/_shared/math/line2.js new file mode 100644 index 0000000..d8ab096 --- /dev/null +++ b/frontend/_shared/math/line2.js @@ -0,0 +1,49 @@ +/// +/// + +/** + * Creates a new line object + * @param {vec2} pStart + * @param {vec2} pEnd + * @param {WebGLRenderingContext} pGl + */ +function line2(pStart, pEnd, pGl, pColor, pThickness) { + const lDiffVector = subVec2(pEnd, pStart); + + const lBuffer = pGl.createBuffer(); + pGl.bindBuffer(pGl.ARRAY_BUFFER, lBuffer); + + var lBufferedData = [ + pStart.x, pStart.y, pColor.x, pColor.y, pColor.z, pColor.w, + pEnd.x, pEnd.y, pColor.x, pColor.y, pColor.z, pColor.w + ]; + + pGl.bufferData(pGl.ARRAY_BUFFER, new Float32Array(lBufferedData), pGl.STATIC_DRAW) + pGl.bindBuffer(pGl.ARRAY_BUFFER, undefined); + + var lSlope = (pEnd.y - pStart.y) / (pEnd.x - pStart.x); + + return { + buffer: lBuffer, + start: pStart, + end: pEnd, + slope: lSlope, + angle: Math.atan(lSlope), + length: length2(lDiffVector), + direction: normalize2(lDiffVector) + }; +} + +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); + { + pGl.enableVertexAttribArray(pProgramInfo.attributeLocations.position); + pGl.vertexAttribPointer(pProgramInfo.attributeLocations.position, 2, pGl.FLOAT, false, BYTES_PER_FLOAT * 6, 0); + + pGl.enableVertexAttribArray(pProgramInfo.attributeLocations.color); + pGl.vertexAttribPointer(pProgramInfo.attributeLocations.color, 4, pGl.FLOAT, false, BYTES_PER_FLOAT * 6, BYTES_PER_FLOAT * 2); + } + + pGl.drawArrays(pGl.LINES, 0, 2); +} \ No newline at end of file -- cgit v1.2.1