Can anyone suggest a good article or tutorial on how to implement vector based collisions/movement in a game? Right now my game is just set up with simple x and y coordinates and dx/dy variables that represent the speed in both the x and y components of the game. If it helps anyone explain it a little better, I'm working on a breakout game and want to get the collision done properly.
Crossbones+ - Reputation: 5809
Posted 29 March 2013 - 04:24 AM
in essence, for a game that is strictly AABB(Axis-Aligned Bounding Boxes) boxes, you figure out which side of the brick you are colliding with, then negate the appropriate axis, for example, if your hitting the bottom or top of a brick, simply negate the y velocity. same for x when hitting the left/right. just be sure to also push the ball out of the brick/paddle, otherwise you might inadvertently cause the ball to continously flip it's velocity if it somehow ends up inside the brick.
Crossbones+ - Reputation: 1416
Posted 31 March 2013 - 05:44 AM
What's a good way to find out which side of the brick I'm colliding with?
On this previous thread, I wrote an explanation of how to solve the colliding side on a box-box collision exactly:
If you get stuck on some part of the explanation, feel free to ask. You should probably draw the situation on paper while following the logic.
(It's another matter whether a Breakout game is best served by strict rectangle-rectangle collision; ultimately you'd probably want to add a special case for handling corner hits so the ball will bounce off at different angles.)
Another approach is to take the angle between velocity vectors and then use that somehow to estimate the type of the hit. This isn't an exact solution and will require hand-tuning for specific rectangle shapes. You get the cosine of the angle by dividing the dot product with vector lengths: