Talking about Game development, HTML5, Javascript and Phaser.
Here I am with a pixel based physics engine. What is a pixel based physics engine? It’s basically a physics engine without physics: there is just a bouncing ball whose circumference is constantly scanned to check if overlaps some stuff on the screen. Then we use trigonometry to calcolate how should the ball bounce.
Due to the inefficient way to handle physics, the simulation is not accurate and I doubt you will be able to build something more interesting than a bouncing ball, but on the other hand you don’t need any physics engine as long as you have some way to check for pixels alpha.
Then years ago I was able to use this engine to create LineBall game, sponsored by both Coolbuddy and Armor Games, so why not giving it a try? Here is an example of what the engine is capable of:
Just look how the ball jumps on a randomly generated terrain.
It might not be the most realistic physics simulation, but it’s more than adequate for a quick game.
And the interesting thing is the engine itself is about 40 lines long, almost with no dependencies: I just needed to use getPixelAlpa
Phaser method to check for pixels alpha, but the whole engine, all inside update
method, is pure JavaScript.
Have a look at the source code:
let game
let gameOptions = {
ballRadius: 10,
gravity: 0.05,
maxSpeed: 10,
precision: 120,
friction: 0.9
}
window.onload = function() {
var gameConfig = {
type: Phaser.AUTO,
backgroundColor: 0xf5f5f5,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
parent: "thegame",
width: 640,
height: 480
},
scene: playGame
}
game = new Phaser.Game(gameConfig);
}
class playGame extends Phaser.Scene{
constructor(){
super("PlayGame");
}
create(){
this.ball = this.add.graphics();
this.ball.fillStyle(0xFf0000, 1);
this.ball.fillCircle(0, 0, gameOptions.ballRadius);
this.ball.x = game.config.width / 2;
this.ball.y = 0;
this.speed = new Phaser.Math.Vector2(0, 0);
let graphics = this.add.graphics();
for(let i = 0; i < 5; i++){
let angle = Phaser.Math.RND.rotation();
let originX = Phaser.Math.RND.integerInRange(100, game.config.width - 100);
let originY = Phaser.Math.RND.integerInRange(100, game.config.height - 100);
let width = Phaser.Math.RND.integerInRange(120, 240);
let triangle = Phaser.Geom.Triangle.BuildEquilateral(originX, originY, width);
Phaser.Geom.Triangle.Rotate(triangle, angle);
graphics.fillStyle(0x444444, 1);
graphics.beginPath();
graphics.moveTo(triangle.x1, triangle.y1);
graphics.lineTo(triangle.x2, triangle.y2);
graphics.lineTo(triangle.x3, triangle.y3);
graphics.closePath();
graphics.fillPath();
}
graphics.generateTexture("texture", game.config.width, game.config.height);
graphics.clear();
this.ground = this.add.sprite(game.config.width / 2, game.config.height / 2, "texture");
let timedEvent = this.time.addEvent({
delay: 10000,
callback: function(){
location.reload();
}
});
}
update(){
this.ball.x = Phaser.Math.Wrap(this.ball.x, 0, game.config.width);
this.ball.y = Phaser.Math.Wrap(this.ball.y, 0, game.config.height);
let collisions = 0;
let sumCollisionX = 0;
let sumCollisionY = 0;
this.ball.x += this.speed.x;
this.ball.y += this.speed.y;
this.speed.y += gameOptions.gravity;
this.speed.x = Phaser.Math.Clamp(this.speed.x, -gameOptions.maxSpeed, gameOptions.maxSpeed);
this.speed.y = Phaser.Math.Clamp(this.speed.y, -gameOptions.maxSpeed, gameOptions.maxSpeed)
for(let i = 1; i <= gameOptions.precision; i++){
let testX = this.ball.x + gameOptions.ballRadius * Math.sin(i * 2 * Math.PI / gameOptions.precision);
let testY = this.ball.y + gameOptions.ballRadius * Math.cos(i * 2 * Math.PI / gameOptions.precision);
let alpha = this.textures.getPixelAlpha(testX, testY, "texture");
if(alpha > 0){
collisions ++;
sumCollisionX += testX;
sumCollisionY += testY;
}
}
let averageX = sumCollisionX / collisions;
let averageY = sumCollisionY / collisions;
if(collisions > 0){
let collisionAngle = Math.atan2(this.ball.y - averageY, this.ball.x - averageX);
let collisionX = this.ball.x - gameOptions.ballRadius * Math.cos(collisionAngle);
let collisionY = this.ball.y - gameOptions.ballRadius * Math.sin(collisionAngle);
let deltaX = (collisionX - this.ball.x) / gameOptions.ballRadius;
let deltaY = (collisionY - this.ball.y) / gameOptions.ballRadius;
let speed = Math.sqrt(this.speed.x * this.speed.x + this.speed.y * this.speed.y) * gameOptions.friction;
let direction = Math.atan2(this.speed.y, this.speed.x);
let bounceAngle = 2 * collisionAngle - direction + Math.PI;
this.speed.x = Math.cos(bounceAngle) * speed;
this.speed.y = Math.sin(bounceAngle) * speed;
while(this.textures.getPixelAlpha(collisionX, collisionY, "texture")){
this.ball.x += 0.1 * Math.cos(bounceAngle);
this.ball.y += 0.1 * Math.sin(bounceAngle);
collisionX = this.ball.x - gameOptions.ballRadius * Math.cos(collisionAngle);
collisionY = this.ball.y - gameOptions.ballRadius * Math.sin(collisionAngle);
}
}
}
}
Do you find this useful? Or do you prefer more robust and complex engines for your games, even if they only feature a bouncing ball? Download the source code of the project and experiment with it.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.