Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411414 Posts in 69360 Topics- by 58415 Members - Latest Member: sophi_26

April 16, 2024, 01:13:38 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)2D Collision Detection - Single File Header (yet another)
Pages: [1]
Print
Author Topic: 2D Collision Detection - Single File Header (yet another)  (Read 2428 times)
qMopey
Level 6
*


View Profile WWW
« on: February 12, 2017, 07:27:49 PM »

Note: the header was renamed to cute_c2.h (the links below still work though).

Lots of people around here are making sweet 2D games with lots of small 2D shapes, like AABBs (tiles) and circles. While working on my own game I made a single-file header called tinyc2. The code is all in plain C with no build steps, makefiles, or any dependencies (other than math.h). I am assuming most 2D games do one of two things for collisions in their game:

  • Build Box2D and use the collisions, possibly disabling the physics
  • Roll a custom solution and fiddle around with it endlessly

The pain of the second option is nearly unbearable, especially when the action is in other parts of game development. Crunching around over and over on collision bugs can be nasty. The first option is a pretty good choice, but downloading and figuring out Box2D, and then disabling the physics parts as necessary can also be quite a pain. So I implemented this header as a third option:

  • Include tinyc2.h and call a single function

I am hoping some peeps around these forums like the header style and try it out! Here is a gif of the demos/examples/tests:



A lot of games on tigsource seem to make use of both trees or grids. tinyc2 was designed with these use cases in mind -- I myself use a grid that indexes into an array of shapes.

Here's a quick example of what using the header looks sort of like. It's a function that was debug drawing circles, AABBs and capsule and colliding some of them together:

Code:
void TestBoolean0( )
{
c2AABB aabb;
aabb.min = c2V( -40.0f, -40.0f );
aabb.max = c2V( -15.0f, -15.0f );

c2Circle circle;
circle.p = c2V( -70.0f, 0 );
circle.r = 20.0f;

c2Capsule capsule;
capsule.a = c2V( -40.0f, 40.0f );
capsule.b = c2V( -20.0f, 100.0f );
capsule.r = 10.0f;

if ( c2CircletoCircle( user_circle, circle ) ) tgLineColor( ctx, 1.0f, 0.0f, 0.0f );
else tgLineColor( ctx, 5.0f, 7.0f, 9.0f );
DrawCircle( circle.p, circle.r );

if ( c2CircletoAABB( user_circle, aabb ) ) tgLineColor( ctx, 1.0f, 0.0f, 0.0f );
else tgLineColor( ctx, 5.0f, 7.0f, 9.0f );
DrawAABB( aabb.min, aabb.max );

if ( c2CircletoCapsule( user_circle, capsule ) ) tgLineColor( ctx, 1.0f, 0.0f, 0.0f );
else tgLineColor( ctx, 5.0f, 7.0f, 9.0f );
DrawCapsule( capsule.a, capsule.b, capsule.r );

tgLineColor( ctx, 0.5f, 0.7f, 0.9f );
DrawCircle( user_circle.p, user_circle.r );
}

Anyways, hope someone finds it useful and gives it a try. Cheers!  WTF Toast Right
« Last Edit: October 01, 2018, 11:18:55 AM by qMopey » Logged
pmprog
Level 1
*


View Profile WWW
« Reply #1 on: February 14, 2017, 12:30:46 AM »

Looks great. I'll add it to my list of libs to try Smiley
Logged
neutonm
Level 0
***


Elusive Slacker


View Profile WWW
« Reply #2 on: February 14, 2017, 09:49:49 AM »

Same here, looked at the code, gonna include into favs. Will be really handy in future.

Good job, Mr.  Gentleman
Logged

InfiniteStateMachine
Level 10
*****



View Profile
« Reply #3 on: February 15, 2017, 09:08:07 AM »

Very cool. Definitely filling a void that I hate working on Smiley
Logged

qMopey
Level 6
*


View Profile WWW
« Reply #4 on: September 24, 2017, 02:29:55 PM »

Updated to 1.3.

  • Generic raycast function
  • Various bugfixes and quality of life improvements
  • New repo full of tests + renderings by sro5h

Let me know if anyone gives it a go! I'd love to see Smiley
Logged
Caravaggio
Level 0
**



View Profile
« Reply #5 on: September 29, 2018, 07:44:31 PM »

Oh, september of LAST year.  Well, is this still active then, I see the git was updated?  It looks interesting but I wasn't quite sure what it was from the OP alone.  Was it a variation of Box2D or something completely custom?
Logged
qMopey
Level 6
*


View Profile WWW
« Reply #6 on: September 29, 2018, 08:00:58 PM »

Yep still active and will be for many years. It's a header file that implements all kinds of 2D collision detection. No it is not affiliated with Box2D at all. It can be used to implement nearly any kind of 2D game.

The big advantages of using it are:
- Include a single header, and immediately have access to a full-featured set of 2D collision functions and types
- No dependencies at all -- works pretty much everywhere
- Battle tested and correctly implemented, bug-free, and very efficient code
- Can solve for collision manifold (the information needed to actually resolve collisions)
- Raycaysts
- GJK implementation (solves for closest points between two shapes)
- Good documentation
- Lots of examples
Logged
oahda
Level 10
*****



View Profile
« Reply #7 on: September 30, 2018, 01:06:21 AM »

This is really neat!
Logged

pelle
Level 2
**



View Profile WWW
« Reply #8 on: September 30, 2018, 01:01:26 PM »

Very nice. Thanks for bumping this thread so I noticed.

Not in need of this at the moment, but definitely saving for when it could be useful. Single-header libs in C are amazing for keeping things simple to integrate and portable.
Logged
qMopey
Level 6
*


View Profile WWW
« Reply #9 on: September 30, 2018, 02:53:34 PM »

Hey thanks you two! Very nice of you to say Smiley
Logged
Ordnas
Level 10
*****



View Profile WWW
« Reply #10 on: October 05, 2018, 04:15:46 AM »

Wow cool, it will be very handy, thank you!  Smiley
Logged

Games:

Lares Yamoir
Level 0
***


View Profile WWW
« Reply #11 on: October 05, 2018, 03:53:23 PM »

This looks intereseting. Just started working on my own game framework again, so this might become useful for learning purposes.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic