summaryrefslogtreecommitdiff
path: root/frontend/_shared/math/collision.js
diff options
context:
space:
mode:
authorMatthew Kosarek <matthew.kosarek@vention.cc>2021-02-27 17:32:32 -0500
committerMatthew Kosarek <matthew.kosarek@vention.cc>2021-02-27 17:32:32 -0500
commitd1b528b01796601c2bfea7b1a9813e5907e1c728 (patch)
tree77e2200e930fcad2166edf1e3d31d4cd1211db56 /frontend/_shared/math/collision.js
parent026abdb98ad30209df0e88795f25b1f74556585e (diff)
Close to being done on line-circle collisions, but it appears we're running into a rotation problem. Going to work on square-line collisions in the meantime
Diffstat (limited to 'frontend/_shared/math/collision.js')
-rw-r--r--frontend/_shared/math/collision.js25
1 files changed, 14 insertions, 11 deletions
diff --git a/frontend/_shared/math/collision.js b/frontend/_shared/math/collision.js
index 74ec5d8..8e4be7d 100644
--- a/frontend/_shared/math/collision.js
+++ b/frontend/_shared/math/collision.js
@@ -2,22 +2,25 @@
/// <reference path="line2.js" />
/// <reference path="circle.js" />
/// <reference path="mat4.js" />
+/// <reference path="point2.js" />
/**
*
- * @param {circle} pCricle
+ * @param {circle} pCircle
* @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);
+function lineCircleCollision2(pCircle, pLine) {
+ return distanceFromPoint2ToLine2(pCircle.position, pLine) <= pCircle.radius;
+}
- if (Math.abs(lHeight - pCricle.radius) < pCricle.radius) {
- console.log('Intersection');
+function getLineCircleCollison2Data(pCircle, pLine) {
+ const lCollisionNormal = pLine.normal,
+ lCollisionPoint = addVec2(pCircle.position, scaleVec2(negate2(lCollisionNormal), pCircle.radius));
+
+ return {
+ relativeVelocity: subVec2(pCircle.velocity, pLine.velocity),
+ collisionNormal: lCollisionNormal,
+ firstPointOfApplication: subVec2(lCollisionPoint, pCircle.position),
+ secondPointOfApplication: subVec2(lCollisionPoint, pLine.start)
}
} \ No newline at end of file