Skip to content

Playcanvas API

Alex edited this page Aug 20, 2020 · 2 revisions

Playcanvas implementation

Installation

The Playcanvas implementation is compiled in IIFE mode, so all you need to do is simply download the contents of dist/pc-ballistics and place them anywhere in your project. The library will automatically start when your app starts, so you don't need to attach it anywhere.

Contents

The folder contains 2 files:

  • ballistics.min.js - this is a Playcanvas implementation. It is served as a glue between the core library and Playcanvas systems.
  • ballistics.wasm - this is a core library binary that does all the heavy lifting in calculating the trajectories.

Quick Start

First, wait for the library to load, then use it. When the library is ready, it will fire an event ballistics:ready. As such, you can use it to make sure the library is ready, before you start shooting your projectiles :) Here is an example snippet of how you can do that:

var Cannon = pc.createScript('Cannon');

Cannon.prototype.initialize = function () {
    // wait for the event from the library, or check the global space in case 
    // library was already loaded before this script
    if (!window.Ballistics)
        this.app.on('ballistics:ready', this.libReady, this);
    else
        this.libReady();
};

Cannon.prototype.libReady = function () {
    this.ready = true;
};

Cannon.prototype.fire = function () {
    if(!this.ready) return;
    
    // if we are here, then library is loaded and ready, we can use it
    var range = Ballistics.range(15, 0);
};

The code snippet used .range() method of the library to find the maximum range a projectile may travel given its speed and the starting height above ground. You can find the description of .range() and others in the menu on the right, under Playcanvas API.

You can also check out a sample project to see the examples of library use in Playcanvas.