2013-08-11 11:15:53 +00:00
|
|
|
/// <reference path="../_definitions.ts" />
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phaser - Physics - PhysicsManager
|
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser.Physics {
|
|
|
|
|
|
|
|
export class PhysicsManager {
|
|
|
|
|
|
|
|
constructor(game: Phaser.Game) {
|
|
|
|
|
|
|
|
this.game = game;
|
|
|
|
|
2013-08-12 03:08:15 +00:00
|
|
|
//this.gravity = new Vec2;
|
|
|
|
//this.drag = new Vec2;
|
|
|
|
//this.bounce = new Vec2;
|
|
|
|
//this.angularDrag = 0;
|
|
|
|
|
|
|
|
//this.bounds = new Rectangle(0, 0, width, height);
|
|
|
|
|
|
|
|
//this._distance = new Vec2;
|
|
|
|
//this._tangent = new Vec2;
|
|
|
|
|
2013-08-11 11:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public game: Phaser.Game;
|
|
|
|
|
2013-08-12 03:08:15 +00:00
|
|
|
// Temp calculation vars
|
2013-08-13 03:22:24 +00:00
|
|
|
private _drag: number;
|
|
|
|
private _delta: number;
|
|
|
|
private _velocityDelta: number;
|
|
|
|
private _length: number = 0;
|
|
|
|
private _distance: Vec2;
|
|
|
|
private _tangent: Vec2;
|
|
|
|
private _separatedX: bool;
|
|
|
|
private _separatedY: bool;
|
|
|
|
private _overlap: number;
|
|
|
|
private _maxOverlap: number;
|
|
|
|
private _obj1Velocity: number;
|
|
|
|
private _obj2Velocity: number;
|
|
|
|
private _obj1NewVelocity: number;
|
|
|
|
private _obj2NewVelocity: number;
|
|
|
|
private _average: number;
|
|
|
|
private _quadTree: QuadTree;
|
|
|
|
private _quadTreeResult: bool;
|
2013-08-12 03:08:15 +00:00
|
|
|
|
|
|
|
//public bounds: Rectangle;
|
|
|
|
|
|
|
|
//public gravity: Vec2;
|
|
|
|
//public drag: Vec2;
|
|
|
|
//public bounce: Vec2;
|
|
|
|
//public angularDrag: number;
|
|
|
|
|
2013-08-11 11:15:53 +00:00
|
|
|
grav: number = 0.2;
|
|
|
|
drag: number = 1;
|
|
|
|
bounce: number = 0.3;
|
|
|
|
friction: number = 0.05;
|
|
|
|
|
|
|
|
min_f: number = 0;
|
|
|
|
max_f: number = 1;
|
|
|
|
|
|
|
|
min_b: number = 0;
|
|
|
|
max_b: number = 1;
|
|
|
|
|
|
|
|
min_g: number = 0;
|
|
|
|
max_g = 1;
|
|
|
|
|
|
|
|
xmin: number = 0;
|
|
|
|
xmax: number = 800;
|
|
|
|
ymin: number = 0;
|
|
|
|
ymax: number = 600;
|
|
|
|
|
|
|
|
objrad: number = 24;
|
|
|
|
tilerad: number = 24 * 2;
|
|
|
|
objspeed: number = 0.2;
|
|
|
|
maxspeed: number = 20;
|
|
|
|
|
|
|
|
public update() {
|
2013-08-12 03:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public updateMotion(body: Phaser.Physics.Body) {
|
|
|
|
|
|
|
|
this._velocityDelta = (this.computeVelocity(body.angularVelocity, body.gravity.x, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity) / 2;
|
|
|
|
body.angularVelocity += this._velocityDelta;
|
2013-08-13 03:22:24 +00:00
|
|
|
body.sprite.transform.rotation += body.angularVelocity * this.game.time.physicsElapsed;
|
2013-08-12 03:08:15 +00:00
|
|
|
body.angularVelocity += this._velocityDelta;
|
|
|
|
|
|
|
|
this._velocityDelta = (this.computeVelocity(body.velocity.x, body.gravity.x, body.acceleration.x, body.drag.x) - body.velocity.x) / 2;
|
|
|
|
body.velocity.x += this._velocityDelta;
|
2013-08-13 03:22:24 +00:00
|
|
|
this._delta = body.velocity.x * this.game.time.physicsElapsed;
|
2013-08-12 03:08:15 +00:00
|
|
|
|
|
|
|
body.aabb.pos.x += this._delta;
|
2013-08-13 03:22:24 +00:00
|
|
|
body.deltaX = this._delta;
|
2013-08-12 03:08:15 +00:00
|
|
|
|
|
|
|
this._velocityDelta = (this.computeVelocity(body.velocity.y, body.gravity.y, body.acceleration.y, body.drag.y) - body.velocity.y) / 2;
|
|
|
|
body.velocity.y += this._velocityDelta;
|
2013-08-13 03:22:24 +00:00
|
|
|
this._delta = body.velocity.y * this.game.time.physicsElapsed;
|
2013-08-12 03:08:15 +00:00
|
|
|
|
|
|
|
body.aabb.pos.y += this._delta;
|
2013-08-13 03:22:24 +00:00
|
|
|
body.deltaY = this._delta;
|
|
|
|
|
|
|
|
//body.aabb.integrateVerlet();
|
2013-08-12 03:08:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
|
|
|
|
*
|
|
|
|
* @param {number} Velocity Any component of velocity (e.g. 20).
|
|
|
|
* @param {number} Acceleration Rate at which the velocity is changing.
|
|
|
|
* @param {number} Drag Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
|
|
|
|
* @param {number} Max An absolute value cap for the velocity.
|
|
|
|
*
|
|
|
|
* @return {number} The altered Velocity value.
|
|
|
|
*/
|
|
|
|
public computeVelocity(velocity: number, gravity: number = 0, acceleration: number = 0, drag: number = 0, max: number = 10000): number {
|
|
|
|
|
|
|
|
if (acceleration !== 0)
|
|
|
|
{
|
2013-08-13 03:22:24 +00:00
|
|
|
velocity += (acceleration + gravity) * this.game.time.physicsElapsed;
|
2013-08-12 03:08:15 +00:00
|
|
|
}
|
|
|
|
else if (drag !== 0)
|
|
|
|
{
|
2013-08-13 03:22:24 +00:00
|
|
|
this._drag = drag * this.game.time.physicsElapsed;
|
2013-08-12 03:08:15 +00:00
|
|
|
|
|
|
|
if (velocity - this._drag > 0)
|
|
|
|
{
|
|
|
|
velocity = velocity - this._drag;
|
|
|
|
}
|
|
|
|
else if (velocity + this._drag < 0)
|
|
|
|
{
|
|
|
|
velocity += this._drag;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
velocity = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 06:02:55 +00:00
|
|
|
//velocity += gravity;
|
2013-08-13 03:22:24 +00:00
|
|
|
|
|
|
|
if (velocity != 0)
|
2013-08-12 03:08:15 +00:00
|
|
|
{
|
|
|
|
if (velocity > max)
|
|
|
|
{
|
|
|
|
velocity = max;
|
|
|
|
}
|
|
|
|
else if (velocity < -max)
|
|
|
|
{
|
|
|
|
velocity = -max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return velocity;
|
2013-08-11 11:15:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|