Convert Figma logo to code with AI

slembcke logoChipmunk2D

A fast and lightweight 2D game physics library.

2,200
347
2,200
60

Top Related Projects

12,540

Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.

2D physics engine for games

Quick Overview

Chipmunk2D is a fast and lightweight 2D rigid body physics library written in C. It's designed to be simple, portable, and efficient, making it suitable for games and other real-time simulations. The library provides a robust set of collision detection and physics simulation features.

Pros

  • High performance and optimized for real-time applications
  • Cross-platform compatibility (works on various operating systems and devices)
  • Extensive documentation and examples
  • Active community and ongoing development

Cons

  • Steeper learning curve compared to some higher-level physics engines
  • Limited to 2D physics simulations
  • Requires manual memory management (being a C library)

Code Examples

  1. Creating a simple body and shape:
cpSpace *space = cpSpaceNew();
cpBody *body = cpBodyNew(1.0, cpMomentForCircle(1.0, 0, 25, cpvzero));
cpBodySetPosition(body, cpv(100, 100));
cpSpaceAddBody(space, body);

cpShape *shape = cpCircleShapeNew(body, 25, cpvzero);
cpSpaceAddShape(space, shape);
  1. Applying forces to a body:
cpVect gravity = cpv(0, -980);
cpBodyApplyForceAtWorldPoint(body, gravity, cpBodyGetPosition(body));
  1. Stepping the simulation:
cpFloat timeStep = 1.0/60.0;
cpSpaceStep(space, timeStep);
  1. Handling collisions:
cpCollisionHandler *handler = cpSpaceAddCollisionHandler(space, COLLISION_TYPE_A, COLLISION_TYPE_B);
handler->beginFunc = onCollisionBegin;
handler->separateFunc = onCollisionSeparate;

static cpBool onCollisionBegin(cpArbiter *arb, cpSpace *space, void *data) {
    // Handle collision begin
    return cpTrue;
}

static void onCollisionSeparate(cpArbiter *arb, cpSpace *space, void *data) {
    // Handle collision separation
}

Getting Started

  1. Download Chipmunk2D from the GitHub repository or use a package manager.
  2. Include the Chipmunk2D headers in your project:
#include <chipmunk/chipmunk.h>
  1. Link against the Chipmunk2D library when compiling.
  2. Initialize a space and start adding bodies and shapes:
cpSpace *space = cpSpaceNew();
cpSpaceSetGravity(space, cpv(0, -980));

// Add bodies and shapes here

// In your game loop:
cpFloat timeStep = 1.0/60.0;
cpSpaceStep(space, timeStep);

// Don't forget to free the space when you're done:
cpSpaceFree(space);

Competitor Comparisons

12,540

Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.

Pros of Bullet3

  • More comprehensive 3D physics simulation, including soft body dynamics
  • Wider industry adoption and support, used in many AAA games and films
  • Extensive documentation and community resources

Cons of Bullet3

  • Steeper learning curve due to its complexity
  • Higher computational overhead, especially for simpler 2D scenarios
  • Larger codebase and integration footprint

Code Comparison

Chipmunk2D (2D circle collision):

cpShape *circle = cpCircleShapeNew(body, radius, cpv(0, 0));
cpSpaceAddShape(space, circle);

Bullet3 (3D sphere collision):

btCollisionShape* sphereShape = new btSphereShape(radius);
btRigidBody* rigidBody = new btRigidBody(mass, motionState, sphereShape);
dynamicsWorld->addRigidBody(rigidBody);

Summary

Bullet3 offers a more robust 3D physics engine with broader capabilities, making it suitable for complex simulations in games and visual effects. However, this comes at the cost of increased complexity and computational requirements. Chipmunk2D, while more limited in scope, provides a simpler and more lightweight solution for 2D physics, which can be advantageous for certain types of projects, especially those targeting mobile platforms or requiring minimal overhead.

2D physics engine for games

Pros of LiquidFun

  • Supports fluid and soft body simulation, offering more advanced physics capabilities
  • Backed by Google, potentially providing better long-term support and resources
  • Designed with mobile platforms in mind, optimized for performance on various devices

Cons of LiquidFun

  • Less active development and updates compared to Chipmunk2D
  • Steeper learning curve due to more complex features and API
  • Limited documentation and community support

Code Comparison

Chipmunk2D (creating a simple body):

cpBody *body = cpBodyNew(1.0, cpMomentForCircle(1.0, 0, 30, cpvzero));
cpBodySetPosition(body, cpv(100, 100));
cpSpaceAddBody(space, body);

LiquidFun (creating a simple body):

b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(100.0f, 100.0f);
b2Body* body = world->CreateBody(&bodyDef);

Both libraries offer efficient 2D physics simulations, but LiquidFun provides more advanced features at the cost of complexity. Chipmunk2D is simpler to use and has more active development, while LiquidFun offers unique capabilities like fluid simulation. The choice between them depends on the specific requirements of your project and the level of physics complexity needed.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

!http://files.slembcke.net/chipmunk/logo/logo1_med.png!

h2. NEW IN CHIPMUNK 7

Chipmunk 7 is complete and now includes the ARM NEON optimizations, the autogeometry code, and the mulithreaded solver.

The latest "programming guide":http://chipmunk-physics.net/release/ChipmunkLatest-Docs/ is available.

h2. ABOUT:

Chipmunk2D is a simple, lightweight, fast and portable 2D rigid body physics library written in C. It's licensed under the unrestrictive, OSI approved MIT license. My aim is to give 2D developers access to the same quality of physics you find in newer 3D games. I hope you enjoy using Chipmunk2D!

h2. FEATURES:

  • Designed specifically for 2D video games.
  • Circle, convex polygon, and beveled line segment collision primitives.
  • Multiple collision primitives can be attached to a single rigid body.
  • Fast broad phase collision detection by using a bounding box tree with great temporal coherence or a spatial hash.
  • Extremely fast impulse solving by utilizing Erin Catto's contact persistence algorithm.
  • Supports sleeping objects that have come to rest to reduce the CPU load.
  • Support for collision event callbacks based on user definable object types types.
  • Flexible collision filtering system with layers, exclusion groups and callbacks. ** Can be used to create all sorts of effects like one way platforms or buoyancy areas. (Examples included)
  • Supports nearest point, segment (raycasting), shape and bounding box queries to the collision detection system.
  • Collision impulses amounts can be retrieved for gameplay effects, sound effects, etc.
  • Large variety of joints - easily make vehicles, ragdolls, and more.
  • Joint callbacks. ** Can be used to easily implement breakable or animated joints. (Examples included)
  • Maintains a contact graph of all colliding objects.
  • Lightweight C99 implementation with no external dependencies outside of the Std. C library.
  • "Many language bindings available":http://chipmunk2d.net/bindingsAndPorts.php.
  • Simple, read the "documentation":http://chipmunk2d.net/documentation.php and see!
  • Unrestrictive MIT license

h2. CONTRACTING:

Howling Moon Software (my company) is available for contracting if you want to make the physics in your game really stand out. Given our unique experience with the library, we can help you use Chipmunk to its fullest potential. Feel free to contact us through our webpage: http://howlingmoonsoftware.com/

h2. BUILDING:

Mac OS X: There is an included Xcode project file for building the static library and demo application. Alternatively you could use the CMake files or the macstatic.command script inside the xcode/ directory to build a static lib and package up the headers for you.

iPhone: A native Objective-C API is included. The Xcode project can build a static library with all the proper compiler settings. Alternatively, you can just run iphonestatic.command in the xcode/ directory. It will build you a fat library compiled as release for the device and debug for the simulator. After running it, you can simply drop the Chipmunk-iOS directory into your iPhone project!

UNIXes: A forum user was kind enough to make a set of CMake files for Chipmunk. This will require you to have CMake installed. To build run 'cmake .' then 'make'. This should build a dynamic library, a static library, and the demo application. A number of people have had build errors on Ubuntu due to not having GLUT or libxmu installed.

Windows: Visual Studio projects are included in the msvc/ directory. While I try to make sure the MSVC 10 project is up to date, I don't have MSVC 9 to keep that project updated regularly. It may not work. I'd appreciate a hand fixing it if that's the case.

h2. GET UP TO DATE:

If you got the source from a point release download, you might want to consider getting the latest source from GitHub. Bugs are fixed and new features are added regularly. Big changes are done in branches and tested before merging them in it's rare for the point release downloads to be better or more bug free than the latest code.

Head on over to "GitHub":https://github.com/slembcke/Chipmunk2D and experience the future TODAY! (Okay, so maybe it's not that exciting.)

h2. GETTING STARTED:

First of all, you can find the C API documentation in the doc/ directory.

A good starting point is to take a look at the included Demo application. The demos all just set up a Chipmunk simulation space and the demo app draws the graphics directly out of that. This makes it easy to see how the Chipmunk API works without worrying about the graphics code. You are free to use the demo drawing routines in your own projects, though it is certainly not the recommended way of drawing Chipmunk objects as it pokes around at the undocumented/private APIs of Chipmunk.

h2. SUPPORT:

The best way to get support is to visit the "Chipmunk Forums":http://chipmunk2d.net/forum/. There are plenty of people around using Chipmunk on the just about every platform I've ever heard of. If you are working on a commercial project and want some more direct help, Howling Moon Software is also available for "contracting":http://howlingmoonsoftware.com/.