1
votes

I cant seem to be able to solve a math problem related to collision detection.

I got the basic idea. Find the difference between two opposite x and y values. Calculate the distance by multiplying the two results by itself to get the distance and then a check to see if the two balls collide. Then checking if any part of the angles intersect. Honestly it works somewhat but is still broken.

Problem: When starting the game and I let the ball collide with the visible half then the collison works and when the ball enters the non visible area the collision returns false. At the other hand, when restarting and letting it collide with the non visible area the collision returns true, then false when it enters the visible part of the semicircle.

What could it be? I suspect something due to my rotation. This is honestly weird. Here a demo:

http://jsfiddle.net/2rz296tf/17/

Below the essential formula that makes it "kind of" work:

var dx = a - b;
var dy = a - b;
var semi = Math.atan2(dx, dy)
var distance = Math.sqrt(dx * dx + dy * dy);

var hit = (distance < radiusA + radiusB) && (semi >= 0 && semi < angleB); 
1
Assuming 2 circles of equal radii, CircleA extends into both halves of target CircleB when centerPointA is closer than radiusB to centerPointB. This is true regardless of the angle of attack. Or are you testing if CircleA intersects 2 specific semi-circle parts of CircleB? Please clarify :-)markE
@markE hey, sure. I am testing for intersection with the line segments of the arc. I want to test the exact point of collision of the animated ball with the giant semi circle, but only the semi circle. This, however is not the case.Asperger
@markE here another demo with the desired result. jsfiddle.net/alnitak/fw9u73xcAsperger
@markE I think the rotation screws it up since it works in the second fiddle. To test it just click somewhere in the preview and you will see what I meanAsperger
I'm feeling dense today so maybe I'm asking the obvious: Do you want to known when over half the area of the approaching circle is contained in the gray semi-circle?markE

1 Answers

1
votes

You're comparing an angle to a length: semi < radiusB. That doesn't make any sense. You should compare semi to the orientation of your shield, not with its radius.

Looking at your code, you should do something like semi >= angle - Math.PI && semi <= angle instead. You'll need to implement a way to deal with the fact that angles run from -pi to pi and then loop around.