Slicing, splitting and cutting HTML5 Box2D physics bodies using Phaser, Planck.js and PolyK

Back in 2011 I tried to build a script to slice, split and cut Box2D bodies using AS3, then two years ago I updated the script to work with Phaser and Matter.js physics.

Now that Box2D is living again thanks to Planck.js, it’s time to update the script to make it work with the latest libraries.

Like in my previous example, I am using the awesome PolyK library which allows us to work with polygons quite easily.

Let’s see the script in action:

Drag the mouse to draw the line and cut that box.

How does it work? You should know how to draw a line with the mouse, and it’s quite boring to explain so let’s jump straight to the point:

* Each time you draw a line, we know its start and end points

* We loop through all Box2D bodies and get their shape vertices. At this time, bodies are just polygons

* We check if the line splits in two the polygon thanks to PolyK’s Slice method.

* If the result of Slice method consists in two – or more – polygons, then the original polygon has been sliced!

* We remove the sliced Box2D body.

* We calculate the centroid of each polygon born from the sliced body

* We finally create the new Box2D bodies from the vertices of the resulting poligons.

Look at the source code, uncommented but quite easy to understand:

let game;
let gameOptions = {
    worldScale: 30
}
window.onload = function() {
    let gameConfig = {
        type: Phaser.AUTO,
        scale: {
            mode: Phaser.Scale.FIT,
            autoCenter: Phaser.Scale.CENTER_BOTH,
            parent: "thegame",
            width: 600,
            height: 600
        },
        scene: playGame
    }
    game = new Phaser.Game(gameConfig);
    window.focus();
}
class playGame extends Phaser.Scene {
    constructor() {
        super("PlayGame");
    }
    create() {
        let gravity = planck.Vec2(0, 3);
        this.world = planck.World(gravity);
        this.debugDraw = this.add.graphics();
        this.createBox(game.config.width / 2, game.config.height - 20, game.config.width, 40, false);
        this.createBox(game.config.width / 2, game.config.height / 2, game.config.width / 2, game.config.height / 2, true);
        this.lineGraphics = this.add.graphics();
        this.input.on("pointerdown", this.startDrawing, this);
        this.input.on("pointerup", this.stopDrawing, this);
        this.input.on("pointermove", this.keepDrawing, this);
        this.isDrawing = false;
    }
    startDrawing() {
        this.isDrawing = true;
    }
    keepDrawing(pointer) {
        if (this.isDrawing) {
            this.lineGraphics.clear();
            this.lineGraphics.lineStyle(1, 0x00ff00);
            this.lineGraphics.moveTo(pointer.downX, pointer.downY);
            this.lineGraphics.lineTo(pointer.x, pointer.y);
            this.lineGraphics.strokePath();
        }
    }
    stopDrawing(pointer) {
        this.lineGraphics.clear();
        this.isDrawing = false;
        let toBeSliced = [];
        let toBeCreated = [];
        for (let body = this.world.getBodyList(); body; body = body.getNext()) {
            let pointsArray = [];
            for (let fixture = body.getFixtureList(); fixture; fixture = fixture.getNext()) {
                let shape = fixture.getShape();
                let vertices = shape.m_vertices.length;
                for (let i = 0; i < vertices; i ++) {
                    let vertex = shape.getVertex(i);
                    let worldPosition = body.getWorldPoint(vertex);
                    pointsArray.push(worldPosition.x * gameOptions.worldScale, worldPosition.y * gameOptions.worldScale);
                }
            }
            let slicedPolygons = PolyK.Slice(pointsArray, pointer.downX, pointer.downY, pointer.upX, pointer.upY);
            if (slicedPolygons.length > 1) {
                toBeSliced.push(body);
                slicedPolygons.forEach (function(points) {
                    toBeCreated.push(points)
                })
            }
        }
        toBeSliced.forEach (function(body) {
            this.world.destroyBody(body)
        }.bind(this));
        toBeCreated.forEach(function(points) {
            let vertices = [];
            for (let i = 0; i < points.length / 2; i ++) {
                vertices.push(planck.Vec2(points[i * 2] / gameOptions.worldScale, points[i * 2 + 1] / gameOptions.worldScale))
            }
            let polygon = planck.Polygon(vertices);
            let centroid = polygon.m_centroid;
            let body = this.world.createBody();
            body.setDynamic();
            body.setPosition(planck.Vec2(centroid.x, centroid.y));
            for (let i = 0; i < vertices.length; i ++) {
                vertices[i].x -= centroid.x;
                vertices[i].y -= centroid.y;
            }
            body.createFixture(planck.Polygon(vertices));
            body.setMassData({
                mass: 1,
                center: planck.Vec2(),
                I: 1
            });
        }.bind(this))
    }

    createBox(posX, posY, width, height, isDynamic) {
        let box = this.world.createBody();
        if (isDynamic) {
            box.setDynamic();
        }
        box.createFixture(planck.Box(width / 2 / gameOptions.worldScale, height / 2 / gameOptions.worldScale));
        box.setPosition(planck.Vec2(posX / gameOptions.worldScale, posY / gameOptions.worldScale));
        box.setMassData({
            mass: 1,
            center: planck.Vec2(),
            I: 1
        });
        return box;
    }

    update(t, dt) {
        this.world.step(dt / 1000 * 2);
        this.world.clearForces();
        this.debugDraw.clear();
        this.debugDraw.lineStyle(2, 0xff00ff);
        this.debugDraw.fillStyle(0xff00ff, 0.1);
        for (let body = this.world.getBodyList(); body; body = body.getNext()) {
            this.debugDraw.beginPath();
            for (let fixture = body.getFixtureList(); fixture; fixture = fixture.getNext()) {
                let shape = fixture.getShape();
                let vertices = shape.m_vertices.length;
                for (let i = 0; i < vertices; i ++) {
                    let vertex = shape.getVertex(i);
                    let worldPosition = body.getWorldPoint(vertex);
                    if (i == 0) {
                        this.debugDraw.moveTo(worldPosition.x * gameOptions.worldScale, worldPosition.y * gameOptions.worldScale);
                    }
                    else {
                        this.debugDraw.lineTo(worldPosition.x * gameOptions.worldScale, worldPosition.y * gameOptions.worldScale);
                    }
                }
            }
            this.debugDraw.closePath();
            this.debugDraw.strokePath();
            this.debugDraw.fillPath();
        }
    }
}

A quite complex task has been accomplished in a few lines, let’s see if you manage to sort something interesting out of it. Download the source code.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers