views:

2895

answers:

9

Whats the best way to detect collisions in a 2d game sprites? I am currently working in allegro and G++

A: 

Collisions between what? It depends whether you use sprites, concave polygons, convex polygons, rectangles, squares, circles, points...

Thomas
Maybe edit your question to reflect that? and it's title?
Simucal
yeah thanks a lot man!
Abhishek Mishra
A: 

Maybe you can apply the techniques here to 2d: http://en.wikipedia.org/wiki/Collision_detection

Ingrid
+4  A: 

Any decent 2D graphics library will either provide its own collision detection functions for everything from aligned sprites to polygons to pixels, or have one or more good third party libraries to perform those functions. Your choice of engine/library/framework should dictate your collision detection choices, as they are likely far more optimized than what you could produce alone.

For Allegro there is Collegro. For SDL there is SDL_Collide.h or SDL-Collide. You can use I_COLLIDE with OpenGL. DarkBASIC has a built in collision system, and DarkPhysics for very accurate interactions including collisions.

Sparr
hey thanks for collegro, i am currently working in allegro
Abhishek Mishra
@Abhishek Mishra you might want to add that to your question, maybe someone else knows another nice solution for you.
Huppie
The Collegro url does not work, but you can find a thread about it here: http://www.allegro.cc/forums/thread/550371Also used for Allegro there is PPCol.
Stefan Hendriks
A: 

This question is pretty general. There are many ways to go about collision detection in a 2d game. It would help to know what you are trying to do.

As a starting point though, there are pretty simple methods that allow for detection between circles, rectangles, etc. I'm not a huge fan of gamedev.net, but there are some good resources there about this type of detection. One such article is here. It covers some basic material that might help you get started.

Basic 2d games can use rectangles or circles to "enclose" an object on the screen. Detection of when rectangles overlap or when circles overlap is fairly straightfoward math. If you need something more complicated (such as convex artibrary polys), then the solution is more complicated. Again, gamedev.net might be of some help here.

But really to answer your question, we need to know what you are trying to do? What type of game? What type of objects are you trying to collide? Are you trying to collide with screen boundaries, etc.

Mark
"I'm not a huge fan of gamedev.net"I usually don't use their tutorials myself. However, the forums are very active so you can search the forums for references to good places to find tutorials (outside of gamedev), as well as specific questions.I was more so trying to lead him in a good direction.
cs_student
+1  A: 

Use a library, I recommend Box2D

Niall
+3  A: 

There are a plethora of ways to detect collision detection. The methods you use will be slightly altered if depending on if your using a 2d or 3d environment. Also remember when instituting a collision detection system, to take into account any physics you may want to implement in the game (needed for most descent 3d games) in order to enhance the reality of it.

The short version is to use bounding boxes. Or in other words, make each entity in the world a box, then check if each of the axises of the box are colliding with other entities.

With large amounts of entities to test for collisions you may want to check into an octree. You would simple divide the world into sectors, then only check for collision between objects in the same sectors.

For more resources, you can go to sourceforge and search for the Bullet dynamics engine which is an open source collision detection and physics engine, or you could check out http://www.gamedev.net which has plenty of resources on copious game development topics.

cs_student
A: 

hi , In my application i am forcing one ball to collide with other one, i can detect that they are colliding now, but i am not able to understand how to detect the direction in which other ball should move, like in snooker, if ball collides on right side next ball moves towards left. I am using objective-c for iphone. we can detect by CGRectContainsPoints method to check that both balls are colliding but any idea to check in which direction other should move. Anyone knows that ? Thanks

A: 

Checking for collision between two balls in 2D is easy. You can google it but basically you check if the length of the two balls radius combined is larger or equal to the distance between the center of the two balls.

Then you can find the collision point by taking the unit vector between the center of the balls and multiply it with one of the balls radius.

Adam Smith
+1  A: 

Implementation of a collision detection system is a complicated matter, but you want to consider three points.

  • World of objects. Space Partitioning. If you do a collision check against every 2d sprite in your world against everything else, you'll have a slow slow program! You need to prioritize. You need to partition the space. You can use an orthogonal grid system and slice your world up into a 2d grid. Or you could use a BSP tree, using lines as the seperator function.

  • Broad phase collision detection This uses bounding volumes such as cylinders or elipses (whichever approximates the shape of your sprites the best) to determine whether or not objects are worth comparing in more detail. The math for this is easy. Learn your 2d matrix transformations. And for 2d intersection, you can even use high powered video cards to do a lot of the work!

  • Narrow phase collision detection Now that you've determined that two or more objects are worth comparing, you step into your fine tuned section. The goal of this phase is to determine the collision result. Penetration depth, volume encompassed, etc... And this information will be fed into whatever physics engine you got planned. In 3d this is the realm of GJK distance algs and other neato algorithms that we all love so much!

You can implement all of this generically and specify the broad and narrow resolutions polymorphically, or provide a hook if you're working in a lower level language.