Now that I have covered a great deal about phaser when it comes to the basics such as state objects, groups, making sprite sheets with canvas it is time to get into some use case examples in which I am using the physics engines that come with phaser.
In this post I am using phaser Community edition 2.11.1 of phaser. As such the code examples in the post might break in older or newer major release of phaser.
So if you are just starting out with using a physics engine in phaser ce A good start would be just a simple bounding ball example. So lets get this one out of the way. The Phaser.Physics.Arcade physics engine is running by default with phaser so there is no need to call physics.startSystem to start that engine which will be used in this example.
So this helper will create a ball sprite, and enable physics for the sprite which will make it so I now have a Sprite.body property that contains many THREE.Point values that can be used to set things like gravity, bounce, and drag. For this example I also make sure to set the body.collideWorldBounds boolean set to true so that the ball will bounce off the boundaries of the game world.
Here I have a simple helper that creates a sprite sheet for the ball using canvas.
Now it is time to get the example working by calling the helpers in the create method of a state object that I will be adding to a Phaser.game instance.
If all goes well the ball will drop down to the bottom of the game world, and then bounce back up almost to where it started, and then repeat until it stops bouncing completely. If I comment out the line that sets body.drag, the ball will keep bouncing indefinitely, and if I do the same with body.bounce the ball will just hit the bottom of the screen once and stop there.
Thank you for reading my post on getting started with physics in phaser ce. I will be wring more content on physics in phaser, and will likely update this post to link to more content of physics in phaser when it comes available.