Convert Figma logo to code with AI

erincatto logobox2d-lite

A small 2D physics engine

1,014
133
1,014
4

Top Related Projects

9,619

Box2D is a 2D physics engine for games

2D physics engine for games

A fast and lightweight 2D game physics library.

14,257

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

2,680

JavaScript 2D physics library

Quick Overview

Box2D-Lite is a simplified version of the popular Box2D physics engine, designed for educational purposes. It provides a basic 2D rigid body simulation with a focus on simplicity and readability, making it ideal for learning about physics engines and game development.

Pros

  • Easy to understand and learn due to its simplified codebase
  • Lightweight and efficient, suitable for small projects or prototypes
  • Open-source and free to use
  • Serves as an excellent starting point for building custom physics engines

Cons

  • Limited features compared to the full Box2D engine
  • Not optimized for large-scale or complex simulations
  • Lacks advanced collision detection and resolution techniques
  • May not be suitable for production-ready games or applications

Code Examples

  1. Creating a world and adding bodies:
World world;
Body* ground = world.CreateBody(Vec2(0.0f, -10.0f), 0.0f);
ground->SetShape(Polygon::CreateBox(50.0f, 10.0f));

Body* box = world.CreateBody(Vec2(0.0f, 4.0f), 1.0f);
box->SetShape(Polygon::CreateBox(1.0f, 1.0f));
box->SetVelocity(Vec2(0.0f, 0.0f));
  1. Stepping the simulation:
float timeStep = 1.0f / 60.0f;
int iterations = 10;
world.Step(timeStep, iterations);
  1. Applying forces to bodies:
Vec2 force(10.0f, 0.0f);
box->ApplyForce(force);
  1. Querying body positions:
Vec2 position = box->GetPosition();
float angle = box->GetRotation();
printf("Box position: (%f, %f), angle: %f\n", position.x, position.y, angle);

Getting Started

  1. Clone the repository:

    git clone https://github.com/erincatto/box2d-lite.git
    
  2. Include the necessary headers in your project:

    #include "World.h"
    #include "Body.h"
    #include "Polygon.h"
    
  3. Create a world and add bodies:

    World world;
    Body* ground = world.CreateBody(Vec2(0.0f, -10.0f), 0.0f);
    ground->SetShape(Polygon::CreateBox(50.0f, 10.0f));
    
    Body* box = world.CreateBody(Vec2(0.0f, 4.0f), 1.0f);
    box->SetShape(Polygon::CreateBox(1.0f, 1.0f));
    
  4. Step the simulation in your main loop:

    float timeStep = 1.0f / 60.0f;
    int iterations = 10;
    while (running) {
        world.Step(timeStep, iterations);
        // Render your scene here
    }
    

Competitor Comparisons

9,619

Box2D is a 2D physics engine for games

Pros of Box2D

  • More comprehensive and feature-rich physics engine
  • Better performance for complex simulations
  • Wider community support and adoption

Cons of Box2D

  • Steeper learning curve due to increased complexity
  • Larger codebase, potentially harder to integrate into small projects
  • May be overkill for simple 2D physics needs

Code Comparison

Box2D-Lite:

class Body {
public:
    Vec2 position;
    float rotation;
    Vec2 velocity;
    float angularVelocity;
};

Box2D:

class b2Body {
public:
    b2Vec2 GetPosition() const;
    float GetAngle() const;
    const b2Vec2& GetLinearVelocity() const;
    float GetAngularVelocity() const;
};

Box2D-Lite is a simplified version of Box2D, designed for educational purposes and basic 2D physics simulations. It offers a more straightforward API and smaller codebase, making it easier to understand and integrate into simple projects. However, it lacks many advanced features and optimizations present in the full Box2D library.

Box2D, on the other hand, is a robust and widely-used physics engine that provides more accurate simulations, better performance for complex scenarios, and a rich set of features. It's suitable for professional game development and complex physics simulations but may be overwhelming for beginners or small-scale projects.

2D physics engine for games

Pros of LiquidFun

  • Advanced fluid simulation capabilities, including particle-based fluids
  • Optimized for mobile devices and supports GPU acceleration
  • Actively maintained by Google with regular updates and improvements

Cons of LiquidFun

  • Larger codebase and more complex to integrate compared to Box2D-Lite
  • Steeper learning curve due to additional features and functionality
  • May be overkill for simple 2D physics simulations

Code Comparison

Box2D-Lite (simple collision detection):

void World::Collide()
{
    for (int i = 0; i < bodyCount; ++i)
    {
        Body* bi = bodies + i;
        for (int j = i + 1; j < bodyCount; ++j)
        {
            Body* bj = bodies + j;
            if (bi->invMass == 0.0f && bj->invMass == 0.0f)
                continue;
            CollideCircles(bi, bj);
        }
    }
}

LiquidFun (particle system update):

void b2ParticleSystem::Solve(const b2TimeStep& step)
{
    if (m_count == 0)
    {
        return;
    }
    m_timestamp++;
    m_allParticleFlags = 0;
    for (int32 i = 0; i < m_count; i++)
    {
        m_allParticleFlags |= m_flagsBuffer.data[i];
    }
    // ... (additional particle system update logic)
}

A fast and lightweight 2D game physics library.

Pros of Chipmunk2D

  • More feature-rich, including advanced collision detection and constraint types
  • Better performance for complex simulations with many objects
  • Extensive documentation and community support

Cons of Chipmunk2D

  • Steeper learning curve due to more complex API
  • Larger codebase, potentially increasing project size
  • May be overkill for simple 2D physics simulations

Code Comparison

Box2d-lite example:

b2World world(gravity);
b2Body* body = world.CreateBody(bodyDef);
body->CreateShape(shapeDef);
world.Step(timeStep);

Chipmunk2D example:

cpSpace *space = cpSpaceNew();
cpBody *body = cpBodyNew(mass, moment);
cpShape *shape = cpCircleShapeNew(body, radius, cpvzero);
cpSpaceAddBody(space, body);
cpSpaceAddShape(space, shape);
cpSpaceStep(space, dt);

Both libraries offer similar basic functionality, but Chipmunk2D provides more granular control over physics objects and their interactions. Box2d-lite has a simpler API, making it easier to get started with basic simulations, while Chipmunk2D offers more advanced features for complex physics scenarios.

14,257

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
  • Supports a wider range of platforms and programming languages
  • Actively maintained with frequent updates and improvements

Cons of Bullet3

  • Steeper learning curve due to its complexity
  • Higher computational overhead, potentially impacting performance in simpler scenarios
  • Larger codebase and more dependencies

Code Comparison

Box2D-Lite (C++):

b2World world(gravity);
b2Body* body = world.CreateBody(bodyDef);
body->CreateShape(shapeDef);
world.Step(timeStep);

Bullet3 (C++):

btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(...);
btRigidBody* body = new btRigidBody(constructionInfo);
dynamicsWorld->addRigidBody(body);
dynamicsWorld->stepSimulation(timeStep);

Summary

Box2D-Lite is a lightweight 2D physics engine, ideal for simple simulations and games. Bullet3 is a powerful 3D physics library suitable for complex simulations and professional applications. Box2D-Lite offers simplicity and ease of use, while Bullet3 provides more features and flexibility at the cost of increased complexity.

2,680

JavaScript 2D physics library

Pros of p2.js

  • Written in JavaScript, making it easier to integrate with web applications
  • Supports a wider range of constraints and collision shapes
  • More actively maintained with regular updates and contributions

Cons of p2.js

  • Generally slower performance compared to Box2D-lite
  • Larger file size and more complex codebase
  • May have a steeper learning curve for beginners

Code Comparison

Box2D-lite (C++):

b2World world(gravity);
b2Body* body = world.CreateBody(bodyDef);
body->CreateShape(shapeDef);
world.Step(timeStep);

p2.js (JavaScript):

var world = new p2.World({ gravity: [0, -9.82] });
var body = new p2.Body({ mass: 1 });
body.addShape(new p2.Circle({ radius: 1 }));
world.addBody(body);
world.step(timeStep);

Both libraries provide similar functionality for creating physics simulations, but p2.js offers a more JavaScript-friendly API. Box2D-lite's C++ implementation may be more efficient for performance-critical applications, while p2.js is better suited for web-based projects and rapid prototyping.

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

Box2D-Lite

Box2D-Lite is a small 2D physics engine. It was developed for the 2006 GDC Physics Tutorial. This is the original version of the larger Box2D library. The Lite version is more suitable for learning about game physics.

Building

  • Install CMake
  • Ensure CMake is in the user PATH
  • Visual Studio 2017: run build.bat
  • Otherwise: run build.sh from a bash shell
  • Results are in the build sub-folder

Build Status

Build Status