Skip to content

Commit 829a1b0

Browse files
committed
Brand new physics system finally (mostly) working. Some poly issues to resolve, but it's running well.
1 parent 0a75132 commit 829a1b0

14 files changed

Lines changed: 684 additions & 232 deletions

File tree

Phaser/input/Input.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,30 @@ module Phaser {
5353
this.activePointer = this.mousePointer;
5454
this.currentPointers = 0;
5555

56+
this.hitCanvas = <HTMLCanvasElement> document.createElement('canvas');
57+
this.hitCanvas.width = 1;
58+
this.hitCanvas.height = 1;
59+
this.hitContext = this.hitCanvas.getContext('2d');
60+
5661
}
5762

5863
/**
5964
* Local private reference to game.
6065
*/
6166
private _game: Game;
6267

68+
/**
69+
* A 1x1 sized canvas used for pixel-perfect checks
70+
* @type {HTMLCanvasElement}
71+
*/
72+
public hitCanvas: HTMLCanvasElement;
73+
74+
/**
75+
* The context of the 1x1 pixel check canvas
76+
* @type {CanvasRenderingContext2D}
77+
*/
78+
public hitContext: CanvasRenderingContext2D;
79+
6380
/**
6481
* A vector object representing the previous position of the Pointer.
6582
* @property vector
@@ -961,6 +978,14 @@ module Phaser {
961978
return Vec2Utils.angle(pointer1.position, pointer2.position);
962979
}
963980

981+
public pixelPerfectCheck(sprite: Phaser.Sprite, pointer: Phaser.Pointer, alpha: number = 255): bool {
982+
983+
this.hitContext.clearRect(0, 0, 1, 1);
984+
985+
return true;
986+
987+
}
988+
964989
}
965990

966991
}

Phaser/physics/advanced/Body.ts

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
/// <reference path="Bounds.ts" />
99
/// <reference path="Space.ts" />
1010
/// <reference path="shapes/IShape.ts" />
11+
/// <reference path="shapes/Triangle.ts" />
12+
/// <reference path="shapes/Circle.ts" />
13+
/// <reference path="shapes/Box.ts" />
14+
/// <reference path="shapes/Poly.ts" />
15+
/// <reference path="shapes/Segment.ts" />
1116

1217
/**
1318
* Phaser - Advanced Physics - Body
@@ -29,12 +34,12 @@ module Phaser.Physics.Advanced {
2934
{
3035
this.sprite = sprite;
3136
this.game = sprite.game;
32-
this.position = new Phaser.Vec2(sprite.x, sprite.y);
37+
this.position = new Phaser.Vec2(Phaser.Physics.Advanced.Manager.pixelsToMeters(sprite.x), Phaser.Physics.Advanced.Manager.pixelsToMeters(sprite.y));
3338
this.angle = sprite.rotation;
3439
}
3540
else
3641
{
37-
this.position = new Phaser.Vec2(x, y);
42+
this.position = new Phaser.Vec2(Phaser.Physics.Advanced.Manager.pixelsToMeters(x), Phaser.Physics.Advanced.Manager.pixelsToMeters(y));
3843
this.angle = 0;
3944
}
4045

@@ -64,6 +69,8 @@ module Phaser.Physics.Advanced {
6469

6570
}
6671

72+
private _tempVec2: Phaser.Vec2 = new Phaser.Vec2;
73+
6774
/**
6875
* Reference to Phaser.Game
6976
*/
@@ -139,6 +146,11 @@ module Phaser.Physics.Advanced {
139146
// Bounds of all shapes
140147
public bounds: Bounds;
141148

149+
public mass: number;
150+
public massInverted: number;
151+
public inertia: number;
152+
public inertiaInverted: number;
153+
142154
public fixedRotation = false;
143155
public categoryBits = 0x0001;
144156
public maskBits = 0xFFFF;
@@ -195,6 +207,62 @@ module Phaser.Physics.Advanced {
195207

196208
}
197209

210+
public addPoly(verts, elasticity?: number = 1, friction?: number = 1, density?: number = 1): Phaser.Physics.Advanced.Shapes.Poly {
211+
212+
var poly: Phaser.Physics.Advanced.Shapes.Poly = new Phaser.Physics.Advanced.Shapes.Poly(verts);
213+
poly.elasticity = elasticity;
214+
poly.friction = friction;
215+
poly.density = density;
216+
217+
this.addShape(poly);
218+
this.resetMassData();
219+
220+
return poly;
221+
222+
}
223+
224+
public addTriangle(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, elasticity?: number = 1, friction?: number = 1, density?: number = 1): Phaser.Physics.Advanced.Shapes.Triangle {
225+
226+
var tri: Phaser.Physics.Advanced.Shapes.Triangle = new Phaser.Physics.Advanced.Shapes.Triangle(x1, y1, x2, y2, x3, y3);
227+
tri.elasticity = elasticity;
228+
tri.friction = friction;
229+
tri.density = density;
230+
231+
this.addShape(tri);
232+
this.resetMassData();
233+
234+
return tri;
235+
236+
}
237+
238+
public addBox(x: number, y: number, width: number, height: number, elasticity?: number = 1, friction?: number = 1, density?: number = 1): Phaser.Physics.Advanced.Shapes.Box {
239+
240+
var box: Phaser.Physics.Advanced.Shapes.Box = new Phaser.Physics.Advanced.Shapes.Box(x, y, width, height);
241+
box.elasticity = elasticity;
242+
box.friction = friction;
243+
box.density = density;
244+
245+
this.addShape(box);
246+
this.resetMassData();
247+
248+
return box;
249+
250+
}
251+
252+
public addCircle(radius: number, x?: number = 0, y?: number = 0, elasticity?: number = 1, friction?: number = 1, density?: number = 1): Phaser.Physics.Advanced.Shapes.Circle {
253+
254+
var circle: Phaser.Physics.Advanced.Shapes.Circle = new Phaser.Physics.Advanced.Shapes.Circle(radius, x, y);
255+
circle.elasticity = elasticity;
256+
circle.friction = friction;
257+
circle.density = density;
258+
259+
this.addShape(circle);
260+
this.resetMassData();
261+
262+
return circle;
263+
264+
}
265+
198266
public addShape(shape) {
199267

200268
// Check not already part of this body
@@ -218,10 +286,6 @@ module Phaser.Physics.Advanced {
218286

219287
}
220288

221-
public mass: number;
222-
public massInverted: number;
223-
public inertia: number;
224-
public inertiaInverted: number;
225289

226290
private setMass(mass) {
227291

@@ -381,7 +445,6 @@ module Phaser.Physics.Advanced {
381445

382446
}
383447

384-
private _tempVec2: Phaser.Vec2 = new Phaser.Vec2;
385448

386449
public updateVelocity(gravity, dt, damping) {
387450

Phaser/physics/advanced/Bounds.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ module Phaser.Physics.Advanced {
7171

7272
}
7373

74+
public get x(): number {
75+
return Phaser.Physics.Advanced.Manager.metersToPixels(this.mins.x);
76+
}
77+
78+
public get y(): number {
79+
return Phaser.Physics.Advanced.Manager.metersToPixels(this.mins.y);
80+
}
81+
82+
public get width(): number {
83+
return Phaser.Physics.Advanced.Manager.metersToPixels(this.maxs.x - this.mins.x);
84+
}
85+
86+
public get height(): number {
87+
return Phaser.Physics.Advanced.Manager.metersToPixels(this.maxs.y - this.mins.y);
88+
}
89+
7490
public isEmpty(): bool {
7591
return (this.mins.x > this.maxs.x || this.mins.y > this.maxs.y);
7692
}

Phaser/physics/advanced/Space.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ module Phaser.Physics.Advanced {
560560
//stats.timeCollision = Date.now() - t0;
561561

562562
return newContactSolverArr;
563+
563564
}
564565

565566
public initSolver(dt, dt_inv, warmStarting) {

Phaser/physics/advanced/shapes/Box.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ module Phaser.Physics.Advanced.Shapes {
2626
var hh = height * 0.5;
2727

2828
super([
29-
new Phaser.Vec2(-hw + x, +hh + y),
30-
new Phaser.Vec2(-hw + x, -hh + y),
31-
new Phaser.Vec2(+hw + x, -hh + y),
32-
new Phaser.Vec2(+hw + x, +hh + y)
29+
{ x: -hw + x, y: +hh + y },
30+
{ x: -hw + x, y: -hh + y },
31+
{ x: +hw + x, y: -hh + y },
32+
{ x: +hw + x, y: +hh + y }
3333
]);
3434

3535
}

Phaser/physics/advanced/shapes/Circle.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ module Phaser.Physics.Advanced.Shapes {
1818

1919
super(Manager.SHAPE_TYPE_CIRCLE);
2020

21+
x = Manager.pixelsToMeters(x);
22+
y = Manager.pixelsToMeters(y);
23+
radius = Manager.pixelsToMeters(radius);
24+
2125
this.center = new Phaser.Vec2(x, y);
2226
this.radius = radius;
2327
this.tc = new Phaser.Vec2;

Phaser/physics/advanced/shapes/Poly.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module Phaser.Physics.Advanced.Shapes {
1414

1515
export class Poly extends Phaser.Physics.Advanced.Shape implements IShape {
1616

17-
constructor(verts?:Phaser.Vec2[]) {
17+
// Verts is an optional array of objects, the objects must have public x and y properties which will be used
18+
// to seed this polygon (i.e. Vec2 objects, or just straight JS objects) and must wind COUNTER clockwise
19+
constructor(verts?) {
1820

1921
super(Manager.SHAPE_TYPE_POLY);
2022

@@ -28,7 +30,7 @@ module Phaser.Physics.Advanced.Shapes {
2830
{
2931
for (var i = 0; i < verts.length; i++)
3032
{
31-
this.verts[i] = Phaser.Vec2Utils.clone(verts[i]);
33+
this.verts[i] = new Phaser.Vec2(verts[i].x, verts[i].y);
3234
this.tverts[i] = this.verts[i];
3335

3436
this.tplanes[i] = {};
@@ -91,6 +93,7 @@ module Phaser.Physics.Advanced.Shapes {
9193
this.convexity = false;
9294
}
9395
}
96+
9497
}
9598

9699
public duplicate() {
@@ -138,7 +141,7 @@ module Phaser.Physics.Advanced.Shapes {
138141

139142
var numVerts = this.verts.length;
140143

141-
//console.log('shapePoly cacheData', numVerts);
144+
console.log('Poly cacheData', numVerts, this.body.name);
142145

143146
if (numVerts == 0)
144147
{

Phaser/physics/advanced/shapes/Triangle.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ module Phaser.Physics.Advanced.Shapes {
1414

1515
export class Triangle extends Phaser.Physics.Advanced.Shapes.Poly {
1616

17-
constructor(p1, p2, p3) {
17+
constructor(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) {
1818

19-
super( [ new Phaser.Vec2(p1.x, p1.y), new Phaser.Vec2(p2.x, p2.y), new Phaser.Vec2(p3.x, p3.y) ] );
19+
x1 = Manager.pixelsToMeters(x1);
20+
y1 = Manager.pixelsToMeters(y1);
21+
x2 = Manager.pixelsToMeters(x2);
22+
y2 = Manager.pixelsToMeters(y2);
23+
x3 = Manager.pixelsToMeters(x3);
24+
y3 = Manager.pixelsToMeters(y3);
25+
26+
super([{ x: x1, y: y1 }, { x: x2, y: y2 }, { x: x3, y: y3 }]);
2027

2128
}
2229

Tests/assets/sprites/cokecan.png

1.35 KB
Loading

0 commit comments

Comments
 (0)