Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collision detection between a line and a circle in JavaScript

I'm looking for a definitive answer, maybe a function cos I'm slow, that will determine if a line segment and circle have collided, in javascript (working with canvas)

A function like the one below that simply returns true if collided or false if not would be awesome. I might even donate a baby to you.

function isCollided(lineP1x, lineP1y, lineP2x, lineP2y, circlex, circley, radius) {

    ...
}

I've found plenty of formulas, like this one, but they are over my head.

like image 437
Jarrod Avatar asked Jun 09 '12 01:06

Jarrod


People also ask

How do you check if a line intersects a circle?

To determine the position of a line with respect to a circle, all we need to do is find its distance from the center of the circle, and compare it with its radius. Then, if the distance is less than the radius, the line must intersect the circle at two distinct points.

What is collision in Javascript?

The overlap of elements is what can be referred to as collision detection. This is a standard part of most games, when moving page elements to be able to detect the position and calculate if they are overlapping.

How do you find the distance between two circles in a 2d game?

Calculate center point (x + width/2, y + height/2) Do that for both circles. use trigonometric functions to calculate distance between those two points: difference of x in square plus difference of y in square, take the square root of that = distance.


2 Answers

Here you will need some Math:

enter image description here This is the basic concept if you don't know how to solve equations in general. I will leave the rest of the thinking to you. ;) Figuring out CD's length isn't that hard.

If you are asking how, that's how: enter image description here Finding collisions in JavaScript is kind of complicated.

like image 157
Derek 朕會功夫 Avatar answered Sep 22 '22 10:09

Derek 朕會功夫


I Spent about a day and a half to make it perfect.. Hope this helps.

function collisionCircleLine(circle,line){ // Both are objects

    var side1 = Math.sqrt(Math.pow(circle.x - line.p1.x,2) + Math.pow(circle.y - line.p1.y,2)); // Thats the pythagoras theoram If I can spell it right

    var side2 = Math.sqrt(Math.pow(circle.x - line.p2.x,2) + Math.pow(circle.y - line.p2.y,2));

    var base = Math.sqrt(Math.pow(line.p2.x - line.p1.x,2) + Math.pow(line.p2.y - line.p1.y,2));

    if(circle.radius > side1 || circle.radius > side2)
        return true;

    var angle1 = Math.atan2( line.p2.x - line.p1.x, line.p2.y - line.p1.y ) - Math.atan2( circle.x - line.p1.x, circle.y - line.p1.y ); // Some complicated Math

    var angle2 = Math.atan2( line.p1.x - line.p2.x, line.p1.y - line.p2.y ) - Math.atan2( circle.x - line.p2.x, circle.y - line.p2.y ); // Some complicated Math again

    if(angle1 > Math.PI / 2 || angle2 > Math.PI / 2) // Making sure if any angle is an obtuse one and Math.PI / 2 = 90 deg
        return false;


        // Now if none are true then

        var semiperimeter = (side1 + side2 + base) / 2;

        var areaOfTriangle = Math.sqrt( semiperimeter * (semiperimeter - side1) * (semiperimeter - side2) * (semiperimeter - base) ); // Heron's formula for the area

        var height = 2*areaOfTriangle/base;

        if( height < circle.radius )
            return true;
        else
            return false;

}

And that is how you do it..

like image 21
Coding Hub Avatar answered Sep 19 '22 10:09

Coding Hub