Body

Introduction

Arcade physics body.

  • Author: Richard Davey

Usage

Get physics body

  1. Enable physics world
  2. Add existing game object(s) to physics world
    • Add a game object
      var gameObject = scene.physics.add.existing(gameObject, isStatic);
      
      • isStatic :
        • 0 : Dynamic body
        • 1 : Static body
    • Add game objects
      scene.physics.world.enable(gameObjects, isStatic);
      
      • gameObjects : An array of game objects, or a group object
      • isStatic :
        • 0 : Dynamic body
        • 1 : Static body
  3. Get physics body
    var body = gameObject.body;
    

Enable

Whether this Body is updated by the physics simulation.

  • Enable (default)
    body.setEnable();
    
    or
    body.enable = true;
    
  • Disable
    body.setEnable(false);
    
    or
    body.enable = false;
    
  • Get
    var enable = body.moves;
    

Whether the Body's position and rotation are affected by its velocity, acceleration, drag, and gravity.

  • Enable (default)
    body.moves = true;
    
  • Disable
    body.moves = false;
    
  • Get
    var moves = body.moves;
    

Use case

Set body.moves to false when game object is controlled by tween or dragging.

Destroy

Physics body will be destroyed automatically when game object is destroyed.

Movement

Velocity

  • Set
    body.setVelocity(x,y);
    
    or
    body.setVelocityX(x);
    body.setVelocityY(x);
    
  • Get
    var vx = body.velocity.x;
    var vy = body.velocity.y;
    
Max speed
  • Set
    body.setMaxSpeed(speed);
    
  • Get
    var speed = body.maxSpeed;
    
Max velocity
  • Set
    body.setMaxVelocity(x, y);
    
  • Get
    var vx = body.maxVelocity.x;
    var vy = body.maxVelocity.y;
    

Acceleration

  • Set
    body.setAcceleration(x, y);
    
    or
    body.setAccelerationX(x);
    body.setAccelerationY(y);
    
  • Get
    var ax = body.acceleration.x;
    var ay = body.acceleration.y;
    
Gravity
  • Set
    body.setGravity(x, y);
    
    or
    body.setGravityX(x);
    body.setGravityY(y);
    
  • Get
    var gx = body.gravity.x;
    var gy = body.gravity.y;
    
  • Enables (default)
    body.setAllowGravity();
    
  • Disable
    body.setAllowGravity(false);
    

Drag

Reduces speed per second.

  • Set
    body.setDrag(x, y);
    
    or
    body.setDragX(x);
    body.setDragY(y);
    
  • Get
    var dx = body.drag.x;
    var dy = body.drag.y;
    
  • Enables (default)
    body.setAllowDrag();
    
  • Disable
    body.setAllowDrag(false);
    
  • Enable damping (default: false)
    body.useDamping = true;
    

Reset position

body.reset(x, y);

Stop

Sets acceleration, velocity, and speed to zero.

body.stop();

Immovable

  • Enable
    body.setImmovable();
    
  • Disable (defalut)
    body.setImmovable(false);
    
  • Get
    var immovable = body.immovable;
    
Friction

If this Body is immovable and in motion, this the proportion of this Body's movement received by the riding body on each axis.

  • Set
    body.setFriction(x, y);
    
    or
    body.setFrictionX(x);
    body.setFrictionY(y);
    
  • Get
    var fx = body.friction.x;
    var fy = body.friction.y;
    

Speed

  • The absolute (non-negative) change in this Body's horizontal/vertical position from the previous step.
    var dx = body.deltaAbsX();
    var dy = body.deltaAbsY();
    

Rotation

Allow rotation

Whether this Body's rotation is affected by its angular acceleration and velocity.

  • Enable (default)
    body.setAllowRotation();
    
  • Disable
    body.setAllowRotation(false);
    
  • Get
    var allowRotation = body.allowRotation;
    

Angular velocity

  • Set
    body.setAngularVelocity(v);
    
  • Get
    var av = body.angularVelocity;
    

Angular acceleration

  • Set
    body.setAngularAcceleration(v);
    
  • Get
    var aa = body.angularAcceleration;
    

Angular drag

Reduces angular speed per second.

  • Set
    body.setAngularDrag(v);
    
  • Get
    var ad = body.angularDrag;
    

Collision

Collision bound

  • Rectangle
    body.setSize(width, height, center);
    
    • center : false to set body's offset to (0, 0).
  • Circle
    body.setCircle(radius, offsetX, offsetY);
    
Offset
body.setOffset(x, y);

Push out

scene.physics.add.collider(objectsA, objectsB);
  • objectsA, objectsB :
    • A game object
    • Game objects in array (Add or remove game objects)
    • Physics group (Add or remove game objects)
    • Group (Add or remove game objects)

Callbacks

Add collider

Point inside

var hit = body.hitTest(x, y);

Is colliding

  • Is colliding this tick
    var isColliding = body.touching;
    
    • isColliding :
      {
          none: true,
          up: true,
          down: true,
          left: true,
          right: true
      }
      
  • Was colliding previous tick
    var wasColliding = body.wasTouching;
    
    • wasColliding :
      {
          none: true,
          up: true,
          down: true,
          left: true,
          right: true
      }
      

Bounce

  • Set
    body.setBounce(x, y);
    
    or
    body.setBounceX(x);
    body.setBounceY(y);
    
  • Get
    var bx = body.bounce.x;
    var by = body.bounce.y;
    

World bounds

  • Default world bounds
  • Custom world bounds :
    body.setBoundsRectangle(bounds);
    
  • Enable
    body.setCollideWorldBounds();
    
  • Disable (default)
    body.setCollideWorldBounds(false);
    
  • Get world bounds rectangle
    var top = body.world.bounds.top;
    var bottom = body.world.bounds.bottom;
    var left = body.world.bounds.left;
    var right = body.world.bounds.right;
    
Blocked

Whether this Body is colliding with a tile or the world boundary.

  • Blocked when moveing down
    var onFloor = body.onFloor(); // blocked.down
    
  • Blocked when moveing up
    var onCeiling = body.onCeiling();  // blocked.up
    
  • Blocked when moveing left or right
    var onWall = body.onWall();  // blocked.left || this.blocked.right
    
  • State
    var blocked = body.blocked;
    
    • blocked :
      {
          none: true,
          up: false,
          down: false,
          left: false,
          right: false
      }
      

Mass

  • Set
    body.setMass(m);
    
  • Get
    var m = body.mass;
    

Static body

Sync

Syncs the Bodies position and size with its parent Game Object.

body.updateFromGameObject();

Debug

  • Bounds of Body
    • Enable drawing body
      body.debugShowBody = true;
      
    • Color
      body.debugBodyColor = 0xff00ff;
      
  • Direction and magnitude of velocity
    • Enable drawing body
      body.debugShowVelocity = true;