Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to know how to check for collisions efficiently in case where the player's box is larger than a map tile.

On the left is my usual case where I make 8 checks against every surrounding tile, but with the right one it would be much more inefficient.

(picture of two cases: on the left is the simple case, on the right is the one I need help with) enter image description here

How should I handle the right case?

share|improve this question
    
Can the character rotate? If yes, does it rotate only multiples of 90°? –  Marton Jul 6 '12 at 7:56
    
Have you profiled this? While obviously checking more tiles is going to be "inefficient", I don't think the hit from looping 28 tiles vs 8 tiles is going to cause any noticeable difference. In addition, you can make it faster because you only need to check the tiles in the direction it's moving. –  Richard Marskell - Drackir Jul 6 '12 at 11:46
    
Oh yeah, I didn't think of that for some reason. Anyway, efficient or not, it sure doesn't look well enough to have special case for each tile in code. –  user1306322 Jul 6 '12 at 15:41

1 Answer 1

up vote 0 down vote accepted

I think you can only optimize your existing method if the player's box doesn't rotate (or rotates only at multiples of 90 degrees). In this case, I would do the following:

Let the corners of the player's box be A, B, C and D, and (Ax, Ay) the coordinates of A, etc. The letters go counter-clockwise, starting from the upper left corner.

Step 1: You can easily get the indexes of the tiles which contain each of these corners. Use integer division, and the (fixed) size of your tiles. Let these indexes be (Ai, Aj), (Bi, Bj) etc.

Step 2: The following tiles (indexes: (Ri, Rj)) will be completely inside the player's box:

(Ai+1, Aj+1) <= (Ri, Rj) <= (Ci-1, Cj-1)

Step 3: The following tiles will be partially intersecting with the player's box:

(Ri == Ai) && (Aj <= Rj <= Bj)

(Ri == Di) && (Dj <= Rj <= Cj)

(Rj == Aj) && (Ai <= Ri <= Di)

(Rj == Bj) && (Bi <= Ri <= Ci)

Implying that i is the horizontal index.

share|improve this answer
    
Thanks, but I'm not sure if it's possible to check for partial or complete intersection of rectangles within XNA. –  user1306322 Jul 6 '12 at 10:45
    
Ermm... I don't think you understand what I wrote. This is how you actually calculate if the rectangles are intersecting (partially or fully). –  Marton Jul 6 '12 at 13:52
    
Oh, yeah. I was sleepy at the time. Maybe I still am :) –  user1306322 Jul 6 '12 at 15:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.