mirror of
https://github.com/wassname/phaser.git
synced 2026-08-19 12:30:07 +08:00
After a mammoth debugging session we now have advanced physics colliding and responding accurately :)
This commit is contained in:
@@ -167,6 +167,10 @@
|
||||
<Content Include="math\Transform.js">
|
||||
<DependentUpon>Transform.ts</DependentUpon>
|
||||
</Content>
|
||||
<TypeScriptCompile Include="math\TransformUtils.ts" />
|
||||
<Content Include="math\TransformUtils.js">
|
||||
<DependentUpon>TransformUtils.ts</DependentUpon>
|
||||
</Content>
|
||||
<Content Include="math\Vec2.js">
|
||||
<DependentUpon>Vec2.ts</DependentUpon>
|
||||
</Content>
|
||||
@@ -195,6 +199,10 @@
|
||||
<Content Include="physics\advanced\ContactSolver.js">
|
||||
<DependentUpon>ContactSolver.ts</DependentUpon>
|
||||
</Content>
|
||||
<TypeScriptCompile Include="physics\advanced\IShape.ts" />
|
||||
<Content Include="physics\advanced\IShape.js">
|
||||
<DependentUpon>IShape.ts</DependentUpon>
|
||||
</Content>
|
||||
<Content Include="physics\advanced\Joint.js">
|
||||
<DependentUpon>Joint.ts</DependentUpon>
|
||||
</Content>
|
||||
|
||||
@@ -23,12 +23,8 @@ module Phaser {
|
||||
this.c = Math.cos(angle);
|
||||
this.s = Math.sin(angle);
|
||||
|
||||
this._tempVec = new Phaser.Vec2;
|
||||
|
||||
}
|
||||
|
||||
private _tempVec: Phaser.Vec2;
|
||||
|
||||
public t: Phaser.Vec2;
|
||||
public c: number;
|
||||
public s: number;
|
||||
@@ -68,28 +64,6 @@ module Phaser {
|
||||
|
||||
}
|
||||
|
||||
public rotate(v:Phaser.Vec2):Phaser.Vec2 {
|
||||
return this._tempVec.setTo(v.x * this.c - v.y * this.s, v.x * this.s + v.y * this.c);
|
||||
}
|
||||
|
||||
public unrotate(v:Phaser.Vec2):Phaser.Vec2 {
|
||||
return this._tempVec.setTo(v.x * this.c + v.y * this.s, -v.x * this.s + v.y * this.c);
|
||||
}
|
||||
|
||||
public transform(v:Phaser.Vec2):Phaser.Vec2 {
|
||||
return this._tempVec.setTo(v.x * this.c - v.y * this.s + this.t.x, v.x * this.s + v.y * this.c + this.t.y);
|
||||
}
|
||||
|
||||
public untransform(v:Phaser.Vec2):Phaser.Vec2 {
|
||||
|
||||
var px = v.x - this.t.x;
|
||||
var py = v.y - this.t.y;
|
||||
|
||||
// expensive - check for alternatives
|
||||
return this._tempVec.setTo(px * this.c + py * this.s, -px * this.s + py * this.c);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/// <reference path="../Game.ts" />
|
||||
/// <reference path="Vec2.ts" />
|
||||
/// <reference path="Transform.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - TransformUtils
|
||||
*
|
||||
* A collection of methods useful for manipulating and performing operations on 2D Transforms.
|
||||
*
|
||||
*/
|
||||
|
||||
module Phaser {
|
||||
|
||||
export class TransformUtils {
|
||||
|
||||
public static rotate(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 {
|
||||
return out.setTo(v.x * t.c - v.y * t.s, v.x * t.s + v.y * t.c);
|
||||
}
|
||||
|
||||
public static unrotate(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 {
|
||||
return out.setTo(v.x * t.c + v.y * t.s, -v.x * t.s + v.y * t.c);
|
||||
}
|
||||
|
||||
public static transform(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 {
|
||||
return out.setTo(v.x * t.c - v.y * t.s + t.t.x, v.x * t.s + v.y * t.c + t.t.y);
|
||||
}
|
||||
|
||||
public static untransform(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 {
|
||||
|
||||
var px = v.x - t.t.x;
|
||||
var py = v.y - t.t.y;
|
||||
|
||||
return out.setTo(px * t.c + py * t.s, -px * t.s + py * t.c);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/// <reference path="../Game.ts" />
|
||||
/// <reference path="../math/Vec2.ts" />
|
||||
/// <reference path="Vec2.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Vec2Utils
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
/// <reference path="../../geom/Point.ts" />
|
||||
/// <reference path="../../math/Vec2Utils.ts" />
|
||||
/// <reference path="../../math/Transform.ts" />
|
||||
/// <reference path="../../math/TransformUtils.ts" />
|
||||
/// <reference path="Manager.ts" />
|
||||
/// <reference path="Joint.ts" />
|
||||
/// <reference path="Bounds.ts" />
|
||||
/// <reference path="Space.ts" />
|
||||
/// <reference path="IShape.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Advanced Physics - Body
|
||||
@@ -128,14 +130,14 @@ module Phaser.Physics.Advanced {
|
||||
public awaked: bool;
|
||||
|
||||
// Shapes
|
||||
public shapes = [];
|
||||
public shapes: IShape[] = [];
|
||||
|
||||
// Joints
|
||||
public joints = [];
|
||||
public joints: IJoint[] = [];
|
||||
public jointHash = {};
|
||||
|
||||
// Bounds of all shapes
|
||||
public bounds;
|
||||
public bounds: Bounds;
|
||||
|
||||
public fixedRotation = false;
|
||||
public categoryBits = 0x0001;
|
||||
@@ -143,22 +145,22 @@ module Phaser.Physics.Advanced {
|
||||
public stepCount = 0;
|
||||
public space: Space;
|
||||
|
||||
/*
|
||||
public duplicate() {
|
||||
|
||||
var body = new Body(this.type, this.transform.t, this.angle);
|
||||
console.log('body duplicate called');
|
||||
|
||||
//var body = new Body(this.type, this.transform.t, this.angle);
|
||||
|
||||
for (var i = 0; i < this.shapes.length; i++)
|
||||
{
|
||||
body.addShape(this.shapes[i].duplicate());
|
||||
}
|
||||
//for (var i = 0; i < this.shapes.length; i++)
|
||||
//{
|
||||
// body.addShape(this.shapes[i].duplicate());
|
||||
//}
|
||||
|
||||
body.resetMassData();
|
||||
//body.resetMassData();
|
||||
|
||||
return body;
|
||||
//return body;
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
public get isDisabled(): bool {
|
||||
return this.type == Phaser.Types.BODY_DISABLED ? true : false;
|
||||
@@ -197,8 +199,11 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
// Check not already part of this body
|
||||
shape.body = this;
|
||||
|
||||
this.shapes.push(shape);
|
||||
|
||||
return shape;
|
||||
|
||||
}
|
||||
|
||||
public removeShape(shape) {
|
||||
@@ -234,8 +239,10 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
public setTransform(pos, angle) {
|
||||
|
||||
this.transform.setTo(pos, angle)
|
||||
this.position = this.transform.transform(this.centroid);
|
||||
this.transform.setTo(pos, angle);
|
||||
// inject the transform into this.position
|
||||
Phaser.TransformUtils.transform(this.transform, this.centroid, this.position);
|
||||
//this.position.copyFrom(this.transform.transform(this.centroid));
|
||||
this.angle = angle;
|
||||
|
||||
}
|
||||
@@ -243,26 +250,37 @@ module Phaser.Physics.Advanced {
|
||||
public syncTransform() {
|
||||
|
||||
this.transform.setRotation(this.angle);
|
||||
|
||||
//var rotc: Phaser.Vec2 = this.transform.rotate(this.centroid);
|
||||
//var sub: Phaser.Vec2 = Phaser.Vec2Utils.subtract(this.position, rotc);
|
||||
//this.transform.setPosition(sub);
|
||||
|
||||
// this.transform.setPosition(vec2.sub(this.position, this.transform.rotate(this.centroid)));
|
||||
Phaser.Vec2Utils.subtract(this.position, this.transform.rotate(this.centroid), this.transform.t);
|
||||
//Phaser.Vec2Utils.subtract(this.position, this.transform.rotate(this.centroid), this.transform.t);
|
||||
|
||||
// OPTIMISE: Creating new vector
|
||||
Phaser.Vec2Utils.subtract(this.position, Phaser.TransformUtils.rotate(this.transform, this.centroid), this.transform.t);
|
||||
|
||||
}
|
||||
|
||||
public getWorldPoint(p:Phaser.Vec2) {
|
||||
// This is returning a new vector - check it's actually used in that way
|
||||
return this.transform.transform(p)
|
||||
// OPTIMISE: Creating new vector
|
||||
return Phaser.TransformUtils.transform(this.transform, p);
|
||||
}
|
||||
|
||||
public getWorldVector(v) {
|
||||
return this.transform.rotate(v)
|
||||
// OPTIMISE: Creating new vector
|
||||
return Phaser.TransformUtils.rotate(this.transform, v);
|
||||
}
|
||||
|
||||
public getLocalPoint(p) {
|
||||
return this.transform.untransform(p)
|
||||
// OPTIMISE: Creating new vector
|
||||
return Phaser.TransformUtils.untransform(this.transform, p);
|
||||
}
|
||||
|
||||
public getLocalVector(v) {
|
||||
return this.transform.unrotate(v)
|
||||
// OPTIMISE: Creating new vector
|
||||
return Phaser.TransformUtils.unrotate(this.transform, v);
|
||||
}
|
||||
|
||||
public setFixedRotation(flag) {
|
||||
@@ -280,7 +298,8 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
if (this.isDynamic == false)
|
||||
{
|
||||
this.position.copyFrom(this.transform.transform(this.centroid));
|
||||
Phaser.TransformUtils.transform(this.transform, this.centroid, this.position);
|
||||
//this.position.copyFrom(this.transform.transform(this.centroid));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -317,7 +336,8 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
// Move center of mass
|
||||
var oldPosition: Phaser.Vec2 = Phaser.Vec2Utils.clone(this.position);
|
||||
this.position = this.transform.transform(this.centroid);
|
||||
//this.position.copyFrom(this.transform.transform(this.centroid));
|
||||
Phaser.TransformUtils.transform(this.transform, this.centroid, this.position);
|
||||
|
||||
// Update center of mass velocity
|
||||
|
||||
@@ -347,6 +367,9 @@ module Phaser.Physics.Advanced {
|
||||
}
|
||||
|
||||
public cacheData() {
|
||||
|
||||
console.log('Body cacheData', this.name, 'len', this.shapes.length);
|
||||
|
||||
this.bounds.clear();
|
||||
|
||||
for (var i = 0; i < this.shapes.length; i++)
|
||||
@@ -385,9 +408,15 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
public updatePosition(dt) {
|
||||
|
||||
//console.log('body update pos', this.position.y);
|
||||
//console.log('pre add temp', this._tempVec2.y);
|
||||
|
||||
//this.position.addself(vec2.scale(this.velocity, dt));
|
||||
this.position.add(Phaser.Vec2Utils.scale(this.velocity, dt, this._tempVec2));
|
||||
|
||||
//console.log('post add temp', this._tempVec2.y);
|
||||
//console.log('post add', this.position.y);
|
||||
|
||||
this.angle += this.angularVelocity * dt;
|
||||
|
||||
}
|
||||
|
||||
@@ -47,21 +47,28 @@ module Phaser.Physics.Advanced {
|
||||
return ["mins:", this.mins.toString(), "maxs:", this.maxs.toString()].join(" ");
|
||||
}
|
||||
|
||||
public setTo(mins, maxs) {
|
||||
public setTo(mins: Phaser.Vec2, maxs: Phaser.Vec2) {
|
||||
|
||||
this.mins.setTo(mins.x, mins.y);
|
||||
this.maxs.setTo(maxs.x, maxs.y);
|
||||
|
||||
}
|
||||
|
||||
public copy(b:Bounds) {
|
||||
public copy(b: Bounds): Bounds {
|
||||
|
||||
this.mins.copyFrom(b.mins);
|
||||
this.maxs.copyFrom(b.maxs);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public clear() {
|
||||
public clear(): Bounds {
|
||||
|
||||
this.mins.setTo(999999, 999999);
|
||||
this.maxs.setTo(-999999, -999999);
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
public isEmpty(): bool {
|
||||
@@ -78,23 +85,27 @@ module Phaser.Physics.Advanced {
|
||||
}
|
||||
*/
|
||||
|
||||
public getPerimeter() {
|
||||
public getPerimeter(): number {
|
||||
return (this.maxs.x - this.mins.x + this.maxs.y - this.mins.y) * 2;
|
||||
}
|
||||
|
||||
public addPoint(p) {
|
||||
public addPoint(p: Phaser.Vec2): Bounds {
|
||||
|
||||
if (this.mins.x > p.x) this.mins.x = p.x;
|
||||
if (this.maxs.x < p.x) this.maxs.x = p.x;
|
||||
if (this.mins.y > p.y) this.mins.y = p.y;
|
||||
if (this.maxs.y < p.y) this.maxs.y = p.y;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public addBounds(b) {
|
||||
public addBounds(b: Bounds): Bounds {
|
||||
|
||||
if (this.mins.x > b.mins.x) this.mins.x = b.mins.x;
|
||||
if (this.maxs.x < b.maxs.x) this.maxs.x = b.maxs.x;
|
||||
if (this.mins.y > b.mins.y) this.mins.y = b.mins.y;
|
||||
if (this.maxs.y < b.maxs.y) this.maxs.y = b.maxs.y;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -106,23 +117,29 @@ module Phaser.Physics.Advanced {
|
||||
return this;
|
||||
}
|
||||
|
||||
public addExtents(center, extent_x, extent_y) {
|
||||
public addExtents(center: Phaser.Vec2, extent_x: number, extent_y: number): Bounds {
|
||||
|
||||
if (this.mins.x > center.x - extent_x) this.mins.x = center.x - extent_x;
|
||||
if (this.maxs.x < center.x + extent_x) this.maxs.x = center.x + extent_x;
|
||||
if (this.mins.y > center.y - extent_y) this.mins.y = center.y - extent_y;
|
||||
if (this.maxs.y < center.y + extent_y) this.maxs.y = center.y + extent_y;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
public expand(ax, ay) {
|
||||
public expand(ax: number, ay: number): Bounds {
|
||||
|
||||
this.mins.x -= ax;
|
||||
this.mins.y -= ay;
|
||||
this.maxs.x += ax;
|
||||
this.maxs.y += ay;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
public containPoint(p) {
|
||||
public containPoint(p: Phaser.Vec2): bool {
|
||||
|
||||
if (p.x < this.mins.x || p.x > this.maxs.x || p.y < this.mins.y || p.y > this.maxs.y)
|
||||
{
|
||||
@@ -133,20 +150,26 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
}
|
||||
|
||||
public intersectsBounds(b) {
|
||||
|
||||
public intersectsBounds(b: Bounds): bool {
|
||||
|
||||
if (this.mins.x > b.maxs.x || this.maxs.x < b.mins.x || this.mins.y > b.maxs.y || this.maxs.y < b.mins.y)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log('intersects TRUE');
|
||||
console.log(this);
|
||||
console.log(b);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static expand(b, ax, ay) {
|
||||
public static expand(b: Bounds, ax, ay) {
|
||||
|
||||
var b = new Bounds(b.mins, b.maxs);
|
||||
b.expand(ax, ay);
|
||||
return b;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
public collide(a, b, contacts: Contact[]) {
|
||||
|
||||
console.log('collide', a.type, b.type);
|
||||
|
||||
// Circle (a is the circle)
|
||||
if (a.type == Manager.SHAPE_TYPE_CIRCLE)
|
||||
{
|
||||
|
||||
@@ -24,6 +24,11 @@ module Phaser.Physics.Advanced {
|
||||
this.lambdaNormal = 0;
|
||||
this.lambdaTangential = 0;
|
||||
|
||||
this.r1 = new Phaser.Vec2;
|
||||
this.r2 = new Phaser.Vec2;
|
||||
this.r1_local = new Phaser.Vec2;
|
||||
this.r2_local = new Phaser.Vec2;
|
||||
|
||||
}
|
||||
|
||||
public hash;
|
||||
@@ -31,8 +36,8 @@ module Phaser.Physics.Advanced {
|
||||
// Linear velocities at contact point
|
||||
public r1: Phaser.Vec2;
|
||||
public r2: Phaser.Vec2;
|
||||
public r1_local;
|
||||
public r2_local;
|
||||
public r1_local: Phaser.Vec2;
|
||||
public r2_local: Phaser.Vec2;
|
||||
// Bounce velocity
|
||||
public bounce;
|
||||
public emn;
|
||||
|
||||
@@ -38,6 +38,8 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
constructor(shape1, shape2) {
|
||||
|
||||
console.log('ContactSolver super');
|
||||
|
||||
this.shape1 = shape1;
|
||||
this.shape2 = shape2;
|
||||
|
||||
@@ -95,7 +97,10 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
for (var i = 0; i < this.contacts.length; i++)
|
||||
{
|
||||
var con = this.contacts[i];
|
||||
var con: Contact = this.contacts[i];
|
||||
|
||||
console.log('initSolver con');
|
||||
console.log(con);
|
||||
|
||||
// Transformed r1, r2
|
||||
Phaser.Vec2Utils.subtract(con.point, body1.position, con.r1);
|
||||
@@ -104,8 +109,10 @@ module Phaser.Physics.Advanced {
|
||||
//con.r2 = vec2.sub(con.point, body2.p);
|
||||
|
||||
// Local r1, r2
|
||||
con.r1_local = body1.transform.unrotate(con.r1);
|
||||
con.r2_local = body2.transform.unrotate(con.r2);
|
||||
Phaser.TransformUtils.unrotate(body1.transform, con.r1, con.r1_local);
|
||||
Phaser.TransformUtils.unrotate(body2.transform, con.r2, con.r2_local);
|
||||
//con.r1_local = body1.transform.unrotate(con.r1);
|
||||
//con.r2_local = body2.transform.unrotate(con.r2);
|
||||
|
||||
var n = con.normal;
|
||||
var t = Phaser.Vec2Utils.perp(con.normal);
|
||||
@@ -142,6 +149,10 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
// bounce velocity dot n
|
||||
con.bounce = Phaser.Vec2Utils.dot(rv, con.normal) * this.elasticity;
|
||||
console.log('bounce?', Phaser.Vec2Utils.dot(rv, con.normal), this.elasticity);
|
||||
|
||||
console.log('con over');
|
||||
console.log(con);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/// <reference path="../../math/Vec2.ts" />
|
||||
/// <reference path="../../geom/Point.ts" />
|
||||
/// <reference path="../../math/Vec2Utils.ts" />
|
||||
/// <reference path="Manager.ts" />
|
||||
/// <reference path="Body.ts" />
|
||||
/// <reference path="Shape.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Advanced Physics - IShape
|
||||
*
|
||||
* Based on the work Ju Hyung Lee started in JS PhyRus.
|
||||
*/
|
||||
|
||||
module Phaser.Physics.Advanced {
|
||||
|
||||
export interface IShape {
|
||||
|
||||
id: number;
|
||||
type: number;
|
||||
|
||||
elasticity: number;
|
||||
friction: number;
|
||||
density: number;
|
||||
|
||||
body: Body;
|
||||
bounds: Bounds;
|
||||
|
||||
area(): number;
|
||||
centroid(): Phaser.Vec2;
|
||||
inertia(mass: number): number;
|
||||
cacheData(xf:Transform);
|
||||
pointQuery(p: Phaser.Vec2): bool;
|
||||
findEdgeByPoint(p: Phaser.Vec2, minDist: number): number;
|
||||
findVertexByPoint(p: Phaser.Vec2, minDist: number): number;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ module Phaser.Physics.Advanced {
|
||||
public static shapeCounter: number = 0;
|
||||
|
||||
public space: Space;
|
||||
public lastTime: number = 0;
|
||||
public lastTime: number = Date.now();
|
||||
public frameRateHz: number = 60;
|
||||
public timeDelta: number = 0;
|
||||
public paused: bool = false;
|
||||
@@ -146,7 +146,13 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
|
||||
|
||||
public pixelsToMeters(value: number): number {
|
||||
return value * 0.02;
|
||||
}
|
||||
|
||||
public metersToPixels(value: number): number {
|
||||
return value * 50;
|
||||
}
|
||||
|
||||
public static pixelsToMeters(value: number): number {
|
||||
return value * 0.02;
|
||||
@@ -164,23 +170,23 @@ module Phaser.Physics.Advanced {
|
||||
return value * 50;
|
||||
}
|
||||
|
||||
public static areaForCircle(radius_outer, radius_inner) {
|
||||
public static areaForCircle(radius_outer, radius_inner): number {
|
||||
return Math.PI * (radius_outer * radius_outer - radius_inner * radius_inner);
|
||||
}
|
||||
|
||||
public static inertiaForCircle(mass, center, radius_outer, radius_inner) {
|
||||
public static inertiaForCircle(mass, center, radius_outer, radius_inner): number {
|
||||
return mass * ((radius_outer * radius_outer + radius_inner * radius_inner) * 0.5 + center.lengthSq());
|
||||
}
|
||||
|
||||
public static areaForSegment(a, b, radius) {
|
||||
public static areaForSegment(a, b, radius): number {
|
||||
return radius * (Math.PI * radius + 2 * Phaser.Vec2Utils.distance(a, b));
|
||||
}
|
||||
|
||||
public static centroidForSegment(a, b) {
|
||||
public static centroidForSegment(a, b): Phaser.Vec2 {
|
||||
return Phaser.Vec2Utils.scale(Phaser.Vec2Utils.add(a, b), 0.5);
|
||||
}
|
||||
|
||||
public static inertiaForSegment(mass, a, b) {
|
||||
public static inertiaForSegment(mass, a, b): number {
|
||||
|
||||
var distsq = Phaser.Vec2Utils.distanceSq(b, a);
|
||||
var offset: Phaser.Vec2 = Phaser.Vec2Utils.scale(Phaser.Vec2Utils.add(a, b), 0.5);
|
||||
@@ -188,7 +194,7 @@ module Phaser.Physics.Advanced {
|
||||
return mass * (distsq / 12 + offset.lengthSq());
|
||||
}
|
||||
|
||||
public static areaForPoly(verts) {
|
||||
public static areaForPoly(verts): number {
|
||||
|
||||
var area = 0;
|
||||
|
||||
@@ -200,7 +206,7 @@ module Phaser.Physics.Advanced {
|
||||
return area / 2;
|
||||
}
|
||||
|
||||
public static centroidForPoly(verts) {
|
||||
public static centroidForPoly(verts): Phaser.Vec2 {
|
||||
|
||||
var area = 0;
|
||||
var vsum = new Phaser.Vec2;
|
||||
@@ -220,7 +226,7 @@ module Phaser.Physics.Advanced {
|
||||
return Phaser.Vec2Utils.scale(vsum, 1 / (3 * area));
|
||||
}
|
||||
|
||||
public static inertiaForPoly(mass, verts, offset) {
|
||||
public static inertiaForPoly(mass, verts, offset): number {
|
||||
|
||||
var sum1 = 0;
|
||||
var sum2 = 0;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
/// <reference path="Manager.ts" />
|
||||
/// <reference path="Body.ts" />
|
||||
/// <reference path="Bounds.ts" />
|
||||
/// <reference path="IShape.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Advanced Physics - Shape
|
||||
@@ -30,6 +31,7 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
public id: number;
|
||||
public type: number;
|
||||
public body: Body;
|
||||
|
||||
// Coefficient of restitution (elasticity)
|
||||
public elasticity: number;
|
||||
@@ -43,6 +45,11 @@ module Phaser.Physics.Advanced {
|
||||
// Axis-aligned bounding box
|
||||
public bounds: Bounds;
|
||||
|
||||
// Over-ridden by ShapePoly
|
||||
public findEdgeByPoint(p: Phaser.Vec2, minDist: number): number {
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,8 +16,16 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
export class ShapeBox extends Phaser.Physics.Advanced.ShapePoly {
|
||||
|
||||
// Give in pixels
|
||||
constructor(x, y, width, height) {
|
||||
|
||||
console.log('creating box', x, y, width, height);
|
||||
|
||||
x = Manager.pixelsToMeters(x);
|
||||
y = Manager.pixelsToMeters(y);
|
||||
width = Manager.pixelsToMeters(width);
|
||||
height = Manager.pixelsToMeters(height);
|
||||
|
||||
var hw = width * 0.5;
|
||||
var hh = height * 0.5;
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
module Phaser.Physics.Advanced {
|
||||
|
||||
export class ShapeCircle extends Phaser.Physics.Advanced.Shape {
|
||||
export class ShapeCircle extends Phaser.Physics.Advanced.Shape implements IShape {
|
||||
|
||||
constructor(radius: number, x?: number = 0, y?: number = 0) {
|
||||
|
||||
@@ -39,43 +39,49 @@ module Phaser.Physics.Advanced {
|
||||
return new ShapeCircle(this.center.x, this.center.y, this.radius);
|
||||
}
|
||||
|
||||
public recenter(c) {
|
||||
public recenter(c:Phaser.Vec2) {
|
||||
this.center.subtract(c);
|
||||
}
|
||||
|
||||
public transform(xf) {
|
||||
this.center = xf.transform(this.center);
|
||||
public transform(xf: Transform) {
|
||||
|
||||
Phaser.TransformUtils.transform(xf, this.center, this.center);
|
||||
//this.center = xf.transform(this.center);
|
||||
}
|
||||
|
||||
public untransform(xf) {
|
||||
this.center = xf.untransform(this.center);
|
||||
public untransform(xf: Transform) {
|
||||
Phaser.TransformUtils.untransform(xf, this.center, this.center);
|
||||
//this.center = xf.untransform(this.center);
|
||||
}
|
||||
|
||||
public area() {
|
||||
public area(): number {
|
||||
return Manager.areaForCircle(this.radius, 0);
|
||||
}
|
||||
|
||||
public centroid() {
|
||||
public centroid(): Phaser.Vec2 {
|
||||
return Phaser.Vec2Utils.clone(this.center);
|
||||
}
|
||||
|
||||
public inertia(mass) {
|
||||
public inertia(mass: number): number {
|
||||
return Manager.inertiaForCircle(mass, this.center, this.radius, 0);
|
||||
}
|
||||
|
||||
public cacheData(xf: Transform) {
|
||||
|
||||
Phaser.TransformUtils.transform(xf, this.center, this.tc);
|
||||
//this.tc = xf.transform(this.center);
|
||||
|
||||
public cacheData(xf) {
|
||||
this.tc = xf.transform(this.center);
|
||||
this.bounds.mins.setTo(this.tc.x - this.radius, this.tc.y - this.radius);
|
||||
this.bounds.maxs.setTo(this.tc.x + this.radius, this.tc.y + this.radius);
|
||||
|
||||
}
|
||||
|
||||
public pointQuery(p) {
|
||||
public pointQuery(p:Phaser.Vec2): bool {
|
||||
//return vec2.distsq(this.tc, p) < (this.r * this.r);
|
||||
return Phaser.Vec2Utils.distanceSq(this.tc, p) < (this.radius * this.radius);
|
||||
}
|
||||
|
||||
public findVertexByPoint(p, minDist) {
|
||||
public findVertexByPoint(p:Phaser.Vec2, minDist: number): number {
|
||||
|
||||
var dsq = minDist * minDist;
|
||||
|
||||
|
||||
@@ -13,10 +13,12 @@
|
||||
|
||||
module Phaser.Physics.Advanced {
|
||||
|
||||
export class ShapePoly extends Phaser.Physics.Advanced.Shape {
|
||||
export class ShapePoly extends Phaser.Physics.Advanced.Shape implements IShape {
|
||||
|
||||
constructor(verts?:Phaser.Vec2[]) {
|
||||
|
||||
console.log('ShapePoly created', verts);
|
||||
|
||||
super(Manager.SHAPE_TYPE_POLY);
|
||||
|
||||
this.verts = [];
|
||||
@@ -29,7 +31,8 @@ module Phaser.Physics.Advanced {
|
||||
{
|
||||
for (var i = 0; i < verts.length; i++)
|
||||
{
|
||||
Phaser.Vec2Utils.clone(verts[i], this.verts[i]);
|
||||
console.log('cloning vert', i);
|
||||
this.verts[i] = Phaser.Vec2Utils.clone(verts[i]);
|
||||
this.tverts[i] = this.verts[i];
|
||||
|
||||
this.tplanes[i] = {};
|
||||
@@ -38,8 +41,12 @@ module Phaser.Physics.Advanced {
|
||||
}
|
||||
}
|
||||
|
||||
console.log('ShapePoly finished', this.verts);
|
||||
|
||||
this.finishVerts();
|
||||
|
||||
console.log('ShapePoly finished 2', this.verts);
|
||||
|
||||
}
|
||||
|
||||
public verts: Phaser.Vec2[];
|
||||
@@ -121,24 +128,26 @@ module Phaser.Physics.Advanced {
|
||||
}
|
||||
}
|
||||
|
||||
public area() {
|
||||
public area(): number {
|
||||
return Manager.areaForPoly(this.verts);
|
||||
}
|
||||
|
||||
public centroid() {
|
||||
public centroid(): Phaser.Vec2 {
|
||||
return Manager.centroidForPoly(this.verts);
|
||||
}
|
||||
|
||||
public inertia(mass) {
|
||||
public inertia(mass: number): number {
|
||||
return Manager.inertiaForPoly(mass, this.verts, new Phaser.Vec2);
|
||||
}
|
||||
|
||||
public cacheData(xf) {
|
||||
public cacheData(xf:Transform) {
|
||||
|
||||
this.bounds.clear();
|
||||
|
||||
var numVerts = this.verts.length;
|
||||
|
||||
console.log('shapePoly cacheData', numVerts);
|
||||
|
||||
if (numVerts == 0)
|
||||
{
|
||||
return;
|
||||
@@ -146,7 +155,8 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
for (var i = 0; i < numVerts; i++)
|
||||
{
|
||||
this.tverts[i] = xf.transform(this.verts[i]);
|
||||
Phaser.TransformUtils.transform(xf, this.tverts[i], this.tverts[i]);
|
||||
//this.tverts[i] = xf.transform(this.verts[i]);
|
||||
}
|
||||
|
||||
if (numVerts < 2)
|
||||
@@ -170,7 +180,7 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
}
|
||||
|
||||
public pointQuery(p) {
|
||||
public pointQuery(p: Phaser.Vec2): bool {
|
||||
|
||||
if (!this.bounds.containPoint(p))
|
||||
{
|
||||
@@ -180,7 +190,7 @@ module Phaser.Physics.Advanced {
|
||||
return this.containPoint(p);
|
||||
}
|
||||
|
||||
public findVertexByPoint(p, minDist) {
|
||||
public findVertexByPoint(p:Phaser.Vec2, minDist: number): number {
|
||||
|
||||
var dsq = minDist * minDist;
|
||||
|
||||
@@ -195,7 +205,7 @@ module Phaser.Physics.Advanced {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public findEdgeByPoint(p, minDist) {
|
||||
public findEdgeByPoint(p: Phaser.Vec2, minDist: number): number {
|
||||
|
||||
var dsq = minDist * minDist;
|
||||
var numVerts = this.tverts.length;
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
module Phaser.Physics.Advanced {
|
||||
|
||||
export class ShapeSegment extends Phaser.Physics.Advanced.Shape {
|
||||
export class ShapeSegment extends Phaser.Physics.Advanced.Shape implements IShape {
|
||||
|
||||
constructor(a, b, radius: number) {
|
||||
|
||||
@@ -62,32 +62,46 @@ module Phaser.Physics.Advanced {
|
||||
this.b.subtract(c);
|
||||
}
|
||||
|
||||
public transform(xf) {
|
||||
this.a = xf.transform(this.a);
|
||||
this.b = xf.transform(this.b);
|
||||
public transform(xf:Transform) {
|
||||
|
||||
Phaser.TransformUtils.transform(xf, this.a, this.a);
|
||||
Phaser.TransformUtils.transform(xf, this.b, this.b);
|
||||
|
||||
//this.a = xf.transform(this.a);
|
||||
//this.b = xf.transform(this.b);
|
||||
|
||||
}
|
||||
|
||||
public untransform(xf) {
|
||||
this.a = xf.untransform(this.a);
|
||||
this.b = xf.untransform(this.b);
|
||||
public untransform(xf:Transform) {
|
||||
|
||||
Phaser.TransformUtils.untransform(xf, this.a, this.a);
|
||||
Phaser.TransformUtils.untransform(xf, this.b, this.b);
|
||||
|
||||
//this.a = xf.untransform(this.a);
|
||||
//this.b = xf.untransform(this.b);
|
||||
|
||||
}
|
||||
|
||||
public area() {
|
||||
public area(): number {
|
||||
return Manager.areaForSegment(this.a, this.b, this.radius);
|
||||
}
|
||||
|
||||
public centroid() {
|
||||
public centroid(): Phaser.Vec2 {
|
||||
return Manager.centroidForSegment(this.a, this.b);
|
||||
}
|
||||
|
||||
public inertia(mass) {
|
||||
public inertia(mass: number): number {
|
||||
return Manager.inertiaForSegment(mass, this.a, this.b);
|
||||
}
|
||||
|
||||
public cacheData(xf) {
|
||||
public cacheData(xf:Transform) {
|
||||
|
||||
Phaser.TransformUtils.transform(xf, this.a, this.ta);
|
||||
Phaser.TransformUtils.transform(xf, this.b, this.tb);
|
||||
|
||||
//this.ta = xf.transform(this.a);
|
||||
//this.tb = xf.transform(this.b);
|
||||
|
||||
this.ta = xf.transform(this.a);
|
||||
this.tb = xf.transform(this.b);
|
||||
this.tn = Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(this.tb, this.ta)).normalize();
|
||||
|
||||
var l;
|
||||
@@ -121,7 +135,7 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
}
|
||||
|
||||
public pointQuery(p) {
|
||||
public pointQuery(p: Phaser.Vec2): bool {
|
||||
|
||||
if (!this.bounds.containPoint(p))
|
||||
{
|
||||
@@ -162,7 +176,7 @@ module Phaser.Physics.Advanced {
|
||||
return true;
|
||||
}
|
||||
|
||||
public findVertexByPoint(p, minDist) {
|
||||
public findVertexByPoint(p:Phaser.Vec2, minDist: number): number {
|
||||
|
||||
var dsq = minDist * minDist;
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
//this.postSolve(arb) { };
|
||||
|
||||
this.gravity = new Phaser.Vec2;
|
||||
this.gravity = new Phaser.Vec2(0, 10);
|
||||
this.damping = 0;
|
||||
|
||||
}
|
||||
@@ -85,6 +85,8 @@ module Phaser.Physics.Advanced {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Body added to space', body.name);
|
||||
|
||||
var index = this.bodyArr.push(body) - 1;
|
||||
this.bodyHash[body.id] = index;
|
||||
|
||||
@@ -447,6 +449,8 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
public genTemporalContactSolvers() {
|
||||
|
||||
console.log('genTemporalContactSolvers');
|
||||
|
||||
//var t0 = Date.now();
|
||||
|
||||
var newContactSolverArr = [];
|
||||
@@ -455,7 +459,9 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
for (var body1_index = 0; body1_index < this.bodyArr.length; body1_index++)
|
||||
{
|
||||
var body1 = this.bodyArr[body1_index];
|
||||
var body1: Body = this.bodyArr[body1_index];
|
||||
|
||||
//console.log('body1', body1_index, body1.type);
|
||||
|
||||
if (!body1)
|
||||
{
|
||||
@@ -466,7 +472,10 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
for (var body2_index = 0; body2_index < this.bodyArr.length; body2_index++)
|
||||
{
|
||||
var body2 = this.bodyArr[body2_index];
|
||||
var body2: Body = this.bodyArr[body2_index];
|
||||
|
||||
//console.log('body2', body2_index, body2.type);
|
||||
|
||||
if (!body2)
|
||||
{
|
||||
continue;
|
||||
@@ -477,6 +486,8 @@ module Phaser.Physics.Advanced {
|
||||
continue;
|
||||
}
|
||||
|
||||
//console.log('step');
|
||||
|
||||
var active1 = body1.isAwake && !body1.isStatic;
|
||||
var active2 = body2.isAwake && !body2.isStatic;
|
||||
|
||||
@@ -485,16 +496,22 @@ module Phaser.Physics.Advanced {
|
||||
continue;
|
||||
}
|
||||
|
||||
//console.log('active');
|
||||
|
||||
if (!body1.isCollidable(body2))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//console.log('collideable');
|
||||
|
||||
if (!body1.bounds.intersectsBounds(body2.bounds))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log('>>>>>>>>>> intersects');
|
||||
|
||||
for (var i = 0; i < body1.shapes.length; i++)
|
||||
{
|
||||
for (var j = 0; j < body2.shapes.length; j++)
|
||||
@@ -532,8 +549,8 @@ module Phaser.Physics.Advanced {
|
||||
|
||||
var newContactSolver = new ContactSolver(shape1, shape2);
|
||||
newContactSolver.contacts = contactArr;
|
||||
newContactSolver.elasticity = Math.max(shape1.e, shape2.e);
|
||||
newContactSolver.friction = Math.sqrt(shape1.u * shape2.u);
|
||||
newContactSolver.elasticity = Math.max(shape1.elasticity, shape2.elasticity);
|
||||
newContactSolver.friction = Math.sqrt(shape1.friction * shape2.friction);
|
||||
newContactSolverArr.push(newContactSolver);
|
||||
}
|
||||
}
|
||||
@@ -660,6 +677,7 @@ module Phaser.Physics.Advanced {
|
||||
for (var i = 0; i < this.bodyArr.length; i++)
|
||||
{
|
||||
var body = this.bodyArr[i];
|
||||
|
||||
if (!body)
|
||||
{
|
||||
continue;
|
||||
@@ -757,7 +775,9 @@ module Phaser.Physics.Advanced {
|
||||
for (var i = 0; i < this.contactSolvers.length; i++)
|
||||
{
|
||||
var arb = this.contactSolvers[i];
|
||||
this.postSolve(arb);
|
||||
|
||||
// Re-enable this
|
||||
//this.postSolve(arb);
|
||||
}
|
||||
|
||||
for (var i = 0; i < this.bodyArr.length; i++)
|
||||
|
||||
Reference in New Issue
Block a user