Skip to content

Commit 114d1d2

Browse files
committed
Compiling, but not yet running correctly. Need to add body renderer next.
1 parent 232c96c commit 114d1d2

10 files changed

Lines changed: 376 additions & 61 deletions

File tree

Phaser/physics/advanced/Body.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ module Phaser.Physics.Advanced {
1717

1818
export class Body {
1919

20-
constructor(sprite: Phaser.Sprite, type: number) {
21-
22-
this.sprite = sprite;
23-
this.game = sprite.game;
20+
constructor(sprite: Phaser.Sprite, type: number, x?: number = 0, y?: number = 0) {
2421

2522
this.id = Phaser.Physics.Advanced.Manager.bodyCounter++;
2623
this.name = 'body' + this.id;
2724
this.type = type;
2825

29-
this.position = new Phaser.Vec2(sprite.x, sprite.y);
30-
this.angle = sprite.rotation;
26+
if (sprite)
27+
{
28+
this.sprite = sprite;
29+
this.game = sprite.game;
30+
this.position = new Phaser.Vec2(sprite.x, sprite.y);
31+
this.angle = sprite.rotation;
32+
}
33+
else
34+
{
35+
this.position = new Phaser.Vec2(x, y);
36+
this.angle = 0;
37+
}
3138

3239
this.transform = new Phaser.Transform(this.position, this.angle);
3340
this.centroid = new Phaser.Vec2;
@@ -136,8 +143,22 @@ module Phaser.Physics.Advanced {
136143
public stepCount = 0;
137144
public space: Space;
138145

139-
// duplicate = Util function
140-
// serialize = Util function
146+
/*
147+
public duplicate() {
148+
149+
var body = new Body(this.type, this.transform.t, this.angle);
150+
151+
for (var i = 0; i < this.shapes.length; i++)
152+
{
153+
body.addShape(this.shapes[i].duplicate());
154+
}
155+
156+
body.resetMassData();
157+
158+
return body;
159+
160+
}
161+
*/
141162

142163
public get isDisabled(): bool {
143164
return this.type == Phaser.Types.BODY_DISABLED ? true : false;
@@ -274,6 +295,8 @@ module Phaser.Physics.Advanced {
274295
var mass = shape.area() * shape.density;
275296
var inertia = shape.inertia(mass);
276297

298+
console.log('rmd', centroid, shape);
299+
277300
totalMassCentroid.multiplyAddByScalar(centroid, mass);
278301
totalMass += mass;
279302
totalInertia += inertia;
@@ -335,7 +358,7 @@ module Phaser.Physics.Advanced {
335358

336359
}
337360

338-
private _tempVec2: Phaser.Vec2;
361+
private _tempVec2: Phaser.Vec2 = new Phaser.Vec2;
339362

340363
public updateVelocity(gravity, dt, damping) {
341364

Phaser/physics/advanced/Manager.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module Phaser.Physics.Advanced {
1717

1818
this.game = game;
1919

20+
this.space = new Space();
21+
2022
Manager.collision = new Collision();
2123

2224
}
@@ -59,6 +61,92 @@ module Phaser.Physics.Advanced {
5961
public static bodyCounter: number = 0;
6062
public static jointCounter: number = 0;
6163
public static shapeCounter: number = 0;
64+
65+
public space: Space;
66+
public lastTime: number = 0;
67+
public frameRateHz: number = 60;
68+
public timeDelta: number = 0;
69+
public paused: bool = false;
70+
public step: bool = false; // step through the simulation (i.e. per click)
71+
public velocityIterations: number = 8;
72+
public positionIterations: number = 4;
73+
public allowSleep: bool = true;
74+
public warmStarting: bool = true;
75+
76+
public update() {
77+
78+
var time = Date.now();
79+
var frameTime = (time - this.lastTime) / 1000;
80+
this.lastTime = time;
81+
82+
// if rAf - why?
83+
frameTime = Math.floor(frameTime * 60 + 0.5) / 60;
84+
85+
//if (!mouseDown)
86+
//{
87+
// var p = canvasToWorld(mousePosition);
88+
// var body = space.findBodyByPoint(p);
89+
// //domCanvas.style.cursor = body ? "pointer" : "default";
90+
//}
91+
92+
if (!this.paused || this.step)
93+
{
94+
var h = 1 / this.frameRateHz;
95+
96+
this.timeDelta += frameTime;
97+
98+
if (this.step)
99+
{
100+
this.step = false;
101+
this.timeDelta = h;
102+
}
103+
104+
for (var maxSteps = 4; maxSteps > 0 && this.timeDelta >= h; maxSteps--)
105+
{
106+
this.space.step(h, this.velocityIterations, this.positionIterations, this.warmStarting, this.allowSleep);
107+
this.timeDelta -= h;
108+
}
109+
110+
if (this.timeDelta > h)
111+
{
112+
this.timeDelta = 0;
113+
}
114+
115+
//if (sceneIndex < demoArr.length)
116+
//{
117+
// demo = demoArr[sceneIndex];
118+
// demo.runFrame();
119+
//}
120+
}
121+
122+
//frameCount++;
123+
124+
}
125+
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
62150

63151
public static pixelsToMeters(value: number): number {
64152
return value * 0.02;
@@ -81,7 +169,7 @@ module Phaser.Physics.Advanced {
81169
}
82170

83171
public static inertiaForCircle(mass, center, radius_outer, radius_inner) {
84-
return mass * ((radius_outer * radius_outer + radius_inner * radius_inner) * 0.5 + center.lengthsq());
172+
return mass * ((radius_outer * radius_outer + radius_inner * radius_inner) * 0.5 + center.lengthSq());
85173
}
86174

87175
public static areaForSegment(a, b, radius) {

Phaser/physics/advanced/Shape.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/// <reference path="../../math/Vec2Utils.ts" />
44
/// <reference path="Manager.ts" />
55
/// <reference path="Body.ts" />
6+
/// <reference path="Bounds.ts" />
67

78
/**
89
* Phaser - Advanced Physics - Shape
@@ -23,7 +24,7 @@ module Phaser.Physics.Advanced {
2324
this.friction = 1.0;
2425
this.density = 1;
2526

26-
//this.bounds = new Bounds;
27+
this.bounds = new Bounds;
2728

2829
}
2930

@@ -40,7 +41,7 @@ module Phaser.Physics.Advanced {
4041
public density: number;
4142

4243
// Axis-aligned bounding box
43-
public bounds;
44+
public bounds: Bounds;
4445

4546
}
4647

Phaser/physics/advanced/ShapeCircle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module Phaser.Physics.Advanced {
5656
}
5757

5858
public centroid() {
59-
//return this.center.duplicate();
59+
return Phaser.Vec2Utils.clone(this.center);
6060
}
6161

6262
public inertia(mass) {
@@ -66,8 +66,8 @@ module Phaser.Physics.Advanced {
6666

6767
public cacheData(xf) {
6868
this.tc = xf.transform(this.center);
69-
this.bounds.mins.set(this.tc.x - this.radius, this.tc.y - this.radius);
70-
this.bounds.maxs.set(this.tc.x + this.radius, this.tc.y + this.radius);
69+
this.bounds.mins.setTo(this.tc.x - this.radius, this.tc.y - this.radius);
70+
this.bounds.maxs.setTo(this.tc.x + this.radius, this.tc.y + this.radius);
7171
}
7272

7373
public pointQuery(p) {

Phaser/physics/advanced/ShapeSegment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ module Phaser.Physics.Advanced {
116116
t = this.ta.y;
117117
}
118118

119-
this.bounds.mins.set(l - this.radius, b - this.radius);
120-
this.bounds.maxs.set(r + this.radius, t + this.radius);
119+
this.bounds.mins.setTo(l - this.radius, b - this.radius);
120+
this.bounds.maxs.setTo(r + this.radius, t + this.radius);
121121

122122
}
123123

0 commit comments

Comments
 (0)