Box2D in Love2D: Angry Birds Mechanics, Joints & Collisions

 75 min video

 3 min read

YouTube video ID: Wh5cDuJFHz8

Source: YouTube video by CS50Watch original video

PDF

The classic Angry Birds experience relies on realistic projectile motion and destructible structures. Early prototypes often used simple axis‑aligned bounding boxes (AABB) for collision detection, but that approach quickly reaches its limits when modeling complex, interconnected objects. Switching to the Box2D library brings a full rigid‑body physics engine into Love2D, allowing developers to simulate gravity, collisions, and joint constraints with far greater fidelity.

Box2D Fundamentals

Box2D centers every object around its own midpoint, unlike Love2D’s default top‑left coordinate system. All bodies live inside a World container, which advances the simulation each frame through world:update(dt). The library distinguishes three body types:

  • Static bodies remain fixed in space and never move.
  • Dynamic bodies are fully subject to forces, gravity, and collisions.
  • Kinematic bodies move under explicit velocity settings but ignore external forces and gravity.

Fixtures attach shapes to bodies and define physical attributes such as friction and restitution. By setting UserData on a fixture, developers can tag objects (e.g., “player”, “obstacle”) for later identification during collision callbacks.

Implementation Details

Mouse Input and Trajectory Prediction

When the player drags the mouse, the game records an initial impulse vector. To show where the bird will travel, the code simulates gravity over 90 frames, repeatedly applying the impulse to a temporary body. Each iteration updates velocity, computes a new position, and draws a point, producing a visual arc that guides the launch.

Collision Callbacks

Box2D provides four callbacks: BeginContact, EndContact, PreSolve, and PostSolve. The most critical for gameplay is BeginContact. Inside this callback the system reads the UserData of the two colliding fixtures. If the player's linear velocity magnitude exceeds 20, the obstacle’s fixture is flagged for removal.

Objects must not be destroyed directly inside callbacks. As one lecturer warned, “You're not supposed to do this in the callbacks because as the way it internally does physics calculations you want to do all the removals after all of the callbacks have resolved.” Instead, flagged objects are cleaned up in a post‑update phase, preventing memory or stack errors.

Sprite Rotation

Because Box2D works with center‑point coordinates, sprite drawing must account for rotation around the body’s center. The code calls body:getAngle() and renders the quad relative to its midpoint (e.g., (17.5, 17.5) for a 35 × 35 pixel sprite), ensuring the visual matches the physics orientation.

Advanced Physics: Joints

Joint types extend the range of possible structures:

  • Weld joints permanently fuse two bodies; breaking them enables crumbling walls.
  • Pulley joints connect bodies with a virtual rope, enforcing distance constraints that mimic real pulleys.
  • Revolute joints create pivot points for rotation, useful for pendulums or swinging platforms.
  • Rope joints act as flexible tethers, limiting movement within a defined radius.

These joints allow designers to craft intricate, destructible environments that react naturally to player actions.

Assignment Requirements

The course project builds on the lecture material:

  • Multi‑bird spawning lets players launch several projectiles in a single level.
  • Material‑based durability assigns hit points to objects—Glass breaks after 1 hit, Wood after 2, and Metal after 3.
  • Joint integration requires students to incorporate weld, pulley, revolute, or rope joints into their level designs, creating structures that can collapse or swing under impact.

By combining Box2D’s physics simulation, collision logic, and joint systems, students can recreate the satisfying, physics‑driven gameplay that made Angry Birds a landmark title.

  Takeaways

  • Box2D provides a World container where all bodies are updated each frame with world:update(dt), handling velocity, gravity, and collisions automatically.
  • Static, dynamic, and kinematic bodies behave differently—static bodies stay fixed, dynamic bodies respond to forces and gravity, while kinematic bodies move under user control without external forces.
  • Collision handling relies on four callbacks, with BeginContact checking fixture UserData and flagging objects for destruction when the player's velocity exceeds a threshold of 20.
  • Joint types such as weld, pulley, revolute, and rope enable complex structures, allowing parts to break, rotate, or stay within a flexible tether radius.
  • The assignment requires multi‑bird spawning, material‑based durability (glass 1 hit, wood 2 hits, metal 3 hits), and integration of joints to create destructible, physics‑driven levels.

Frequently Asked Questions

How does trajectory prediction work in the Love2D Box2D implementation?

Trajectory prediction simulates gravity over 90 frames by repeatedly applying the initial impulse vector to a temporary body. Each step updates velocity, computes a new position, and renders a point, producing a visual arc that shows where the bird will travel before launch.

Why must object removal be performed after all physics callbacks have resolved?

Box2D processes physics callbacks before finalizing the simulation step, so deleting a body inside BeginContact or other callbacks disrupts the internal data structures. Deferring removal to a separate cleanup phase after world:update(dt) avoids memory corruption and ensures the physics engine completes its calculations safely.

Who is CS50 on YouTube?

CS50 is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.

Does this page include the full transcript of the video?

Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.

Helpful resources related to this video

If you want to practice or explore the concepts discussed in the video, these commonly used tools may help.

Links may be affiliate links. We only include resources that are genuinely relevant to the topic.

PDF