# 2D Physics Library

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm working on a shareware game for Mac/Windows and I am constantly running into problems with my weak knowledge of physics and response programming. The game engine is finished, but I still have problems like enemies getting stuck in walls, bullets firing through them, and general colision response not feeling correct. I have a map of around 2000 convex, immobile 2D objects (with up to 32 vertices each) that I am trying to collide fast moving bullets (points), and players (moving 2D objects with up to 32 vertices). Are there any libraries out there that might help me? I've looked at free the 3D physics engine giants ODE and Newton and I'm not really sure how I would go about applying a 3D engine to a 2D world. I'm programming in C using XCode with GCC 4.01 for Mac and MSVC++ on the Windows side. Sound and input is handled by SDL and graphics by OpenGL. Thanks!

##### Share on other sites
I don't know of any currently available library that focuses on 2d collision detection and response (that doesn't mean there isn't one). However, the problems you described are certainly solvable, so I wouldn't give up on writing the code yourself (perhaps with a little help from the folks on this forum).

It sounds like you need:

1. Line segment vs. convex 2d polygon intersection
2. Swept convex 2d polygon vs. convex 2d polygon intersection

You would use the first for fast-moving projectiles, by representing the projectile motion over the timestep as a segment and intersecting that segment with the obstacles. I have code for 2d linear component vs. convex polygon intersection which I will be glad to share; let me know if you'd like me to post it.

Number 2 is a little more complicated, but quite manageable. I have code for this also, but it's sort of in disarray and might be hard to put together and post. oliii also has code for 2d polygon vs. polygon collision detection and response which he has made available. There are also some good references available if you're interested in implementing it from scratch. I can provide more details on that if you're interested.

With objects in the thousands, you may also need a broad-phase step, which is another topic. In short though, the problems you mentioned have established solutions which aren't too difficult to implement.

##### Share on other sites
some things to look at:

flatland

There are other wrappers around 3D libraries to do 2D physics I think.

dolphin

However, it's not obvious from your post that you actually want a physics engine. If you just want to do "player" movement and intersection tests, then that's not physics...

##### Share on other sites
Well, I suppose it's not really a physics engine I want. After looking further into it, what I really want is a C function that checks to see if two 2D polygons (int poly[2][32]) have collided and if so move the fast moving one (player) outside of the solid, non-moving one (map piece). The best method appears to be using the Seperation Axis Theorem, which I really don't fully understand. Does anyone have any suggestions/code snippits?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 23
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
634782
• Total Posts
3019267
×