Using ChipMunk + SDL2


#1

Hello,

i would like to implements pixel perfect collision detection using ChipMunk + SDL2

Could someone provides a little example of using SDL2 with chipmunk?
I haven’t find any tutorial on the web. Doesn’t seems to me that there are manual or documents related to this arguments.
I found some little projects on github, but none of this works.

Thank you very much in advance


#2

I ship an example called FlappyBlurrr as part of BlurrrSDK. It uses
SDL2’s 2D renderer and Chipmunk for collisions and gravity.

You can download it at:
https://blurrrsdk.com

You should be aware that Chipmunk is not designed with pixel-perfect
collision in mind. It is geometry based.

-Eric


#3

Thank you for the reply.

Looking at this old thread, chipmunk should have added Signed Distance Functions to calculate collisions even with complex concave / convex objects (https://chipmunk-physics.net/forum/viewtopic.php?f=1&t=94&sid=47bb8b1d433ee717f7303f276acccee5&start=10)

I would like to ask about it, but i can’t register to the chipmunk forum, even after sending many e-mails (never got a reply)

I Used Nape physics in flash, and the author created a special function to convert at runtime any bitmap into a physic body (bitmaptobody). Moreover you can scale or rotate the object (both graphical and physics object) and you can still check collision very well.

If only Nape were portable…


#4

Ah, I forgot that he added an “Autogeometry” feature to the (formerly)
Pro version. I’ve never used it, but I presume at initialization, it
generates a geometric bounds from a sprite. The Pro stuff is now all
open source, but last time I checked, I think it was implemented in
Objective-C so it is not really portable. But since he does not have
any dependencies on Apple frameworks, I imagine it shouldn’t be
terribly difficult to port it to C (but admittedly tedious).

Alternatively, since you already know how it works (Signed Distance
Functions), assuming you already know all the sprites of your game at
ship time, you could instead pre-create geometry from your game’s
sprites in some outside tool and just feed that computed geometry to
the normal Chipmunk APIs. There should be a performance advantage of
doing this as you won’t have to recompute the geometry from a sprite
on every creation.

-Eric


#5

Thank you.

Yes, there are some software around that permits to edit physics bodies (https://www.codeandweb.com/physicseditor).

Main problem is to get Chipmunk phisics working, atm.


#6

Hello,

i downlodaded the 170 MB of the SDK, but failed to find an example debug draw for chipmunk + SDL2. Could you please give more info?

Thank you


#7

Sorry, I didn’t write any examples using DebugDraw. If I recall, it
uses direct OpenGL drawing primitives which the SDL 2D API doesn’t
have equivalents for (right now).

It sounded like you wanted an example of SDL + Chipmunk, which is why
I referred you to my FlappyBlurrr example. It doesn’t use DebugDraw,
but it does use the normal Chipmunk API (with normal SDL 2D rendering
APIs). The collision shapes uses are pretty simple…boxes for the
pipes and a circle for the bird.

If you are using OpenGL directly in your SDL app, then I think you
should be able to use the DebugDraw stuff almost directly. But if you
want to use DebugDraw without using direct OpenGL, I suspect it could
be ported to use SDL_gpu (which also is included with Blurrr SDK).

Thanks,
Eric