summaryrefslogtreecommitdiff
path: root/frontend/_shared/math/collision.js
blob: 74ec5d8813249edb066f20554df1d78f901cef8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/// <reference path="vec2.js" />
/// <reference path="line2.js" />
/// <reference path="circle.js" />
/// <reference path="mat4.js" />

/**
 * 
 * @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');
    }
}