2
\$\begingroup\$

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.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

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?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ +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. \$\endgroup\$
    – Alan
    Aug 14, 2010 at 19:01
  • \$\begingroup\$ 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 \$\endgroup\$
    – Kaj
    Aug 14, 2010 at 19:07
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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 \$\endgroup\$
    – Kaj
    Aug 19, 2010 at 6:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .