Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Share your collisiondetection techniques!  (Read 4004 times)

0 Members and 1 Guest are viewing this topic.

Haikarainen

  • Guest
Share your collisiondetection techniques!
« on: June 30, 2011, 01:11:02 am »
I'd thought you guys could share the techniques and algorithms you use for collision detection here, i wont be able to upload any of mine atm since im not home.

It is, in my point of view, a really confusing bit wich can be achived thru many ways, Share ! :D

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Share your collisiondetection techniques!
« Reply #1 on: June 30, 2011, 03:19:00 am »
What do you mean with techniques? Anything from speeding up the process to how to design an entire system? Or just specific "SphereVsBox" algorithms?

This subject is pretty huge :P
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Haikarainen

  • Guest
Share your collisiondetection techniques!
« Reply #2 on: June 30, 2011, 04:46:20 am »
Quote from: "Groogy"
What do you mean with techniques? Anything from speeding up the process to how to design an entire system? Or just specific "SphereVsBox" algorithms?

This subject is pretty huge :P


Hehe yup, thats why i created this thread. I guess anything goes, but my specific thought was regarding game-development. Like platform, top down, anything from box to pixel perfect.

danman

  • Hero Member
  • *****
  • Posts: 1121
    • View Profile
    • Email
Share your collisiondetection techniques!
« Reply #3 on: June 30, 2011, 10:31:52 pm »
Quote

Hehe yup, thats why i created this thread. I guess anything goes, but my specific thought was regarding game-development. Like platform, top down, anything from box to pixel perfect.


To summary, anythings  :D .

I'll try later if I'll recall this post, but you can use google search code to find the heaven door ;) .
Pointilleur professionnel

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Share your collisiondetection techniques!
« Reply #4 on: July 01, 2011, 12:06:32 am »
My latest [serious] game attempt (2D platformer) does everything line based.  The map itself is not tile based like many platformers.  Instead it consists of a series of line segments forming the walls/ground.  This allows for slopes at any angle.

Objects are represented by "a bounding box with a twist".  Basically the bottom-center point represents the "foot".  The bottom left and right corners are raised.  So it ends up looking kind of like this:
Code: [Select]

__
||
\/


How steep the 'V' at the bottom is determines how steep a slope the object is able to climb.


The actual collision detection is all based on line-line collisions.  When an object moves, I draw lines from each of the corners moving in the direction the object wants to move.  If those lines intersect with any wall lines, then I stop the object.

There are other tricks to get it all working, but that's the general idea.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Share your collisiondetection techniques!
« Reply #5 on: July 01, 2011, 02:23:11 am »
I use the Separating Axis Theorem. It works for any two convex polygons, is incredibly fast, and the bounding box check is a special case of it.
I use the latest build of SFML2

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Share your collisiondetection techniques!
« Reply #6 on: July 01, 2011, 04:43:44 am »
Can you elaborate on that?

I know the separating axis theorum is that any two convex polygons that do not intersect have an line that separates them.  However I never understood how you'd go about finding that line and/or whether or not it exists.

I'd be very interested in a more detailed explanation and/or example code.

EDIT:  and I can see how it could apply to object/object collision, but how could you apply it to object/world collision?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Share your collisiondetection techniques!
« Reply #7 on: July 01, 2011, 09:41:37 am »
Here is a tutorial about the Separating Axis Theorem:
http://www.metanetsoftware.com/technique/tutorialA.html
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Share your collisiondetection techniques!
« Reply #8 on: July 01, 2011, 09:55:21 pm »
Quote from: "Disch"
I'd be very interested in a more detailed explanation and/or example code.

EDIT:  and I can see how it could apply to object/object collision, but how could you apply it to object/world collision?
There's some example code in the Utilities.cpp file of my Terra Game Engine. It's the function IsColliding().

If you're doing a tile-based world, just treat the tiles like objects.
I use the latest build of SFML2

Haikarainen

  • Guest
Share your collisiondetection techniques!
« Reply #9 on: July 16, 2011, 02:21:32 pm »
Time to share mine! This is basically for a platformer, where i have a collisionbox for the player, that collides with subrects of sf::Images(aka tiles).

It has 4 boolean functions: OverlapsLeft(), OverlapsRight(), OverlapTop(), and OverlapsBottom(). What they do is they first check if the tile intersects with any side(wich is 1px on one axis, and width/height of the collisionbox for the other, so for OverlapsLeft i have x=1, and y = height), if they do, it iterates the tiles pixels in that intersection. If any of the pixels has an alphavalue of 128 or greater, it returns true.

Code for Overlaps top is found here:
https://legacy.sfmluploads.org/index.php?page=view_code&id=53

Then i just check for collisions when moving left or right, and pretty much every frame for top/bottom.

Sui

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://www.suisoft.co.uk/
Recursive Dimensional Clustering
« Reply #10 on: July 17, 2011, 11:03:13 am »
When I profiled my first game (Gravity Core) I found that a major CPU drain was collision checking.

The game was checking every game entity against every other which is an exponential growth problem.

i.e.
Checking 5 objects against 4 (excl self) = 20 checks
Checking 10 objects against 9 = 90 checks
Checking 100 objects against 99 = 9900 checks!!

To help with this, I implemented a heuristic algorythm that clusters together entities so that only relevant groups need to be checked.

It was based on an excellent article in Game Programming Gems 2. (These are great books - just not cheap!)
http://www.amazon.co.uk/Game-Programming-Gems-Vol-CD/dp/1584500549/ref=sr_1_1?ie=UTF8&s=books&qid=1310893296&sr=8-1

If you shop around, there are some reasonably priced second hand ones knocking around (e.g. Amazon resellers on Amazon.COM).

It's well worth investigating.

If anyone's interested I can try to dig out some sourcecode, though it will need some rework as it's tied into my sprite interface.
Gary Marples, Developer of games and other stuff.
www.suisoft.co.uk/games/BlogTwitter