1
\$\begingroup\$

This question already has an answer here:

So, listening to this very smart piece of advice, I've already completed a basic Tetris game. Moving on, I started a small breakout. But suddenly : a nightmare came. Collisions.

Since I've been struggling with this a lot, I read a bunch of article of the web. Being a beginner, I thought that, in the first approach, I would only use Rectangle to detect collisions.

Right.

Now I still have some problems.

First of all, all the articles I stumbled upon on the Internet told me how to detect collisions, e.g. checking if objects A and B collided with each other. But it is not what I really need. In order to respond properly, I need to tell my objects if the collision happened on the X or Y axis and then setting the new velocity according to some rules. The problem is that there are so many possibilities when making the distinction between types of collisions that I don't think I've taken the right path.

The second point (which, I believe, is really tied with the first) is the pattern in which the detection and the resolution of a collision happen. Right now, I have a CollidableObject classes and a CollisionDetector classes which knows about these objects. Each frame, the detector is looking for collision (or at least for me, is trying) and when he finds one, it alerts each collided object, saying "Hey you ! You've have collided with Z on the X axis, change that velocity now." and the object abides.

The main issue right now is that I can detect the collision axis and therefore, when saying "Hey you ! You collided !", I cannot respond properly. In which direction should my velocity change ?

I've been thinking a lot about this lately, trying to figure it out on my own, but I haven't find something that I liked so I am asking here now.

Thank you for your answers !

(Note : I don't know if this is relevant, but I'm coding in C++ using SDL)

\$\endgroup\$

marked as duplicate by Anko, congusbongus, MichaelHouse Dec 30 '14 at 19:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ Is this just a long-winded way of asking how to detect the direction a collision came from? \$\endgroup\$ – Anko Dec 24 '14 at 1:58
  • 1
    \$\begingroup\$ Indeed. I'm always writing something way too long in order to clearly state why I had such issues but I would need to be more concise sometimes. Anyway, your linked helped a lot ! Thank you very much !! \$\endgroup\$ – Rivten Dec 27 '14 at 2:20
0
\$\begingroup\$

The most basic response to a collision like this is, "Reverse velocity in the direction of the collision". If you bump something up top, change the upwards velocity to a downwards velocity. Leave the side-to-side velocity just the same.

If your objects can move more than 1 "game cell" per frame, then you may also need to backtrack to the point of the collision, and apply however far you went into the barrier, as having bounced and moved away from the barrier.

This is the main difference from basic Tetris, where your game objects move only in "whole cell" steps, and no fixups like that are needed.

Sometimes you need to do this multiple times. For example, if the ball goes fast into a corner collision, it can bounce off both walls within the single update.

Hope that helps!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.