2D collisions

Started by
4 comments, last by Guthur 15 years, 4 months ago
How do you make it work? How are 2d collisions handled? I am making an asteroids sort of game and I need to know when a missile hits an objects or an object collides with an other object and so on. Any info on how to acomplish this would be appreciated. Edit: With directx and c++ Thanks.
Advertisement
There are some articles here and here. If you want something more advanced, look here.
I looked into those articles and well.. about the n one I have little idea how to put that into code :S... I can't get much from the source code cause I only know c++(so far).

A question I have is so the basics is check if two objects' positions overlap do you have to make this check for every object, for every possible object? How is that possible for objects that become created 'in-game'? Wouldn't it become unmanagable pretty fast? Isnt there an algorithm to work this out?
If you have lots of objects in your game, you can use spatial partitioning to reduce the number of collision checks. One simple way to do this is to use a quadtree. That article uses the quadtree for visibility testing, but the same idea can be applied to collision detection.

Another method is to divide your objects into collision groups, where some groups only need to be tested against some other groups, and not all groups need inter-object testing.

For example, in a typical game you probably don't want to test if bullets collide with each other. So you would create a collision group for the bullets, and this group won't have inter-object testing.
Hi,

I think you should start with something very simple and not optimized to get a basic idea of performing collision detection between game entities.

Let's say you have your game's main character and one obstacle!
You must find a way to

- Do the collision check between main character and the obstacle in each game loop.
- Create a function wich returns true if they're colliding.

Now, if you have more obstacles you need to do the collision check for all of them and not just for one:

- main character, obstacle_0
- main character, obstacle_1
- ...
- main character, obstacle_n

A good idea would be to put all the obstacles into an array, or even better in a list, so you can do the check very easily

for( int i = 0; i < OBSTACLES_AMOUNT; i ++ ){    if( checkCollision( main_character, obstacles ) )<br>    {<br>         <span class="cpp-comment">// do collision response</span><br>    }<br>}<br><br></pre></div><!–ENDSCRIPT–><br><br><br>
You could go for the really simple option of using a 3rd Party Library ;).

Thats what I decided to do; physics is cool but unfortuately it can take time to get all the understanding in place to produce an effective simulation.

Though if you'd like the practice its a worthwhile exercise, and you may only want simple collision detection in which case a full physics SDK might be a little bit of overkill; I wanted friction and forces in my software.


Innovation not reiterationIf at any point I look as if I know what I'm doing don't worry it was probably an accident.

This topic is closed to new replies.

Advertisement