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'm making my first game, and in it I use a moving ball to hit static blocks (kinda like breakout).

Basically, I loop through all of the static blocks on the screen each frame, update the position of the ball based on its velocity, and if it collides with a block, the ball will bounce back off of the block.

If the ball hits one block exactly, then there is no problem whatsoever. But if the ball happens to hit two blocks at the same time (that is, it hits in between both blocks), it passes right through both of them--no deflection whatsoever.

(I know the looping is inefficient and I'm looking to correct that later, but if it has to do with the problem, I just hope someone can point to me why it is that the collision resolution fails when two similar objects are hit at once.)

share|improve this question
    
I think you need to explain a bit more the details of your algorithm. It is impossible to tell what is wrong from your description. –  sm4 May 30 '13 at 1:09
    
Yeah, I know it was a little broad, but I didn't want to make it too lengthy since I'm sure others have had similar problems (but I can't find anywhere -_- ). I got a good response below but if it doesn't work, I'll update with more detail. –  Luchis May 30 '13 at 2:14
add comment

1 Answer

up vote 0 down vote accepted

I actually ran into this exact problem not too long ago when making a similar game. Each frame, the ball would move to its next increment and sometimes the ball would increment itself into two blocks at the same time.

When I did a collision check against the blocks it would process the first one and alter its direction, but then before it got a chance to move it would process the second collision and change direction again, making it go the same direction it originally started in. This may be what's happening in your game.

I don't know how your collision detection is coded but make sure that if it detects a collision it breaks the loop so it won't try another one in the same frame. I also created a counter that was set to 2 each time a collision happened that it would increment -1 each frame so that a collision couldn't happen the frame after a collision already happened, only when the counter was equal to 0.

share|improve this answer
    
getsauce, that seems like a good idea. I think that could be it. I'm just checking responses but will implement it tomorrow. I'll mark as answer if it works. –  Luchis May 30 '13 at 2:13
    
Bingo! That was it. Thanks! –  Luchis May 30 '13 at 23:34
add comment

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.