summaryrefslogtreecommitdiff
path: root/frontend/2d/_collisions/rectangle_line.js
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/2d/_collisions/rectangle_line.js')
-rw-r--r--frontend/2d/_collisions/rectangle_line.js105
1 files changed, 105 insertions, 0 deletions
diff --git a/frontend/2d/_collisions/rectangle_line.js b/frontend/2d/_collisions/rectangle_line.js
new file mode 100644
index 0000000..ac73f71
--- /dev/null
+++ b/frontend/2d/_collisions/rectangle_line.js
@@ -0,0 +1,105 @@
+/// <reference path="../../scripts/jquery-3.5.1.min.js"/>
+/// <reference path="../../_shared/math/vec2.js" />
+/// <reference path="../../_shared/math/mat4.js" />
+/// <reference path="../../_shared/2d/shader.js" />
+/// <reference path="../../_shared/math/rectangle.js" />
+/// <reference path="../../_shared/math/line2.js" />
+/// <reference path="../../_shared/math/collision.js" />
+/// <reference path="../../_shared/2d/program_common.js" />
+
+(function() {
+ var programContext,
+ rectangleObject,
+ lineObjectList,
+ programInfo,
+ exitRequestFunc;
+
+ function main() {
+ programContext = getContext('#rectangle_line_collision');
+
+ if (programContext.gl === null) {
+ console.error('Unable to initialize WebGL. Your browser or machine may not support it.');
+ return;
+ }
+
+ programContext.gl.clearColor(0.1, 0.15, 0.2, 1.0);
+ programContext.gl.clear(programContext.gl.COLOR_BUFFER_BIT);
+ programContext.playButton.on('click', run);
+ }
+
+ function run() {
+ console.log('Running Rectangle-Line Collisions');
+ programContext.load().then(function(pProgramInfo) {
+ programInfo = pProgramInfo;
+
+ rectangleObject = rectangle(programContext.gl, {
+ width: 50,
+ height: 25,
+ color: { x: 0.3, y: 0.5, z: 0.1, w: 0.7 },
+ position: vec2(programContext.width / 2.0 - 100, programContext.height / 2.0 + 100)
+ });
+ rectangleObject.velocity = vec2(0, -50);
+
+ lineObjectList = [];
+
+ lineObjectList.push(line2({ x: 100, y: 100 }, { x: programContext.width - 100, y: 200 }, programContext.gl,
+ { x: 1, y: 0, z: 0, w: 1 }, 2.0));
+
+ lineObjectList.push(line2({ x: 100, y: 200 }, { x: programContext.width - 100, y: 100 }, programContext.gl,
+ { x: 1, y: 1, z: 0, w: 1 }, 2.0));
+
+ lineObjectList.push(line2({ x: 100, y: 0 }, { x: 100, y: programContext.height }, programContext.gl,
+ { x: 0, y: 1, z: 0, w: 1 }, 2.0));
+
+ lineObjectList.push(line2({ x: programContext.width - 100, y: 0 }, { x: programContext.width - 100, y: programContext.height }, programContext.gl,
+ { x: 0, y: 1, z: 0, w: 1 }, 2.0));
+
+
+ exitRequestFunc = requestUpdateLoop(update, cleanup);
+ programContext.stopButton.on('click', reset);
+ });
+ }
+
+ function update(pDeltaTimeSeconds) {
+ pDeltaTimeSeconds = pDeltaTimeSeconds;
+ updateRigidBody2(rectangleObject, pDeltaTimeSeconds);
+ collision(pDeltaTimeSeconds);
+ render();
+ }
+
+ function collision(pDeltaTimeSeconds) {
+
+ }
+
+ function render() {
+ programContext.gl.clearColor(0.1, 0.15, 0.2, 1.0);
+ programContext.gl.clearDepth(1.0);
+ programContext.gl.enable(programContext.gl.DEPTH_TEST);
+ programContext.gl.depthFunc(programContext.gl.LEQUAL);
+ programContext.gl.clear(programContext.gl.COLOR_BUFFER_BIT | programContext.gl.DEPTH_BUFFER_BIT);
+ programContext.gl.useProgram(programInfo.program);
+ programContext.gl.uniformMatrix4fv(programInfo.uniformLocations.projection, false, programContext.perspective);
+
+ programInfo.renderShape(rectangleObject);
+ lineObjectList.forEach(function(lineObject) {
+ renderLine2(programContext.gl, programInfo, lineObject);
+ });
+ }
+
+ function cleanup() {
+ programContext.gl.deleteBuffer(rectangleObject.buffer);
+ lineObjectList.forEach(function(lineObject) {
+ programContext.gl.deleteBuffer(lineObject.buffer);
+ });
+ programContext.gl.deleteProgram(programInfo.program);
+ programContext.gl.clearColor(0.1, 0.15, 0.2, 1.0);
+ programContext.gl.clear(programContext.gl.COLOR_BUFFER_BIT);
+ }
+
+ function reset() {
+ exitRequestFunc();
+ programContext.reset();
+ }
+
+ $(document).ready(main);
+})() \ No newline at end of file