Refactor completed. Now to tie the physics and sprites together.

This commit is contained in:
Richard Davey 2013-06-14 18:54:12 +01:00
parent 533a4c2e30
commit 0a7513240a
21 changed files with 1112 additions and 2385 deletions

View file

@ -210,33 +210,33 @@
<Content Include="physics\advanced\Manager.js">
<DependentUpon>Manager.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="physics\advanced\ShapeCircle.ts" />
<TypeScriptCompile Include="physics\advanced\ShapeBox.ts" />
<Content Include="physics\advanced\ShapeBox.js">
<DependentUpon>ShapeBox.ts</DependentUpon>
</Content>
<Content Include="physics\advanced\ShapeCircle.js">
<DependentUpon>ShapeCircle.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="physics\advanced\ShapeSegment.ts" />
<TypeScriptCompile Include="physics\advanced\ShapePoly.ts" />
<Content Include="physics\advanced\ShapePoly.js">
<DependentUpon>ShapePoly.ts</DependentUpon>
</Content>
<Content Include="physics\advanced\ShapeSegment.js">
<DependentUpon>ShapeSegment.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="physics\advanced\ShapeTriangle.ts" />
<TypeScriptCompile Include="physics\advanced\shapes\Shape.ts" />
<TypeScriptCompile Include="physics\advanced\shapes\IShape.ts" />
<TypeScriptCompile Include="physics\advanced\shapes\Box.ts" />
<Content Include="physics\advanced\shapes\Box.js">
<DependentUpon>Box.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="physics\advanced\shapes\Circle.ts" />
<Content Include="physics\advanced\shapes\Circle.js">
<DependentUpon>Circle.ts</DependentUpon>
</Content>
<Content Include="physics\advanced\shapes\IShape.js">
<DependentUpon>IShape.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="physics\advanced\shapes\Poly.ts" />
<Content Include="physics\advanced\shapes\Poly.js">
<DependentUpon>Poly.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="physics\advanced\shapes\Segment.ts" />
<Content Include="physics\advanced\shapes\Segment.js">
<DependentUpon>Segment.ts</DependentUpon>
</Content>
<Content Include="physics\advanced\shapes\Shape.js">
<DependentUpon>Shape.ts</DependentUpon>
</Content>
<Content Include="physics\advanced\ShapeTriangle.js">
<DependentUpon>ShapeTriangle.ts</DependentUpon>
<TypeScriptCompile Include="physics\advanced\shapes\Triangle.ts" />
<Content Include="physics\advanced\shapes\Triangle.js">
<DependentUpon>Triangle.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="physics\advanced\Space.ts" />
<Content Include="physics\advanced\Space.js">

View file

@ -1,11 +1,13 @@
/// <reference path="../../math/Vec2.ts" />
/// <reference path="../../geom/Point.ts" />
/// <reference path="../../math/Vec2Utils.ts" />
/// <reference path="shapes/Shape.ts" />
/// <reference path="shapes/Circle.ts" />
/// <reference path="shapes/Poly.ts" />
/// <reference path="shapes/Segment.ts" />
/// <reference path="Manager.ts" />
/// <reference path="Body.ts" />
/// <reference path="shapes/Shape.ts" />
/// <reference path="Contact.ts" />
/// <reference path="ShapeCircle.ts" />
/**
* Phaser - Advanced Physics - Collision Handlers
@ -22,8 +24,6 @@ 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)
{
@ -114,13 +114,13 @@ module Phaser.Physics.Advanced {
}
public circle2Circle(circ1, circ2, contactArr) {
return this._circle2Circle(circ1.tc, circ1.r, circ2.tc, circ2.r, contactArr);
public circle2Circle(circ1: Phaser.Physics.Advanced.Shapes.Circle, circ2: Phaser.Physics.Advanced.Shapes.Circle, contactArr: Contact[]) {
return this._circle2Circle(circ1.tc, circ1.radius, circ2.tc, circ2.radius, contactArr);
}
public circle2Segment(circ: ShapeCircle, seg, contactArr: Contact[]) {
public circle2Segment(circ: Phaser.Physics.Advanced.Shapes.Circle, seg: Phaser.Physics.Advanced.Shapes.Segment, contactArr: Contact[]) {
var rsum = circ.radius + seg.r;
var rsum = circ.radius + seg.radius;
// Normal distance from segment
var dn = Phaser.Vec2Utils.dot(circ.tc, seg.tn) - Phaser.Vec2Utils.dot(seg.ta, seg.tn);
@ -142,19 +142,20 @@ module Phaser.Physics.Advanced {
return 0;
}
return this._circle2Circle(circ.tc, circ.radius, seg.ta, seg.r, contactArr);
return this._circle2Circle(circ.tc, circ.radius, seg.ta, seg.radius, contactArr);
}
else if (dt > dtMax)
{
{
if (dt > dtMax + rsum)
{
return 0;
}
return this._circle2Circle(circ.tc, circ.radius, seg.tb, seg.r, contactArr);
return this._circle2Circle(circ.tc, circ.radius, seg.tb, seg.radius, contactArr);
}
var n: Phaser.Vec2 = new Phaser.Vec2;
if (dn > 0)
{
n.copyFrom(seg.tn);
@ -178,7 +179,7 @@ module Phaser.Physics.Advanced {
}
public circle2Poly(circ: ShapeCircle, poly, contactArr: Contact[]) {
public circle2Poly(circ: Phaser.Physics.Advanced.Shapes.Circle, poly: Phaser.Physics.Advanced.Shapes.Poly, contactArr: Contact[]) {
var minDist = -999999;
var minIdx = -1;
@ -229,7 +230,7 @@ module Phaser.Physics.Advanced {
}
public segmentPointDistanceSq(seg, p) {
public segmentPointDistanceSq(seg: Phaser.Physics.Advanced.Shapes.Segment, p) {
var w: Phaser.Vec2 = new Phaser.Vec2;
var d: Phaser.Vec2 = new Phaser.Vec2;
@ -258,7 +259,7 @@ module Phaser.Physics.Advanced {
}
// FIXME and optimise me lots!!!
public segment2Segment(seg1, seg2, contactArr) {
public segment2Segment(seg1: Phaser.Physics.Advanced.Shapes.Segment, seg2: Phaser.Physics.Advanced.Shapes.Segment, contactArr: Contact[]) {
var d = [];
d[0] = this.segmentPointDistanceSq(seg1, seg2.ta);
@ -301,12 +302,12 @@ module Phaser.Physics.Advanced {
var minp1 = Phaser.Vec2Utils.multiplyAdd(seg1.ta, u, s);
var minp2 = Phaser.Vec2Utils.multiplyAdd(seg2.ta, v, t);
return this._circle2Circle(minp1, seg1.r, minp2, seg2.r, contactArr);
return this._circle2Circle(minp1, seg1.radius, minp2, seg2.radius, contactArr);
}
// Identify vertexes that have penetrated the segment.
public findPointsBehindSeg(contactArr, seg, poly, dist, coef) {
public findPointsBehindSeg(contactArr: Contact[], seg: Phaser.Physics.Advanced.Shapes.Segment, poly: Phaser.Physics.Advanced.Shapes.Poly, dist: number, coef: number) {
var dta = Phaser.Vec2Utils.cross(seg.tn, seg.ta);
var dtb = Phaser.Vec2Utils.cross(seg.tn, seg.tb);
@ -319,7 +320,7 @@ module Phaser.Physics.Advanced {
{
var v = poly.tverts[i];
if (Phaser.Vec2Utils.dot(v, n) < Phaser.Vec2Utils.dot(seg.tn, seg.ta) * coef + seg.r)
if (Phaser.Vec2Utils.dot(v, n) < Phaser.Vec2Utils.dot(seg.tn, seg.ta) * coef + seg.radius)
{
var dt = Phaser.Vec2Utils.cross(seg.tn, v);
@ -331,9 +332,11 @@ module Phaser.Physics.Advanced {
}
}
public segment2Poly(seg, poly, contactArr) {
public segment2Poly(seg: Phaser.Physics.Advanced.Shapes.Segment, poly: Phaser.Physics.Advanced.Shapes.Poly, contactArr: Contact[]) {
var seg_td = Phaser.Vec2Utils.dot(seg.tn, seg.ta);
var seg_d1 = poly.distanceOnPlane(seg.tn, seg_td) - seg.r;
var seg_d1 = poly.distanceOnPlane(seg.tn, seg_td) - seg.radius;
if (seg_d1 > 0)
{
return 0;
@ -341,7 +344,7 @@ module Phaser.Physics.Advanced {
var n: Phaser.Vec2 = new Phaser.Vec2;
Phaser.Vec2Utils.negative(seg.tn, n);
var seg_d2 = poly.distanceOnPlane(n, -seg_td) - seg.r;
var seg_d2 = poly.distanceOnPlane(n, -seg_td) - seg.radius;
//var seg_d2 = poly.distanceOnPlane(vec2.neg(seg.tn), -seg_td) - seg.r;
if (seg_d2 > 0)
@ -374,11 +377,11 @@ module Phaser.Physics.Advanced {
//var poly_n = vec2.neg(poly.tplanes[poly_i].n);
var va: Phaser.Vec2 = new Phaser.Vec2;
Phaser.Vec2Utils.multiplyAdd(seg.ta, poly_n, seg.r, va);
Phaser.Vec2Utils.multiplyAdd(seg.ta, poly_n, seg.radius, va);
//var va = vec2.mad(seg.ta, poly_n, seg.r);
var vb: Phaser.Vec2 = new Phaser.Vec2;
Phaser.Vec2Utils.multiplyAdd(seg.tb, poly_n, seg.r, vb);
Phaser.Vec2Utils.multiplyAdd(seg.tb, poly_n, seg.radius, vb);
//var vb = vec2.mad(seg.tb, poly_n, seg.r);
if (poly.containPoint(va))
@ -412,22 +415,22 @@ module Phaser.Physics.Advanced {
var poly_a = poly.tverts[poly_i];
var poly_b = poly.tverts[(poly_i + 1) % poly.verts.length];
if (this._circle2Circle(seg.ta, seg.r, poly_a, 0, contactArr))
if (this._circle2Circle(seg.ta, seg.radius, poly_a, 0, contactArr))
{
return 1;
}
if (this._circle2Circle(seg.tb, seg.r, poly_a, 0, contactArr))
if (this._circle2Circle(seg.tb, seg.radius, poly_a, 0, contactArr))
{
return 1;
}
if (this._circle2Circle(seg.ta, seg.r, poly_b, 0, contactArr))
if (this._circle2Circle(seg.ta, seg.radius, poly_b, 0, contactArr))
{
return 1;
}
if (this._circle2Circle(seg.tb, seg.r, poly_b, 0, contactArr))
if (this._circle2Circle(seg.tb, seg.radius, poly_b, 0, contactArr))
{
return 1;
}
@ -438,7 +441,7 @@ module Phaser.Physics.Advanced {
}
// Find the minimum separating axis for the given poly and plane list.
public findMSA(poly, planes, num) {
public findMSA(poly: Phaser.Physics.Advanced.Shapes.Poly, planes, num: number) {
var min_dist = -999999;
var min_index = -1;
@ -446,8 +449,10 @@ module Phaser.Physics.Advanced {
for (var i = 0; i < num; i++)
{
var dist = poly.distanceOnPlane(planes[i].n, planes[i].d);
if (dist > 0)
{ // no collision
{
// no collision
return { dist: 0, index: -1 };
}
else if (dist > min_dist)
@ -462,7 +467,7 @@ module Phaser.Physics.Advanced {
}
public findVertsFallback(contactArr, poly1, poly2, n, dist) {
public findVertsFallback(contactArr: Contact[], poly1: Phaser.Physics.Advanced.Shapes.Poly, poly2: Phaser.Physics.Advanced.Shapes.Poly, n, dist: number) {
var num = 0;
@ -493,7 +498,7 @@ module Phaser.Physics.Advanced {
}
// Find the overlapped vertices.
public findVerts(contactArr, poly1, poly2, n, dist) {
public findVerts(contactArr: Contact[], poly1: Phaser.Physics.Advanced.Shapes.Poly, poly2: Phaser.Physics.Advanced.Shapes.Poly, n, dist: number) {
var num = 0;
@ -523,7 +528,7 @@ module Phaser.Physics.Advanced {
}
public poly2Poly(poly1, poly2, contactArr) {
public poly2Poly(poly1: Phaser.Physics.Advanced.Shapes.Poly, poly2: Phaser.Physics.Advanced.Shapes.Poly, contactArr: Contact[]) {
var msa1 = this.findMSA(poly2, poly1.tplanes, poly1.verts.length);

View file

@ -1,5 +1,4 @@
/// <reference path="../../math/Vec2.ts" />
/// <reference path="../../geom/Point.ts" />
/// <reference path="../../math/Vec2Utils.ts" />
/// <reference path="Manager.ts" />
/// <reference path="Body.ts" />
@ -33,29 +32,29 @@ module Phaser.Physics.Advanced {
public hash;
// Linear velocities at contact point
// Linear velocities at contact point
public r1: Phaser.Vec2;
public r2: Phaser.Vec2;
public r1_local: Phaser.Vec2;
public r2_local: Phaser.Vec2;
// Bounce velocity
public bounce;
public emn;
public emt;
// Bounce velocity
public bounce: number;
public emn: number;
public emt: number;
// Contact point
// Contact point
public point;
// Contact normal (toward shape2)
public normal: Phaser.Vec2;
// Penetration depth (d < 0)
// Penetration depth (d < 0)
public depth;
// Accumulated normal constraint impulse
// Accumulated normal constraint impulse
public lambdaNormal;
// Accumulated tangential constraint impulse
// Accumulated tangential constraint impulse
public lambdaTangential;
}

View file

@ -1,43 +0,0 @@
/// <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" />
/// <reference path="ShapePoly.ts" />
/**
* Phaser - Advanced Physics - ShapeBox
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
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;
super([
new Phaser.Vec2(-hw + x, +hh + y),
new Phaser.Vec2(-hw + x, -hh + y),
new Phaser.Vec2(+hw + x, -hh + y),
new Phaser.Vec2(+hw + x, +hh + y)
]);
}
}
}

View file

@ -1,102 +0,0 @@
/// <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 - Shape
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced {
export class ShapeCircle extends Phaser.Physics.Advanced.Shape implements IShape {
constructor(radius: number, x?: number = 0, y?: number = 0) {
super(Manager.SHAPE_TYPE_CIRCLE);
this.center = new Phaser.Vec2(x, y);
this.radius = radius;
this.tc = new Phaser.Vec2;
this.finishVerts();
}
public radius: number;
public center: Phaser.Vec2;
public tc: Phaser.Vec2;
public finishVerts() {
this.radius = Math.abs(this.radius);
}
public duplicate() {
return new ShapeCircle(this.center.x, this.center.y, this.radius);
}
public recenter(c:Phaser.Vec2) {
this.center.subtract(c);
}
public transform(xf: Transform) {
Phaser.TransformUtils.transform(xf, this.center, this.center);
//this.center = xf.transform(this.center);
}
public untransform(xf: Transform) {
Phaser.TransformUtils.untransform(xf, this.center, this.center);
//this.center = xf.untransform(this.center);
}
public area(): number {
return Manager.areaForCircle(this.radius, 0);
}
public centroid(): Phaser.Vec2 {
return Phaser.Vec2Utils.clone(this.center);
}
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);
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: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:Phaser.Vec2, minDist: number): number {
var dsq = minDist * minDist;
if (Phaser.Vec2Utils.distanceSq(this.tc, p) < dsq)
{
return 0;
}
return -1;
}
public distanceOnPlane(n, d) {
Phaser.Vec2Utils.dot(n, this.tc) - this.radius - d;
}
}
}

View file

@ -1,303 +0,0 @@
/// <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 - ShapePoly (convex only)
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced {
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 = [];
this.planes = [];
this.tverts = [];
this.tplanes = [];
if (verts)
{
for (var i = 0; i < verts.length; i++)
{
//console.log('cloning vert', i);
this.verts[i] = Phaser.Vec2Utils.clone(verts[i]);
this.tverts[i] = this.verts[i];
this.tplanes[i] = {};
this.tplanes[i].n = new Phaser.Vec2;
this.tplanes[i].d = 0;
}
}
//console.log('ShapePoly finished', this.verts);
this.finishVerts();
//console.log('ShapePoly finished 2', this.verts);
}
public verts: Phaser.Vec2[];
public planes;
public tverts;
public tplanes;
public convexity: bool;
public finishVerts() {
if (this.verts.length < 2)
{
this.convexity = false;
this.planes = [];
return;
}
this.convexity = true;
this.tverts = [];
this.tplanes = [];
// Must be counter-clockwise verts
for (var i = 0; i < this.verts.length; i++)
{
var a = this.verts[i];
var b = this.verts[(i + 1) % this.verts.length];
var n = Phaser.Vec2Utils.normalize(Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(a, b)));
this.planes[i] = {};
this.planes[i].n = n;
this.planes[i].d = Phaser.Vec2Utils.dot(n, a);
this.tverts[i] = this.verts[i];
this.tplanes[i] = {};
this.tplanes[i].n = new Phaser.Vec2;
this.tplanes[i].d = 0;
}
for (var i = 0; i < this.verts.length; i++)
{
var b = this.verts[(i + 2) % this.verts.length];
var n = this.planes[i].n;
var d = this.planes[i].d;
if (Phaser.Vec2Utils.dot(n, b) - d > 0)
{
this.convexity = false;
}
}
}
public duplicate() {
return new ShapePoly(this.verts);
}
public recenter(c) {
for (var i = 0; i < this.verts.length; i++)
{
this.verts[i].subtract(c);
}
}
public transform(xf) {
for (var i = 0; i < this.verts.length; i++)
{
this.verts[i] = xf.transform(this.verts[i]);
}
}
public untransform(xf) {
for (var i = 0; i < this.verts.length; i++)
{
this.verts[i] = xf.untransform(this.verts[i]);
}
}
public area(): number {
return Manager.areaForPoly(this.verts);
}
public centroid(): Phaser.Vec2 {
return Manager.centroidForPoly(this.verts);
}
public inertia(mass: number): number {
return Manager.inertiaForPoly(mass, this.verts, new Phaser.Vec2);
}
public cacheData(xf:Transform) {
this.bounds.clear();
var numVerts = this.verts.length;
//console.log('shapePoly cacheData', numVerts);
if (numVerts == 0)
{
return;
}
for (var i = 0; i < numVerts; i++)
{
Phaser.TransformUtils.transform(xf, this.tverts[i], this.tverts[i]);
//this.tverts[i] = xf.transform(this.verts[i]);
}
if (numVerts < 2)
{
this.bounds.addPoint(this.tverts[0]);
return;
}
for (var i = 0; i < numVerts; i++)
{
var a = this.tverts[i];
var b = this.tverts[(i + 1) % numVerts];
var n = Phaser.Vec2Utils.normalize(Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(a, b)));
this.tplanes[i].n = n;
this.tplanes[i].d = Phaser.Vec2Utils.dot(n, a);
this.bounds.addPoint(a);
}
}
public pointQuery(p: Phaser.Vec2): bool {
if (!this.bounds.containPoint(p))
{
return false;
}
return this.containPoint(p);
}
public findVertexByPoint(p:Phaser.Vec2, minDist: number): number {
var dsq = minDist * minDist;
for (var i = 0; i < this.tverts.length; i++)
{
if (Phaser.Vec2Utils.distanceSq(this.tverts[i], p) < dsq)
{
return i;
}
}
return -1;
}
public findEdgeByPoint(p: Phaser.Vec2, minDist: number): number {
var dsq = minDist * minDist;
var numVerts = this.tverts.length;
for (var i = 0; i < this.tverts.length; i++)
{
var v1 = this.tverts[i];
var v2 = this.tverts[(i + 1) % numVerts];
var n = this.tplanes[i].n;
var dtv1 = Phaser.Vec2Utils.cross(v1, n);
var dtv2 = Phaser.Vec2Utils.cross(v2, n);
var dt = Phaser.Vec2Utils.cross(p, n);
if (dt > dtv1)
{
if (Phaser.Vec2Utils.distanceSq(v1, p) < dsq)
{
return i;
}
}
else if (dt < dtv2)
{
if (Phaser.Vec2Utils.distanceSq(v2, p) < dsq)
{
return i;
}
}
else
{
var dist = Phaser.Vec2Utils.dot(n, p) - Phaser.Vec2Utils.dot(n, v1);
if (dist * dist < dsq)
{
return i;
}
}
}
return -1;
}
public distanceOnPlane(n, d) {
var min = 999999;
for (var i = 0; i < this.verts.length; i++)
{
min = Math.min(min, Phaser.Vec2Utils.dot(n, this.tverts[i]));
}
return min - d;
}
public containPoint(p) {
for (var i = 0; i < this.verts.length; i++)
{
var plane = this.tplanes[i];
if (Phaser.Vec2Utils.dot(plane.n, p) - plane.d > 0)
{
return false;
}
}
return true;
}
public containPointPartial(p, n) {
for (var i = 0; i < this.verts.length; i++)
{
var plane = this.tplanes[i];
if (Phaser.Vec2Utils.dot(plane.n, n) < 0.0001)
{
continue;
}
if (Phaser.Vec2Utils.dot(plane.n, p) - plane.d > 0)
{
return false;
}
}
return true;
}
}
}

View file

@ -1,206 +0,0 @@
/// <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 - Shape
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced {
export class ShapeSegment extends Phaser.Physics.Advanced.Shape implements IShape {
constructor(a, b, radius: number) {
super(Manager.SHAPE_TYPE_SEGMENT);
// What types are A and B??!
this.a = a.duplicate();
this.b = b.duplicate();
this.radius = radius;
this.normal = Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(b, a));
this.normal.normalize();
this.ta = new Phaser.Vec2;
this.tb = new Phaser.Vec2;
this.tn = new Phaser.Vec2;
this.finishVerts();
}
public a: Phaser.Vec2;
public b: Phaser.Vec2;
public radius: number;
public normal: Phaser.Vec2;
public ta: Phaser.Vec2;
public tb: Phaser.Vec2;
public tn: Phaser.Vec2;
public finishVerts() {
this.normal = Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(this.b, this.a));
this.normal.normalize();
this.radius = Math.abs(this.radius);
}
public duplicate() {
return new ShapeSegment(this.a, this.b, this.radius);
}
public recenter(c) {
this.a.subtract(c);
this.b.subtract(c);
}
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: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(): number {
return Manager.areaForSegment(this.a, this.b, this.radius);
}
public centroid(): Phaser.Vec2 {
return Manager.centroidForSegment(this.a, this.b);
}
public inertia(mass: number): number {
return Manager.inertiaForSegment(mass, this.a, this.b);
}
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.tn = Phaser.Vec2Utils.perp(Phaser.Vec2Utils.subtract(this.tb, this.ta)).normalize();
var l;
var r;
var t;
var b;
if (this.ta.x < this.tb.x)
{
l = this.ta.x;
r = this.tb.x;
}
else
{
l = this.tb.x;
r = this.ta.x;
}
if (this.ta.y < this.tb.y)
{
b = this.ta.y;
t = this.tb.y;
} else
{
b = this.tb.y;
t = this.ta.y;
}
this.bounds.mins.setTo(l - this.radius, b - this.radius);
this.bounds.maxs.setTo(r + this.radius, t + this.radius);
}
public pointQuery(p: Phaser.Vec2): bool {
if (!this.bounds.containPoint(p))
{
return false;
}
var dn = Phaser.Vec2Utils.dot(this.tn, p) - Phaser.Vec2Utils.dot(this.ta, this.tn);
var dist = Math.abs(dn);
if (dist > this.radius)
{
return false;
}
var dt = Phaser.Vec2Utils.cross(p, this.tn);
var dta = Phaser.Vec2Utils.cross(this.ta, this.tn);
var dtb = Phaser.Vec2Utils.cross(this.tb, this.tn);
if (dt <= dta)
{
if (dt < dta - this.radius)
{
return false;
}
return Phaser.Vec2Utils.distanceSq(this.ta, p) < (this.radius * this.radius);
}
else if (dt > dtb)
{
if (dt > dtb + this.radius)
{
return false;
}
return Phaser.Vec2Utils.distanceSq(this.tb, p) < (this.radius * this.radius);
}
return true;
}
public findVertexByPoint(p:Phaser.Vec2, minDist: number): number {
var dsq = minDist * minDist;
if (Phaser.Vec2Utils.distanceSq(this.ta, p) < dsq)
{
return 0;
}
if (Phaser.Vec2Utils.distanceSq(this.tb, p) < dsq)
{
return 1;
}
return -1;
}
public distanceOnPlane(n, d) {
var a = Phaser.Vec2Utils.dot(n, this.ta) - this.radius;
var b = Phaser.Vec2Utils.dot(n, this.tb) - this.radius;
return Math.min(a, b) - d;
}
}
}

View file

@ -1,27 +0,0 @@
/// <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" />
/// <reference path="ShapePoly.ts" />
/**
* Phaser - Advanced Physics - ShapeTriangle
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced {
export class ShapeTriangle extends Phaser.Physics.Advanced.ShapePoly {
constructor(p1, p2, p3) {
super( [ new Phaser.Vec2(p1.x, p1.y), new Phaser.Vec2(p2.x, p2.y), new Phaser.Vec2(p3.x, p3.y) ] );
}
}
}

View file

@ -1,5 +1,4 @@
/// <reference path="../../math/Vec2.ts" />
/// <reference path="../../geom/Point.ts" />
/// <reference path="../../math/Vec2Utils.ts" />
/// <reference path="Manager.ts" />
/// <reference path="Body.ts" />

View file

@ -2,7 +2,7 @@
/// <reference path="../Manager.ts" />
/// <reference path="../Body.ts" />
/// <reference path="Shape.ts" />
/// <reference path="ShapePoly.ts" />
/// <reference path="Poly.ts" />
/**
* Phaser - Advanced Physics - Shapes - Box
@ -12,7 +12,7 @@
module Phaser.Physics.Advanced.Shapes {
export class Box extends Phaser.Physics.Advanced.ShapePoly {
export class Box extends Phaser.Physics.Advanced.Shapes.Poly {
// Give in pixels
constructor(x, y, width, height) {

View file

@ -34,8 +34,8 @@ module Phaser.Physics.Advanced.Shapes {
this.radius = Math.abs(this.radius);
}
public duplicate() {
return new ShapeCircle(this.center.x, this.center.y, this.radius);
public duplicate(): Circle {
return new Circle(this.center.x, this.center.y, this.radius);
}
public recenter(c:Phaser.Vec2) {

View file

@ -94,7 +94,7 @@ module Phaser.Physics.Advanced.Shapes {
}
public duplicate() {
return new ShapePoly(this.verts);
return new Phaser.Physics.Advanced.Shapes.Poly(this.verts);
}
public recenter(c) {

View file

@ -18,7 +18,6 @@ module Phaser.Physics.Advanced.Shapes {
super(Manager.SHAPE_TYPE_SEGMENT);
// What types are A and B??!
this.a = a.duplicate();
this.b = b.duplicate();
this.radius = radius;
@ -53,7 +52,7 @@ module Phaser.Physics.Advanced.Shapes {
}
public duplicate() {
return new ShapeSegment(this.a, this.b, this.radius);
return new Phaser.Physics.Advanced.Shapes.Segment(this.a, this.b, this.radius);
}
public recenter(c) {

View file

@ -1,5 +1,4 @@
/// <reference path="../../../math/Vec2.ts" />
/// <reference path="../../../geom/Point.ts" />
/// <reference path="../../../math/Vec2Utils.ts" />
/// <reference path="../Manager.ts" />
/// <reference path="../Body.ts" />

View file

@ -1,4 +1,4 @@
/// <reference path="../../math/Vec2.ts" />
/// <reference path="../../../math/Vec2.ts" />
/// <reference path="../Manager.ts" />
/// <reference path="../Body.ts" />
/// <reference path="Shape.ts" />
@ -10,9 +10,9 @@
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced {
module Phaser.Physics.Advanced.Shapes {
export class ShapeTriangle extends Phaser.Physics.Advanced.Shapes.Poly {
export class Triangle extends Phaser.Physics.Advanced.Shapes.Poly {
constructor(p1, p2, p3) {

View file

@ -47,6 +47,8 @@ TODO:
* Move all of the renderDebugInfo methods to the DebugUtils class
* Check bounds/edge points when sprite is only 1x1 sized :)
* See what I can move out of Body and into a BodyUtils class.
* See about optimising Advanced Physics a lot more, so it doesn't create lots of Vec2s everywhere.
V1.0.0
@ -114,7 +116,7 @@ V1.0.0
* Added SpriteUtils.overlapsXY and overlapsPoint to check if a point is within a sprite, taking scale and rotation into account.
* Added Cache.getImageKeys (and similar) to return an array of all the keys for all currently cached objects.
* Added Group.bringToTop feature. Will sort the Group, move the given sprites z-index to the top and shift the rest down by one.
* Brand new Advanced Physics system added and working! Woohoo :)

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,7 @@
/// <reference path="../../Phaser/Game.ts" />
/// <reference path="../../Phaser/physics/advanced/Manager.ts" />
/// <reference path="../../Phaser/physics/advanced/ShapeBox.ts" />
/// <reference path="../../Phaser/physics/advanced/ShapeCircle.ts" />
/// <reference path="../../Phaser/physics/advanced/shapes/Box.ts" />
/// <reference path="../../Phaser/physics/advanced/shapes/Circle.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update, render);
function init() {
@ -24,8 +24,7 @@
walls = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_STATIC);
walls.game = game;
// position is in relation to the containing body! don't forget this
ground = walls.addShape(new Phaser.Physics.Advanced.ShapeBox(400, 500, 500, 20));
ground.friction = 10;
ground = walls.addShape(new Phaser.Physics.Advanced.Shapes.Box(400, 500, 500, 20));
//walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 0.2, 20.48, 0.4));
//walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 15.16, 20.48, 0.4));
//walls.addShape(new Phaser.Physics.Advanced.ShapeBox(-10.04, 7.68, 0.4, 14.56));
@ -35,9 +34,9 @@
// Add a circle
circle = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_DYNAMIC, physics.pixelsToMeters(300), physics.pixelsToMeters(200));
circle.game = game;
var shape = new Phaser.Physics.Advanced.ShapeCircle(0.4, 0, 0);
var shape = new Phaser.Physics.Advanced.Shapes.Circle(0.4, 0, 0);
shape.elasticity = 0.8;
shape.friction = 10.0;
shape.friction = 1;
shape.density = 1;
circle.addShape(shape);
circle.resetMassData();

View file

@ -1,7 +1,7 @@
/// <reference path="../../Phaser/Game.ts" />
/// <reference path="../../Phaser/physics/advanced/Manager.ts" />
/// <reference path="../../Phaser/physics/advanced/ShapeBox.ts" />
/// <reference path="../../Phaser/physics/advanced/ShapeCircle.ts" />
/// <reference path="../../Phaser/physics/advanced/shapes/Box.ts" />
/// <reference path="../../Phaser/physics/advanced/shapes/Circle.ts" />
(function () {
@ -22,7 +22,7 @@
var circle: Phaser.Physics.Advanced.Body;
var walls: Phaser.Physics.Advanced.Body;
var ground: Phaser.Physics.Advanced.ShapeBox;
var ground: Phaser.Physics.Advanced.Shapes.Box;
function create() {
@ -36,8 +36,7 @@
walls.game = game;
// position is in relation to the containing body! don't forget this
ground = walls.addShape(new Phaser.Physics.Advanced.ShapeBox(400, 500, 500, 20));
ground.friction = 10;
ground = walls.addShape(new Phaser.Physics.Advanced.Shapes.Box(400, 500, 500, 20));
//walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 0.2, 20.48, 0.4));
//walls.addShape(new Phaser.Physics.Advanced.ShapeBox(0, 15.16, 20.48, 0.4));
@ -52,9 +51,9 @@
circle = new Phaser.Physics.Advanced.Body(null, Phaser.Types.BODY_DYNAMIC, physics.pixelsToMeters(300), physics.pixelsToMeters(200));
circle.game = game;
var shape = new Phaser.Physics.Advanced.ShapeCircle(0.4, 0, 0);
var shape = new Phaser.Physics.Advanced.Shapes.Circle(0.4, 0, 0);
shape.elasticity = 0.8;
shape.friction = 10.0;
shape.friction = 1;
shape.density = 1;
circle.addShape(shape);
circle.resetMassData();

272
build/phaser.d.ts vendored
View file

@ -9734,9 +9734,9 @@ module Phaser.Physics.Advanced {
public r2: Vec2;
public r1_local: Vec2;
public r2_local: Vec2;
public bounce;
public emn;
public emt;
public bounce: number;
public emn: number;
public emt: number;
public point;
public normal: Vec2;
public depth;
@ -9761,18 +9761,18 @@ module Phaser.Physics.Advanced {
}
}
/**
* Phaser - Advanced Physics - Shape
* Phaser - Advanced Physics - Shape - Circle
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced {
class ShapeCircle extends Shape implements IShape {
module Phaser.Physics.Advanced.Shapes {
class Circle extends Shape implements IShape {
constructor(radius: number, x?: number, y?: number);
public radius: number;
public center: Vec2;
public tc: Vec2;
public finishVerts(): void;
public duplicate(): ShapeCircle;
public duplicate(): Circle;
public recenter(c: Vec2): void;
public transform(xf: Transform): void;
public untransform(xf: Transform): void;
@ -9786,6 +9786,65 @@ module Phaser.Physics.Advanced {
}
}
/**
* Phaser - Advanced Physics - Shapes - Convex Polygon
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced.Shapes {
class Poly extends Shape implements IShape {
constructor(verts?: Vec2[]);
public verts: Vec2[];
public planes;
public tverts;
public tplanes;
public convexity: bool;
public finishVerts(): void;
public duplicate(): Poly;
public recenter(c): void;
public transform(xf): void;
public untransform(xf): void;
public area(): number;
public centroid(): Vec2;
public inertia(mass: number): number;
public cacheData(xf: Transform): void;
public pointQuery(p: Vec2): bool;
public findVertexByPoint(p: Vec2, minDist: number): number;
public findEdgeByPoint(p: Vec2, minDist: number): number;
public distanceOnPlane(n, d): number;
public containPoint(p): bool;
public containPointPartial(p, n): bool;
}
}
/**
* Phaser - Advanced Physics - Shapes - Segment
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced.Shapes {
class Segment extends Shape implements IShape {
constructor(a, b, radius: number);
public a: Vec2;
public b: Vec2;
public radius: number;
public normal: Vec2;
public ta: Vec2;
public tb: Vec2;
public tn: Vec2;
public finishVerts(): void;
public duplicate(): Segment;
public recenter(c): void;
public transform(xf: Transform): void;
public untransform(xf: Transform): void;
public area(): number;
public centroid(): Vec2;
public inertia(mass: number): number;
public cacheData(xf: Transform): void;
public pointQuery(p: Vec2): bool;
public findVertexByPoint(p: Vec2, minDist: number): number;
public distanceOnPlane(n, d): number;
}
}
/**
* Phaser - Advanced Physics - Collision Handlers
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
@ -9793,22 +9852,22 @@ module Phaser.Physics.Advanced {
module Phaser.Physics.Advanced {
class Collision {
constructor();
public collide(a, b, contacts: Contact[]);
public collide(a, b, contacts: Contact[]): number;
private _circle2Circle(c1, r1, c2, r2, contactArr);
public circle2Circle(circ1, circ2, contactArr): number;
public circle2Segment(circ: ShapeCircle, seg, contactArr: Contact[]): number;
public circle2Poly(circ: ShapeCircle, poly, contactArr: Contact[]): number;
public segmentPointDistanceSq(seg, p): number;
public segment2Segment(seg1, seg2, contactArr): number;
public findPointsBehindSeg(contactArr, seg, poly, dist, coef): void;
public segment2Poly(seg, poly, contactArr);
public findMSA(poly, planes, num): {
public circle2Circle(circ1: Shapes.Circle, circ2: Shapes.Circle, contactArr: Contact[]): number;
public circle2Segment(circ: Shapes.Circle, seg: Shapes.Segment, contactArr: Contact[]): number;
public circle2Poly(circ: Shapes.Circle, poly: Shapes.Poly, contactArr: Contact[]): number;
public segmentPointDistanceSq(seg: Shapes.Segment, p): number;
public segment2Segment(seg1: Shapes.Segment, seg2: Shapes.Segment, contactArr: Contact[]): number;
public findPointsBehindSeg(contactArr: Contact[], seg: Shapes.Segment, poly: Shapes.Poly, dist: number, coef: number): void;
public segment2Poly(seg: Shapes.Segment, poly: Shapes.Poly, contactArr: Contact[]): number;
public findMSA(poly: Shapes.Poly, planes, num: number): {
dist: number;
index: number;
};
public findVertsFallback(contactArr, poly1, poly2, n, dist): number;
public findVerts(contactArr, poly1, poly2, n, dist): number;
public poly2Poly(poly1, poly2, contactArr): number;
public findVertsFallback(contactArr: Contact[], poly1: Shapes.Poly, poly2: Shapes.Poly, n, dist: number): number;
public findVerts(contactArr: Contact[], poly1: Shapes.Poly, poly2: Shapes.Poly, n, dist: number): number;
public poly2Poly(poly1: Shapes.Poly, poly2: Shapes.Poly, contactArr: Contact[]): number;
}
}
/**
@ -9972,193 +10031,26 @@ module Phaser.Physics.Advanced {
}
}
/**
* Phaser - Advanced Physics - ShapePoly (convex only)
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced {
class ShapePoly extends Shape implements IShape {
constructor(verts?: Vec2[]);
public verts: Vec2[];
public planes;
public tverts;
public tplanes;
public convexity: bool;
public finishVerts(): void;
public duplicate(): ShapePoly;
public recenter(c): void;
public transform(xf): void;
public untransform(xf): void;
public area(): number;
public centroid(): Vec2;
public inertia(mass: number): number;
public cacheData(xf: Transform): void;
public pointQuery(p: Vec2): bool;
public findVertexByPoint(p: Vec2, minDist: number): number;
public findEdgeByPoint(p: Vec2, minDist: number): number;
public distanceOnPlane(n, d): number;
public containPoint(p): bool;
public containPointPartial(p, n): bool;
}
}
/**
* Phaser - Advanced Physics - ShapeBox
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced {
class ShapeBox extends ShapePoly {
constructor(x, y, width, height);
}
}
/**
* Phaser - Advanced Physics - Shape
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced {
class ShapeSegment extends Shape implements IShape {
constructor(a, b, radius: number);
public a: Vec2;
public b: Vec2;
public radius: number;
public normal: Vec2;
public ta: Vec2;
public tb: Vec2;
public tn: Vec2;
public finishVerts(): void;
public duplicate(): ShapeSegment;
public recenter(c): void;
public transform(xf: Transform): void;
public untransform(xf: Transform): void;
public area(): number;
public centroid(): Vec2;
public inertia(mass: number): number;
public cacheData(xf: Transform): void;
public pointQuery(p: Vec2): bool;
public findVertexByPoint(p: Vec2, minDist: number): number;
public distanceOnPlane(n, d): number;
}
}
/**
* Phaser - Advanced Physics - ShapeTriangle
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced {
class ShapeTriangle extends ShapePoly {
constructor(p1, p2, p3);
}
}
/**
* Phaser - Advanced Physics - Shapes - Box
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced.Shapes {
class Box extends ShapePoly {
class Box extends Poly {
constructor(x, y, width, height);
}
}
/**
* Phaser - Advanced Physics - Shape - Circle
* Phaser - Advanced Physics - Shapes - Triangle
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced.Shapes {
class Circle extends Shape implements IShape {
constructor(radius: number, x?: number, y?: number);
public radius: number;
public center: Vec2;
public tc: Vec2;
public finishVerts(): void;
public duplicate(): ShapeCircle;
public recenter(c: Vec2): void;
public transform(xf: Transform): void;
public untransform(xf: Transform): void;
public area(): number;
public centroid(): Vec2;
public inertia(mass: number): number;
public cacheData(xf: Transform): void;
public pointQuery(p: Vec2): bool;
public findVertexByPoint(p: Vec2, minDist: number): number;
public distanceOnPlane(n, d): void;
class Triangle extends Poly {
constructor(p1, p2, p3);
}
}
/**
* Phaser - Advanced Physics - Shapes - Convex Polygon
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced.Shapes {
class Poly extends Shape implements IShape {
constructor(verts?: Vec2[]);
public verts: Vec2[];
public planes;
public tverts;
public tplanes;
public convexity: bool;
public finishVerts(): void;
public duplicate(): ShapePoly;
public recenter(c): void;
public transform(xf): void;
public untransform(xf): void;
public area(): number;
public centroid(): Vec2;
public inertia(mass: number): number;
public cacheData(xf: Transform): void;
public pointQuery(p: Vec2): bool;
public findVertexByPoint(p: Vec2, minDist: number): number;
public findEdgeByPoint(p: Vec2, minDist: number): number;
public distanceOnPlane(n, d): number;
public containPoint(p): bool;
public containPointPartial(p, n): bool;
}
}
/**
* Phaser - Advanced Physics - Shapes - Segment
*
* Based on the work Ju Hyung Lee started in JS PhyRus.
*/
module Phaser.Physics.Advanced.Shapes {
class Segment extends Shape implements IShape {
constructor(a, b, radius: number);
public a: Vec2;
public b: Vec2;
public radius: number;
public normal: Vec2;
public ta: Vec2;
public tb: Vec2;
public tn: Vec2;
public finishVerts(): void;
public duplicate(): ShapeSegment;
public recenter(c): void;
public transform(xf: Transform): void;
public untransform(xf: Transform): void;
public area(): number;
public centroid(): Vec2;
public inertia(mass: number): number;
public cacheData(xf: Transform): void;
public pointQuery(p: Vec2): bool;
public findVertexByPoint(p: Vec2, minDist: number): number;
public distanceOnPlane(n, d): number;
}
}
interface IPoint {
getDist(): number;
}
module Shapes {
class Point implements IPoint {
public x: number;
public y: number;
constructor(x: number, y: number);
public getDist(): number;
static origin: Point;
}
}
var p: IPoint;
var dist: number;
/**
* Phaser - PixelUtils
*
* A collection of methods useful for manipulating pixels.

File diff suppressed because it is too large Load diff