1

Possible Duplicate:
Circle-Rectangle collision detection (intersection)
Collision detection between a line and a circle in JavaScript

This is more pseudo than actual working code...

var dX = rectanlge.x - circle.x; var dY = rectangle.y - circle.y; var distance = Math.sqrt((dX*dX)+(dY*dY)); if(distance < circle.radius){ //Collision } 

This is all I have so far to detect whether a circle and rectangle collide in a canvas animation. I am clearly going wrong, can some one point me in the right direction?

rectangles = []; var rectangle = function(x,y,width,height){ this.x = x; this.y = y; this.width = width; this.height = height; }; 

Then I would loop through this array

3
  • How is your rectangle defined? Commented Dec 4, 2012 at 22:41
  • I have added that into my question Commented Dec 4, 2012 at 22:44
  • For starters, you have rectanlge instead of rectangle. Commented Dec 4, 2012 at 23:41

1 Answer 1

2

You will find very nice algorithm there: http://www.migapro.com/circle-and-rotated-rectangle-collision-detection/

Sign up to request clarification or add additional context in comments.

Comments