summaryrefslogtreecommitdiff
path: root/frontend/2d/_collisions/rectangle_line.js
blob: e42cc84e99951ffd8acd4aba02777025a0515dc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/// <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: 50, 
                mass: 1,
                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 }, 10000000.0));

            lineObjectList.push(line2({ x: 100, y: 200 }, { x: programContext.width - 100, y: 100 }, programContext.gl,
                { x: 1, y: 1, z: 0, w: 1 }, 10000000.0));

            lineObjectList.push(line2({ x: 100, y: 0 }, { x: 100, y: programContext.height }, programContext.gl,
                { x: 0, y: 1, z: 0, w: 1 }, 10000000.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 }, 10000000.0));
            

            exitRequestFunc = requestUpdateLoop(update, cleanup);
            programContext.stopButton.on('click', reset);
        });
    }

    function update(pDeltaTimeSeconds) {
        pDeltaTimeSeconds = pDeltaTimeSeconds;
        updateRigidBody2(rectangleObject, pDeltaTimeSeconds);
        collision(pDeltaTimeSeconds);
        render();
    }

    function collision(pDeltaTimeSeconds) {
        for (var lineIdx = 0; lineIdx < lineObjectList.length; lineIdx++) {
            var lLine = lineObjectList[lineIdx];
            
            if (areIntersecting(rectangleObject, lLine, pDeltaTimeSeconds)) {
                break;
            }
        }
    }
    
    function doesPointIntersect(pPoint, pLine) {
        const EPSILON = 0.98;
        return distanceFromPoint2ToLine2(pPoint, pLine) <= EPSILON;
    }

    function getIntersectionInformation(pRectangle, pCollisionPoint, pLine) {
        var lCollisionNormal = pLine.normal,
            lRectCollisionPoint_WorldCoords = multMat4ByVec2(pRectangle.model, pCollisionPoint),
            lRectPosition = pRectangle.position,
            lTrueRectCollisionPoint = normalize2(subVec2(lRectPosition, lRectCollisionPoint_WorldCoords));

        console.log(lTrueRectCollisionPoint);

        return {
            relativeVelocity:           subVec2(pRectangle.velocity, pLine.velocity),
            collisionNormal:            lCollisionNormal,
            rectangleCollisionPoint:    lTrueRectCollisionPoint
        }
    }

    function areIntersecting(pRectangle, pLine, pDeltaTimeSeconds) {
        var lCollisionPoint = undefined,
            lCheckCollision = function(pRectangle) {
                var lLowerLeft      = multMat4ByVec2(pRectangle.model, vec2(-pRectangle.width / 2.0, -pRectangle.height / 2.0)),
                    lUpperLeft      = multMat4ByVec2(pRectangle.model, vec2(-pRectangle.width / 2.0, pRectangle.height / 2.0)),
                    lUpperRight     = multMat4ByVec2(pRectangle.model, vec2(pRectangle.width / 2.0, pRectangle.height / 2.0)),
                    lLowerRight     = multMat4ByVec2(pRectangle.model, vec2(pRectangle.width / 2.0, -pRectangle.height / 2.0));

                if (doesPointIntersect(lLowerLeft, pLine)) {
                    lCollisionPoint = vec2(-pRectangle.width / 2.0, -pRectangle.height / 2.0);
                    return true;
                } else if (doesPointIntersect(lUpperLeft, pLine)) {
                    lCollisionPoint = vec2(-pRectangle.width / 2.0, pRectangle.height / 2.0);
                    return true;
                } else if (doesPointIntersect(lUpperRight, pLine)) {
                    lCollisionPoint = vec2(pRectangle.width / 2.0, pRectangle.height / 2.0);
                    return true;
                } else if (doesPointIntersect(lLowerRight, pLine)) {
                    lCollisionPoint = vec2(pRectangle.width / 2.0, -pRectangle.height / 2.0);
                    return true;
                }

                return false;
            };

        if (!lCheckCollision(pRectangle)) {
            return;
        }

        // Subdivide until we find the exact moment where we intersected
        var lSubdividedDeltaTime = pDeltaTimeSeconds,
            lSubRectangle = undefined;
        do {
            lSubRectangle = JSON.parse(JSON.stringify(pRectangle));
            lSubRectangle.position = {...pRectangle.prevPos};
            lSubRectangle.velocity = {...pRectangle.prevVelocity};
            lSubRectangle.getMomentOfInertia = pRectangle.getMomentOfInertia;
            lSubdividedDeltaTime = lSubdividedDeltaTime / 2.0;
            updateRigidBody2(lSubRectangle, lSubdividedDeltaTime);
            if (lSubdividedDeltaTime === 0) {
                console.error('This should NOT be happening');
                break;
            }
        } while (lCheckCollision(lSubRectangle)); 
        
        var lIntersectionResult = getIntersectionInformation(lSubRectangle, lCollisionPoint, pLine),
            lRelativeVelocity   = lIntersectionResult.relativeVelocity,
            lCollisionNormal    = lIntersectionResult.collisionNormal,
            lRectPerp           = getPerp2(lIntersectionResult.rectangleCollisionPoint);

        const lNumerator = dot2(scaleVec2(lRelativeVelocity, -(1.0 + 1.0)), lCollisionNormal);
        const lLinearDenomPart = dot2(lCollisionNormal, (scaleVec2(lCollisionNormal, 1.0 / pRectangle.mass)));
        const lRotationalDenomPart = (Math.pow(dot2(lRectPerp, lCollisionNormal), 2) / pRectangle.getMomentOfInertia());

        const lImpulseMagnitude = lNumerator / (lLinearDenomPart + lRotationalDenomPart);

        pRectangle.position = lSubRectangle.position;
        pRectangle.velocity = addVec2(lSubRectangle.velocity, scaleVec2(lCollisionNormal, lImpulseMagnitude / pRectangle.mass));
        pRectangle.rotationVelocity = lSubRectangle.rotationVelocity + dot2(lRectPerp, scaleVec2(lCollisionNormal, lImpulseMagnitude)) / pRectangle.getMomentOfInertia();

        updateRigidBody2(pRectangle, pDeltaTimeSeconds - lSubdividedDeltaTime);
        return true;
    }

    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);
})()