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.

Per this excellent question, and excellent answers, I am attempting to write a breakout clone.

Regarding the collision detection against the walls, does XNA provide a set of helper methods/objects to determine which side of the rectangle was hit? I'd like to use the Vector2.Reflect method to properly create a new velocity vector, but it requires the proper normal. I can write the methods to figure out which side was hit, and thus which normal to use, but I wanted to know if XNA provided code to handle this already.

share|improve this question

2 Answers 2

I honestly don't know, but I would think breakout is one of the suggested games because that's eactly the kind of problem you learn to solve yourself. I'd go as far as to say that writing your own vector2.Reflect would benefit you.
I mean.....if you'd use supplied methods wouldn't you sidestep the learning/wing earning process?

share|improve this answer
1  
+1. I see what you're getting at, and I agree with you to a point, but is it important that every programmer have written their own hash map? Or is it more important that they understand how a hash map works and when to use it. To me, the latter is more important--perhaps because I come from the "programmer-time-is-expensive-dont-reinvent-the-wheel" business world. –  Alan Aug 14 '10 at 19:01
    
Fair point. Although a good drilling in vector math will prove useful more often than understanding the internals of a hashmap (in my opinion, although I prefer to know both). I'm a hack of a programmer, but the fact that I wrote this and similar code from scratch hundreds of times by now has firmly ingrained it in my system. But I see your point and I guess it kinda depends on where you come from. Realize though that the industry is hearing voices that there's a lack of generalists with deep knowledge - and don't forget we're not reinventing the wheel, we're trying to create a better one –  Kaj Aug 14 '10 at 19:07

I don't know if you can detect which side was hit by default (for a normal collision check in most cases the answer is no, it just returns a boolean of whether there's a collision or not), but there are other ways around this.

Obviously if you just say "is there a collision? Reflect" you can run into a problem where the ball hits the wall, reflects but not enough to actually make it back to "outside" the wall, reflects again, and so on every frame where it just gets stuck in the wall forever. So you need to avoid that. One of many ways of doing this: treat the ball as a state machine that is by default in the "non-reflecting" state. When in this state and you detect a collision, change it to the "reflecting" state. When in the reflecting state, ignore collisions, and only return to the non-reflecting state when there is NO collision.

Another problem: how do you tell the difference between a side collision (horizontal reflection) and a top or bottom collision (vertical reflection)? The original Breakout actually didn't make this distinction, so much; when the ball hit a brick, it would reflect vertically no matter where it hit, and the walls and ceiling were presumably hard-coded (checking the x or y coordinates of the ball). Changing the trajectory on collision with paddle is just a matter of comparing the x coordinate of the ball with the paddle to figure out sides. You could use that method for other things as well, so if you wanted the ball to reflect horizontally OR vertically against a brick, I'd be comparing the coordinates of the brick with the ball.

share|improve this answer
    
If you are inclined to treat the ball as a sphere there's no need to do the state switching as it should always return to a non-colliding state after a reflect - it's moving when it's incoming so there is speed after the collision. This does however assume that you detect your collision when the edge of the ball hits, which you generally do with a sweep (see if the distance of the line segment from the center of the beginning of circle at the start of the move to that on the end of the move gets closer to any edge than the length of your radius, if so, determine when, and resolve the collision –  Kaj Aug 19 '10 at 6:53

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.