• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
Game development
  1. MDN
  2. Game development
  3. Tutorials
  4. 2D breakout game using Phaser
  5. Physics

Physics

In This Article
  1. Adding physics
  2. Removing our previous update instructions
  3. Final code check
  4. Fun with physics
  5. Compare your code
  6. Next steps

« PreviousNext »

This is the 5th step out of 16 of the Gamedev Phaser tutorial. You can find the source code as it should look after completing this lesson at Gamedev-Phaser-Content-Kit/demos/lesson05.html.

For proper collision detection between objects in our game we will need to have physics; this article introduces you to what's available in Phaser, as well as demonstrating a typical simple setup.

Adding physics

Phaser is bundled with three different physics engines — Arcade Physics, P2 and Ninja Physics — with a fourth option, Box2D, being available as a commercial plugin. For simple games like ours, we can use the Arcade Physics engine. We don't need any heavy geometry calculations — after all it's just a ball bouncing off walls and bricks.

First, let's initialize the Arcade Physics engine in our game. Add the physics.startSystem() method at the beginning of the create function (make it the first line inside the function), as shown below:

game.physics.startSystem(Phaser.Physics.ARCADE);

Next, we need to enable our ball for the physics system — Phaser object physics is not enabled by default. Add the following line at the bottom of the create() function:

game.physics.enable(ball, Phaser.Physics.ARCADE);

Next, if we want to move our ball on the screen, we can set velocity on its body. Add the following line, again at the bottom of create():

ball.body.velocity.set(150, 150);

Removing our previous update instructions

Remember to remove our old method of adding values to x and y from the update() function:

function update() {
    ball.x += 1;
    ball.y += 1;
}

we are now handling this properly, with a physics engine.

Final code check

The latest code should look like this:

var ball;
function preload() {
    game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
    game.scale.pageAlignHorizontally = true;
    game.scale.pageAlignVertically = true;
    game.stage.backgroundColor = '#eee';
    game.load.image('ball', 'img/ball.png');
}
function create() {
    game.physics.startSystem(Phaser.Physics.ARCADE);
    ball = game.add.sprite(50, 50, 'ball');
    game.physics.enable(ball, Phaser.Physics.ARCADE);
    ball.body.velocity.set(150, 150);
}
function update() {
}

Try reloading index.html again — The ball should now be moving constantly in the given direction. At the moment, the physics engine has gravity and friction set to zero. Adding gravity would result in the ball falling down while friction would eventually stop the ball.

Fun with physics

You can do much more with physics, for example by adding ball.body.gravity.y = 100; you will set the vertical gravity of the ball. As a result it will be launched upwards, but then fall due to the effects of gravity pulling it down.

This kind of functionality is just the tip of the iceberg — there are various functions and variables that can help you manipulate the physics objects. Check out the official physics documentation and see the huge collection of examples using the Arcade and P2 physics systems.

Compare your code

You can check the finished code for this lesson in the live demo below, and play with it to understand better how it works:

Next steps

Now we can move to the next lesson and see how to make the ball bounce off the walls.

« PreviousNext »

Document Tags and Contributors

Tags: 
  • 2D
  • Beginner
  • Canvas
  • Games
  • JavaScript
  • Phaser
  • physics
  • Tutorial
 Contributors to this page: chrisdavidmills, end3r
 Last updated by: chrisdavidmills, Mar 14, 2016, 3:43:24 AM
  1. Introduction
    1. Introduction to game development for the Web
    2. Anatomy of a video game
  2. APIs for game development
    1. Canvas
    2. CSS
    3. Full Screen
    4. Gamepad
    5. IndexedDB
    6. JavaScript
    7. Pointer Lock
    8. SVG
    9. Typed Arrays
    10. Web Audio
    11. WebGL
    12. WebRTC
    13. Web Sockets
    14. Web Workers
    15. XmlHttpRequest
  3. Tools
    1. asm.js
    2. Emscripten
    3. WebVR
    4. Gecko profiler
    5. Game engines and tools
  4. Techniques
    1. Using async scripts for asm.js
    2. Optimizing startup performance
    3. Using WebRTC peer-to-peer data channels
    4. Efficient animation for web games
    5. 3D games on the Web
      1. 3D games on the Web overview
      2. Explaining basic 3D theory
      3. Building up a basic demo with Three.js
      4. Building up a basic demo with PlayCanvas
      5. WebVR
    6. Audio for Web Games
    7. 2D collision detection
    8. 3D collision detection
      1. 3D collision detection overview
      2. Bounding volume collision detection with THREE.js
    9. Tiles and tilemaps
      1. Tiles and tilemaps overview
      2. Static maps
      3. Scrolling maps
  5. Workflows
    1. 2D breakout game using pure JavaScript
    2. 2D breakout game using Phaser
    3. 2D maze game with device orientation
  6. Publishing games
    1. Publishing games overview
    2. Game distribution
    3. Game promotion
    4. Game monetization
  7. Examples