2
votes

I am writing a collision detection of two circles. But what I really want is that the main object will only collide with other objects on half of its surface, like the image below shows:

enter image description here

The half cricle surface will be colliding with other circles from different directions. How should I write the function to simulate this?

What I have found out at the moment for two circles detection is like this:

var dx = circle1.x - circle2.x;
var dy = circle1.y - circle2.y;
var distance = Math.sqrt(dx * dx + dy * dy);

if (distance < circle1.radius + circle2.radius) {
    // collision detected!
}

But how about only half of the main circle gets involved? How about only a fraction of the circle surface gets involved in the collision? Say only Math.PI / 2 degree of the circle will be detecting the collision?

2

2 Answers

0
votes

Assuming your code for detecting collision is correct (I didn't test it) all you have to do is determine which circle is further to the right to know which one will collide.

Consider that your half circle in the image is circle1.

if (distance < circle1.radius + circle2.radius) {
    // collision detected!
    if(circle1.x < circle2.x){
        // Circle collided on the right side of the other circle,
        // which means it's a real collision..
    }
}
0
votes

Is that half-circle able to rotate? If not, then it's fairly easy: collision happens only on the right side, meaning that you have to check only between half_circle_center.x_position and half_circle_center.x_position + half_circle.radius