HTML5 prototype of a planet gravity platform using Phaser 3 and Arcade physics

This is one of the times I create something so much weird I don’t even know how to name or describe it, but if you have already seen this somewhere, just let me know and I’ll give it a name.

Planet gravity is not a new concept, there is the Angry Birds Space tutorial series ready for you to have a look, but this time the concept is applied to a platformer, where the “planet” is a rectangle and the physics engine is Arcade Physics.

Have a look at the game:

Play with arrow keys, LEFT to move counterclockwise, RIGHT to move clockwise, UP to jump.

Try to reach the edge of the platform and see what happens.

The source code is uncommented and a bit redundant, but it’s quite easy to understand thanks to the massive use of switch statement.

Each side of the platform has its own physics settings, and the transition between sides is made with a tween:

let game;
let gameOptions = {
    gameGravity: 900,
    heroSpeed: 200,
    jumpForce: 250
}

const SIDE_UP = 0;
const SIDE_RIGHT = 1;
const SIDE_DOWN = 2;
const SIDE_LEFT = 3;

window.onload = function() {
    let gameConfig = {
        type: Phaser.AUTO,
        backgroundColor: 0x444444,
        scale: {
            mode: Phaser.Scale.FIT,
            autoCenter: Phaser.Scale.CENTER_BOTH,
            parent: "thegame",
            width: 800,
            height: 400
        },
        physics: {
            default: "arcade"
        },
       scene: playGame
    }
    game = new Phaser.Game(gameConfig);
}

class playGame extends Phaser.Scene{
    constructor(){
        super("PlayGame");
    }
    preload(){
        this.load.image("tile", "tile.png");
        this.load.image("hero", "hero.png");
    }
    create(){
        this.wall = this.physics.add.sprite(game.config.width / 2, game.config.height / 2, "tile");
        this.wall.displayWidth = game.config.width / 2;
        this.wall.displayHeight = game.config.height / 2;
        this.wall.setImmovable(true);
        this.hero = this.physics.add.sprite(game.config.width / 2, this.wall.getBounds().top - 100, "hero");
        this.hero.body.gravity.y = gameOptions.gameGravity;
        this.controls = this.input.keyboard.createCursorKeys();
        this.rotating = false;
        this.direction = SIDE_UP;
    }

    update(){
        this.physics.world.collide(this.hero, this.wall, null, null, this);
        if(!this.rotating){
            if(this.controls.left.isDown && !this.controls.right.isDown){
                this.moveCounterClockwise();
            }
            else {
                if(this.controls.right.isDown && !this.controls.left.isDown){
                    this.moveClockwise();
                }
                else{
                    this.stopMoving();
                }
            }
            if(this.controls.up.isDown){
                this.jump();
            }
            this.checkRotation();
        }
    }

    moveCounterClockwise(){
        this.hero.setFlipX(true);
        switch(this.direction){
            case SIDE_UP:
                this.hero.setVelocity(-gameOptions.heroSpeed, this.hero.body.velocity.y);
                break;
            case SIDE_DOWN:
                this.hero.setVelocity(gameOptions.heroSpeed, this.hero.body.velocity.y);
                break;
            case SIDE_LEFT:
                this.hero.setVelocity(this.hero.body.velocity.x, gameOptions.heroSpeed);
                break;
            case SIDE_RIGHT:
                this.hero.setVelocity(this.hero.body.velocity.x, -gameOptions.heroSpeed);
                break;
        }
    }

    moveClockwise(){
        this.hero.setFlipX(false);
        switch(this.direction){
            case SIDE_UP:
                this.hero.setVelocity(gameOptions.heroSpeed, this.hero.body.velocity.y);
                break;
            case SIDE_DOWN:
                this.hero.setVelocity(-gameOptions.heroSpeed, this.hero.body.velocity.y);
                break;
            case SIDE_LEFT:
                this.hero.setVelocity(this.hero.body.velocity.x, -gameOptions.heroSpeed);
                break;
            case SIDE_RIGHT:
                this.hero.setVelocity(this.hero.body.velocity.x, gameOptions.heroSpeed);
                break;
        }
    }

    stopMoving(){
        switch(this.direction){
            case SIDE_UP:
                this.hero.setVelocity(0, this.hero.body.velocity.y);
                break;
            case SIDE_DOWN:
                this.hero.setVelocity(0, this.hero.body.velocity.y);
                break;
            case SIDE_LEFT:
                this.hero.setVelocity(this.hero.body.velocity.x, 0);
                break;
            case SIDE_RIGHT:
                this.hero.setVelocity(this.hero.body.velocity.x, 0);
                break;
        }
    }

    checkRotation(){
        switch(this.direction){
            case SIDE_UP:
                if(this.hero.getBounds().left > this.wall.getBounds().right && !this.rotating){
                    this.handleRotation(1, this.wall.getBounds().right + this.hero.displayWidth / 2 + this.getHeight(), this.wall.getBounds().top + this.hero.displayHeight / 2);
                }
                if(this.hero.getBounds().right < this.wall.getBounds().left &amp;&amp; !this.rotating){
                    this.handleRotation(-1, this.wall.getBounds().left - this.hero.displayWidth / 2 - this.getHeight(), this.wall.getBounds().top + this.hero.displayHeight / 2);
                }
                break;
            case SIDE_RIGHT:
                if(this.hero.getBounds().top > this.wall.getBounds().bottom &amp;&amp; !this.rotating){
                    this.handleRotation(1, this.wall.getBounds().right - this.hero.displayWidth / 2, this.wall.getBounds().bottom + this.hero.displayHeight / 2 + this.getHeight());
                }
                if(this.hero.getBounds().bottom < this.wall.getBounds().top &amp;&amp; !this.rotating){
                    this.handleRotation(-1, this.wall.getBounds().right - this.hero.displayWidth / 2, this.wall.getBounds().top - this.hero.displayHeight / 2 - this.getHeight());
                }
                break;
            case SIDE_DOWN:
                if(this.hero.getBounds().right < this.wall.getBounds().left &amp;&amp; !this.rotating){
                    this.handleRotation(1, this.wall.getBounds().left - this.hero.displayWidth / 2 - this.getHeight(), this.wall.getBounds().bottom - this.hero.displayHeight / 2);
                }
                if(this.hero.getBounds().left > this.wall.getBounds().right &amp;&amp; !this.rotating){
                    this.handleRotation(-1, this.wall.getBounds().right + this.hero.displayWidth / 2 + this.getHeight(), this.wall.getBounds().bottom - this.hero.displayHeight / 2);
                }
                break;
            case SIDE_LEFT:
                if(this.hero.getBounds().bottom < this.wall.getBounds().top &amp;&amp; !this.rotating){
                    this.handleRotation(1, this.wall.getBounds().left + this.hero.displayWidth / 2, this.wall.getBounds().top - this.hero.displayHeight / 2 - this.getHeight());
                }
                if(this.hero.getBounds().top > this.wall.getBounds().bottom &amp;&amp; !this.rotating){
                    this.handleRotation(-1, this.wall.getBounds().left + this.hero.displayWidth / 2, this.wall.getBounds().bottom + this.hero.displayHeight / 2 + this.getHeight());
                }
                break;
        }
    }

    handleRotation(delta, targetX, targetY){
        this.hero.body.setAllowGravity(false);
        this.hero.setVelocity(0, 0)
        this.rotating = true;
        this.tweens.add({
            targets: [this.hero],
            angle: this.hero.angle + 90 * delta,
            x: targetX,
            y: targetY,
            duration: 200,
            callbackScope: this,
            onComplete: function(){
                this.rotating = false;
                this.hero.body.setAllowGravity(true);
                this.direction = Phaser.Math.Wrap(this.direction + delta, 0, 4);
                this.setGravity();
            }
        });
    }

    setGravity(){
        switch(this.direction){
            case SIDE_UP:
                this.hero.setGravity(0, gameOptions.gameGravity);
                break;
            case SIDE_DOWN:
                this.hero.setGravity(0, -gameOptions.gameGravity);
                break;
            case SIDE_LEFT:
                this.hero.setGravity(gameOptions.gameGravity, 0);
                break;
            case SIDE_RIGHT:
                this.hero.setGravity(-gameOptions.gameGravity, 0);
                break;
        }
    }

    jump(){
        switch(this.direction){
            case SIDE_UP:
                if(this.hero.body.touching.down){
                    this.hero.setVelocityY(-gameOptions.jumpForce);
                }
                break;
            case SIDE_DOWN:
                if(this.hero.body.touching.up){
                    this.hero.setVelocityY(gameOptions.jumpForce);
                }
                break;
            case SIDE_LEFT:
                if(this.hero.body.touching.right){
                    this.hero.setVelocityX(-gameOptions.jumpForce);
                }
                break;
            case SIDE_RIGHT:
                if(this.hero.body.touching.left){
                    this.hero.setVelocityX(gameOptions.jumpForce);
                }
                break;
        }
    }

    getHeight(){
        switch(this.direction){
            case SIDE_UP:
                return this.wall.getBounds().top - this.hero.getBounds().bottom;
            case SIDE_DOWN:
                return this.hero.getBounds().top - this.wall.getBounds().bottom;
            case SIDE_LEFT:
                return this.wall.getBounds().left - this.hero.getBounds().right;
            case SIDE_RIGHT:
                return this.hero.getBounds().left - this.wall.getBounds().right;
        }
    }
}

How would improve the game? Spikes to avoid and coins to collect? Or do you have some other ideas? Give me feedback, and meanwhile 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