Question

so I have a canvas element filled with alot of rectangles. Now I'm trying to check for collision of 2 rectangles so I'm trying to check if there is a rectangle underneath the other rectangle.

I could use a foreach loop to check but that would be inneficient if theres alot of rectangles.

Thanks.

Was it helpful?

Solution

I assume your rectangles are represented by objects.

The bad news:

Yes, you must use loops to hit-test each rectangle object against every other rectangle object.

The good news:

Modern browsers are very fast.

So start by coding your nested loops to hit-test all combinations of rectangles.

If the results are sluggish, look at this post which examines how to divide the canvas into subsections so that only those rectangles in a subsection need be tested against each other. The post uses java, but you can "see-it-in-c#" fairly easily.

http://gamedevelopment.tutsplus.com/tutorials/quick-tip-use-quadtrees-to-detect-likely-collisions-in-2d-space--gamedev-374

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top