Lots of Tilemap updates, moved the renderer out, added components and new tests.

This commit is contained in:
Richard Davey
2013-07-02 23:41:25 +01:00
parent c647792c12
commit c81cf0c882
34 changed files with 2395 additions and 1563 deletions
+63 -50
View File
@@ -35,7 +35,7 @@ module Phaser.Physics {
this.sprite = sprite;
this.game = sprite.game;
this.position = new Phaser.Vec2(Phaser.Physics.Manager.pixelsToMeters(sprite.x), Phaser.Physics.Manager.pixelsToMeters(sprite.y));
this.angle = sprite.rotation;
this.angle = this.game.math.degreesToRadians(sprite.rotation);
}
else
{
@@ -61,7 +61,6 @@ module Phaser.Physics {
this.bounds = new Bounds;
this.allowCollisions = Phaser.Types.ANY;
this.fixedRotation = false;
this.categoryBits = 0x0001;
this.maskBits = 0xFFFF;
@@ -82,11 +81,8 @@ module Phaser.Physics {
}
public toString(): string {
return "[{Body (name=" + this.name + " velocity=" + this.velocity.toString() + " angularVelocity: " + this.angularVelocity + ")}]";
}
private _tempVec2: Phaser.Vec2 = new Phaser.Vec2;
private _fixedRotation: bool = false;
/**
* Reference to Phaser.Game
@@ -118,8 +114,26 @@ module Phaser.Physics {
*/
public type: number;
/**
* The angle of the body in radians. Used by all of the internal physics methods.
*/
public angle: number;
/**
* The rotation of the body in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
*/
public get rotation(): number {
return this.game.math.radiansToDegrees(this.angle);
}
/**
* Set the rotation of the body in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
* The value is automatically wrapped to be between 0 and 360.
*/
public set rotation(value: number) {
this.angle = this.game.math.degreesToRadians(this.game.math.wrap(value, 360, 0));
}
// Local to world transform
public transform: Phaser.Transform;
@@ -174,17 +188,16 @@ module Phaser.Physics {
public inertia: number;
public inertiaInverted: number;
public fixedRotation = false;
public categoryBits = 0x0001;
public maskBits = 0xFFFF;
public stepCount = 0;
public categoryBits: number = 0x0001;
public maskBits: number = 0xFFFF;
public stepCount: number = 0;
public space: Space;
public duplicate() {
console.log('body duplicate called');
//var body = new Body(this.type, this.transform.t, this.angle);
//var body = new Body(this.type, this.transform.t, this.rotation);
//for (var i = 0; i < this.shapes.length; i++)
//{
@@ -313,28 +326,28 @@ module Phaser.Physics {
}
private setMass(mass) {
private setMass(mass:number) {
this.mass = mass;
this.massInverted = mass > 0 ? 1 / mass : 0;
}
private setInertia(inertia) {
private setInertia(inertia:number) {
this.inertia = inertia;
this.inertiaInverted = inertia > 0 ? 1 / inertia : 0;
}
public setTransform(pos, angle) {
public setTransform(pos:Phaser.Vec2, angle:number) {
// inject the transform into this.position
this.transform.setTo(pos, angle);
Manager.write('setTransform: ' + this.position.toString());
Manager.write('centroid: ' + this.centroid.toString());
//Manager.write('setTransform: ' + this.position.toString());
//Manager.write('centroid: ' + this.centroid.toString());
Phaser.TransformUtils.transform(this.transform, this.centroid, this.position);
Manager.write('post setTransform: ' + this.position.toString());
//Manager.write('post setTransform: ' + this.position.toString());
//this.position.copyFrom(this.transform.transform(this.centroid));
this.angle = angle;
@@ -342,17 +355,17 @@ module Phaser.Physics {
public syncTransform() {
Manager.write('syncTransform:');
Manager.write('p: ' + this.position.toString());
Manager.write('centroid: ' + this.centroid.toString());
Manager.write('xf: ' + this.transform.toString());
Manager.write('a: ' + this.angle);
//Manager.write('syncTransform:');
//Manager.write('p: ' + this.position.toString());
//Manager.write('centroid: ' + this.centroid.toString());
//Manager.write('xf: ' + this.transform.toString());
//Manager.write('a: ' + this.angle);
this.transform.setRotation(this.angle);
// OPTIMISE: Creating new vector
Phaser.Vec2Utils.subtract(this.position, Phaser.TransformUtils.rotate(this.transform, this.centroid), this.transform.t);
Manager.write('--------------------');
Manager.write('xf: ' + this.transform.toString());
Manager.write('--------------------');
//Manager.write('--------------------');
//Manager.write('xf: ' + this.transform.toString());
//Manager.write('--------------------');
}
@@ -376,9 +389,16 @@ module Phaser.Physics {
return Phaser.TransformUtils.unrotate(this.transform, v);
}
public setFixedRotation(flag) {
this.fixedRotation = flag;
public set fixedRotation(value:bool) {
this._fixedRotation = value;
this.resetMassData();
}
public get fixedRotation(): bool {
return this._fixedRotation;
}
public resetMassData() {
@@ -392,7 +412,6 @@ module Phaser.Physics {
if (this.isDynamic == false)
{
Phaser.TransformUtils.transform(this.transform, this.centroid, this.position);
//this.position.copyFrom(this.transform.transform(this.centroid));
return;
}
@@ -407,14 +426,11 @@ module Phaser.Physics {
var mass = shape.area() * shape.density;
var inertia = shape.inertia(mass);
//console.log('rmd', centroid, shape);
totalMassCentroid.multiplyAddByScalar(centroid, mass);
totalMass += mass;
totalInertia += inertia;
}
//this.centroid.copy(vec2.scale(totalMassCentroid, 1 / totalMass));
Phaser.Vec2Utils.scale(totalMassCentroid, 1 / totalMass, this.centroid);
this.setMass(totalMass);
@@ -455,9 +471,9 @@ module Phaser.Physics {
public cacheData(source:string = '') {
Manager.write('cacheData -- start');
Manager.write('p: ' + this.position.toString());
Manager.write('xf: ' + this.transform.toString());
//Manager.write('cacheData -- start');
//Manager.write('p: ' + this.position.toString());
//Manager.write('xf: ' + this.transform.toString());
this.bounds.clear();
@@ -468,11 +484,11 @@ module Phaser.Physics {
this.bounds.addBounds(shape.bounds);
}
Manager.write('bounds: ' + this.bounds.toString());
//Manager.write('bounds: ' + this.bounds.toString());
Manager.write('p: ' + this.position.toString());
Manager.write('xf: ' + this.transform.toString());
Manager.write('cacheData -- stop');
//Manager.write('p: ' + this.position.toString());
//Manager.write('xf: ' + this.transform.toString());
//Manager.write('cacheData -- stop');
}
@@ -538,15 +554,16 @@ module Phaser.Physics {
{
this.sprite.x = this.position.x * 50;
this.sprite.y = this.position.y * 50;
// Obey fixed rotation?
this.sprite.rotation = this.game.math.radiansToDegrees(this.angle);
this.sprite.transform.rotation = this.game.math.radiansToDegrees(this.angle);
}
}
public resetForce() {
this.force.setTo(0, 0);
this.torque = 0;
}
public applyForce(force:Phaser.Vec2, p:Phaser.Vec2) {
@@ -638,11 +655,8 @@ module Phaser.Physics {
}
public kineticEnergy() {
var vsq = this.velocity.dot(this.velocity);
var wsq = this.angularVelocity * this.angularVelocity;
return 0.5 * (this.mass * vsq + this.inertia * wsq);
return 0.5 * (this.mass * this.velocity.dot(this.velocity) + this.inertia * (this.angularVelocity * this.angularVelocity));
}
@@ -670,12 +684,7 @@ module Phaser.Physics {
public isCollidable(other:Body) {
if (this == other)
{
return false;
}
if (this.isDynamic == false && other.isDynamic == false)
if ((this.isDynamic == false && other.isDynamic == false) || this == other)
{
return false;
}
@@ -699,6 +708,10 @@ module Phaser.Physics {
}
public toString(): string {
return "[{Body (name=" + this.name + " velocity=" + this.velocity.toString() + " angularVelocity: " + this.angularVelocity + ")}]";
}
}
}
-13
View File
@@ -313,12 +313,8 @@ module Phaser.Physics {
var r2 = new Phaser.Vec2;
// Transformed r1, r2
Phaser.Vec2Utils.rotate(con.r1_local, body1.angle, r1);
//var r1 = vec2.rotate(con.r1_local, body1.a);
Phaser.Vec2Utils.rotate(con.r2_local, body2.angle, r2);
//var r2 = vec2.rotate(con.r2_local, body2.a);
Manager.write('r1_local.x = ' + con.r1_local.x + ' r1_local.y = ' + con.r1_local.y + ' angle: ' + body1.angle);
Manager.write('r1 rotated: r1.x = ' + r1.x + ' r1.y = ' + r1.y);
@@ -330,10 +326,7 @@ module Phaser.Physics {
var p2 = new Phaser.Vec2;
Phaser.Vec2Utils.add(body1.position, r1, p1);
//var p1 = vec2.add(body1.p, r1);
Phaser.Vec2Utils.add(body2.position, r2, p2);
//var p2 = vec2.add(body2.p, r2);
Manager.write('body1.pos.x=' + body1.position.x + ' y=' + body1.position.y);
Manager.write('body2.pos.x=' + body2.position.x + ' y=' + body2.position.y);
@@ -341,7 +334,6 @@ module Phaser.Physics {
// Corrected delta vector
var dp = new Phaser.Vec2;
Phaser.Vec2Utils.subtract(p2, p1, dp);
//var dp = vec2.sub(p2, p1);
// Position constraint
var c = Phaser.Vec2Utils.dot(dp, n) + con.depth;
@@ -368,16 +360,11 @@ module Phaser.Physics {
// Apply correction impulses
var impulse_dt = new Phaser.Vec2;
Phaser.Vec2Utils.scale(n, lambda_dt, impulse_dt);
//var impulse_dt = vec2.scale(n, lambda_dt);
body1.position.multiplyAddByScalar(impulse_dt, -m1_inv);
//body1.p.mad(impulse_dt, -m1_inv);
body1.angle -= sn1 * lambda_dt * i1_inv;
body2.position.multiplyAddByScalar(impulse_dt, m2_inv);
//body2.p.mad(impulse_dt, m2_inv);
body2.angle += sn2 * lambda_dt * i2_inv;
Manager.write('body1.pos.x=' + body1.position.x + ' y=' + body1.position.y);