@saintdoggie/entity-physics
v0.6.4
Published
Simple entity physics for game worlds
Downloads
2
Readme
Entity Physics
Simple entity physics simulation for game worlds. This project is still a work-in-progress.
This project forked from brunodb3/entity-physics
Usage
Install the package to your project:
$ npm install --save @saintdoggie/entity-physics
# or
$ yarn add @saintdoggie/entity-physics
Then add it to your game client/server:
import { Physics } from "@saintdoggie/entity-physics";
// Instantiate the Physics processor
const physics = new Physics();
// Set tick rate to be 60 fps
setTimeout(() => {
physics.tick();
}, 1000 / 60);
You can add entities to the physics processor and add forces to those entities. Their kinematics will be calculated at every tick (velocity, position, direction...):
import { Physics, Entity } from "@saintdoggie/entity-physics";
const physics = new Physics();
// All vectors are Victor from the Victor package.
// Entities start with `entity.kinematics.velocity = { x: 0, y: 0, z: 0 }`
const someEntity = new Entity("some-id");
// If you want an entity to be processed at every tick, you need to add it to
// the `entities` array in the physics processor
physics.entities.addEntity(someEntity);
// All forces are multiplied by the `deltaTime`, allowing for multiple framerates
// without compromising the actual processing of the entities
// (at 20fps the position will be the same as at 60fps)
someEntity.addForce({ x: 1, y: 0 });
The force calculations are as follows:
newForce = newForce * acceleration; // multiply by acceleration for smoother movement
velocity = velocity + newForce; // add the new force to the velocity
velocity = velocity * frictionMultiplier; // multiply the velocity by a friction multiplier, in order for the entity to stop when the force is 0
Entities can have different rates for acceleration, maximum/minimum velocity lengths amongst other options:
const entity = new Entity('some-id', {
runningMultiplier: 1.5, // when running, the force is multiplied by this value
minVelocity: 0.5,
maxVelocity: 9.5,
acceleration: 0.7,
frictionMultiplier: 0.9
...
});
For more information on how to use the library, see the .spec
files, as they provide working examples.