Breakout collision tunneling problem.

Started by
2 comments, last by Lizard2033 19 years, 10 months ago
Ok so I''m making a breakout/arkanoid clone for a starter project because this is the first game I''ve made. The problem Im having is with tunneling (checking for collision each frame but when it moves several pixels in one direction it can hop into or over objects and not realize the initial hit on the edge), the game works right now, I just want to fix the tunneling because it will make it look a lot better (you can see it "tuck under" the bricks every now and then). I''ve tried several methods but couldnt quite get them to work, basically what I tried was: 1. Main loop calls the ball objects update position method 2. The update position divides the velocites by the higher velocity to get an increment value to add to the position each time (ie. if xVelocity = 7, yVelocity = 4, dx = 1, dy = 4/7, so if you iterate the collision detection 7 times it moves the object the desired amount) 3. a while loops starts which adds dx and dy to and y respectively then calls the collision detection method which will return true if something has been hit. 4. if hitCheck returns true it breaks out of the while loop by returning false for the updatePosition method (this is just an arbitrary return to break out of the loop and method, it''s not used anywhere) Now right off the bat I know this will give me a one pixel overlap because I''m moving the ball then checking collision not the other way around but I''m ok with that, besides it''s a quick fix. But I was having issues with this system and I couldnt get it to work, can anyone notice a blatent reason why? Thanks -Alex
Advertisement
How is it not working? Remember that with pixels you''re dealing with integer values, so adding the fractional deltas might give you unpredictable results. Also, if you detect a collision you can back up to the previous position, so there will be no overlap.

If you''re dealing with integer values (pixels) you might look into the Bresenheim (sp?) line-drawing algorithm, which will give you all the pixels between two arbitrary positions. Then you can check these in order and stop when you detect a collision...
It sounds to me like you should be doing ray-casting. Assuming your ball is round, do an intersection of your ball startpos & direction vector with a circle around each of the brick''s corners (with the balls radius). Also check for intersection with each brick edge, offset by the radius.

The same things exist with collision for 3D games. You just need to adapt that to the 2D version, the you will never go through a brick no matter how fast the ball goes.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
If you test pixel for pixel collision, no matter what, regardless of how many trig identities or for loops you use, your gonna get tunneling before the ball deflects of the paddle. There''s two solutions. Once is is the simple one which I use in 2d games involving collision between polygons. Once you collision test returns true, or during the actual collision algorithm, when a collision becomes evident. Don''t just reverse the velocity of the ball/entity. You need to push it pixel by pixel until its collision point is no longer within the object bricks collision area/radius. The other method is using true vector collision. I don''t know enough about that yet to comment. if you want to see my collision code, just ask ! :]
I study day and night, memorizing the game.

This topic is closed to new replies.

Advertisement