Skip to content

Commit ec84515

Browse files
committed
After hours and hours of debugging, polygon to polygon collision is now working perfectly :)
1 parent 829a1b0 commit ec84515

19 files changed

Lines changed: 1269 additions & 763 deletions

File tree

Phaser/Phaser.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@
213213
<TypeScriptCompile Include="physics\advanced\shapes\Shape.ts" />
214214
<TypeScriptCompile Include="physics\advanced\shapes\IShape.ts" />
215215
<TypeScriptCompile Include="physics\advanced\shapes\Box.ts" />
216+
<TypeScriptCompile Include="physics\advanced\Plane.ts" />
217+
<Content Include="physics\advanced\Plane.js">
218+
<DependentUpon>Plane.ts</DependentUpon>
219+
</Content>
216220
<Content Include="physics\advanced\shapes\Box.js">
217221
<DependentUpon>Box.ts</DependentUpon>
218222
</Content>

Phaser/math/Transform.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ module Phaser {
2222
this.t = Phaser.Vec2Utils.clone(pos);
2323
this.c = Math.cos(angle);
2424
this.s = Math.sin(angle);
25+
this.angle = angle;
2526

2627
}
2728

2829
public t: Phaser.Vec2;
2930
public c: number;
3031
public s: number;
32+
public angle: number;
33+
34+
public toString() {
35+
36+
return 't=' + this.t.toString() + ' c=' + this.c + ' s=' + this.s + ' a=' + this.angle;
37+
38+
}
3139

3240
public setTo(pos:Phaser.Vec2, angle:number) {
3341

@@ -41,15 +49,21 @@ module Phaser {
4149

4250
public setRotation(angle:number) {
4351

44-
this.c = Math.cos(angle);
45-
this.s = Math.sin(angle);
52+
if (angle !== this.angle)
53+
{
54+
this.c = Math.cos(angle);
55+
this.s = Math.sin(angle);
56+
this.angle = angle;
57+
}
58+
4659
return this;
4760

4861
}
4962

5063
public setPosition(p:Phaser.Vec2) {
5164

5265
this.t.copyFrom(p);
66+
5367
return this;
5468

5569
}

Phaser/math/TransformUtils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ module Phaser {
1414
export class TransformUtils {
1515

1616
public static rotate(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 {
17+
//return new vec2(v.x * this.c - v.y * this.s, v.x * this.s + v.y * this.c);
1718
return out.setTo(v.x * t.c - v.y * t.s, v.x * t.s + v.y * t.c);
1819
}
1920

2021
public static unrotate(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 {
22+
//return new vec2(v.x * this.c + v.y * this.s, -v.x * this.s + v.y * this.c);
2123
return out.setTo(v.x * t.c + v.y * t.s, -v.x * t.s + v.y * t.c);
2224
}
2325

2426
public static transform(t: Transform, v:Phaser.Vec2, out?: Vec2 = new Vec2):Phaser.Vec2 {
27+
//return new vec2(v.x * this.c - v.y * this.s + this.t.x, v.x * this.s + v.y * this.c + this.t.y);
2528
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);
2629
}
2730

@@ -30,6 +33,7 @@ module Phaser {
3033
var px = v.x - t.t.x;
3134
var py = v.y - t.t.y;
3235

36+
//return new vec2(px * this.c + py * this.s, -px * this.s + py * this.c);
3337
return out.setTo(px * t.c + py * t.s, -px * t.s + py * t.c);
3438

3539
}

Phaser/math/Vec2.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ module Phaser {
288288
* @return {string} a string representation of the object.
289289
**/
290290
public toString(): string {
291-
return "[{Vec2 (x=" + this.x + " y=" + this.y + ")}]";
291+
//return "[{Vec2 (x=" + this.x + " y=" + this.y + ")}]";
292+
return "x=" + this.x + " y=" + this.y;
292293
}
293294

294295
}

Phaser/physics/advanced/Body.ts

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ module Phaser.Physics.Advanced {
6969

7070
}
7171

72+
public toString(): string {
73+
return "[{Body (name=" + this.name + " velocity=" + this.velocity.toString() + " angularVelocity: " + this.angularVelocity + ")}]";
74+
}
75+
7276
private _tempVec2: Phaser.Vec2 = new Phaser.Vec2;
7377

7478
/**
@@ -305,25 +309,28 @@ module Phaser.Physics.Advanced {
305309

306310
this.transform.setTo(pos, angle);
307311
// inject the transform into this.position
312+
Manager.write('setTransform: ' + this.position.toString());
313+
Manager.write('centroid: ' + this.centroid.toString());
308314
Phaser.TransformUtils.transform(this.transform, this.centroid, this.position);
315+
Manager.write('post setTransform: ' + this.position.toString());
309316
//this.position.copyFrom(this.transform.transform(this.centroid));
310317
this.angle = angle;
311318

312319
}
313320

314321
public syncTransform() {
315322

323+
Manager.write('syncTransform:');
324+
Manager.write('p: ' + this.position.toString());
325+
Manager.write('centroid: ' + this.centroid.toString());
326+
Manager.write('xf: ' + this.transform.toString());
327+
Manager.write('a: ' + this.angle);
316328
this.transform.setRotation(this.angle);
317-
318-
//var rotc: Phaser.Vec2 = this.transform.rotate(this.centroid);
319-
//var sub: Phaser.Vec2 = Phaser.Vec2Utils.subtract(this.position, rotc);
320-
//this.transform.setPosition(sub);
321-
322-
// this.transform.setPosition(vec2.sub(this.position, this.transform.rotate(this.centroid)));
323-
//Phaser.Vec2Utils.subtract(this.position, this.transform.rotate(this.centroid), this.transform.t);
324-
325329
// OPTIMISE: Creating new vector
326330
Phaser.Vec2Utils.subtract(this.position, Phaser.TransformUtils.rotate(this.transform, this.centroid), this.transform.t);
331+
Manager.write('--------------------');
332+
Manager.write('xf: ' + this.transform.toString());
333+
Manager.write('--------------------');
327334

328335
}
329336

@@ -332,17 +339,17 @@ module Phaser.Physics.Advanced {
332339
return Phaser.TransformUtils.transform(this.transform, p);
333340
}
334341

335-
public getWorldVector(v) {
342+
public getWorldVector(v:Phaser.Vec2) {
336343
// OPTIMISE: Creating new vector
337344
return Phaser.TransformUtils.rotate(this.transform, v);
338345
}
339346

340-
public getLocalPoint(p) {
347+
public getLocalPoint(p:Phaser.Vec2) {
341348
// OPTIMISE: Creating new vector
342349
return Phaser.TransformUtils.untransform(this.transform, p);
343350
}
344351

345-
public getLocalVector(v) {
352+
public getLocalVector(v:Phaser.Vec2) {
346353
// OPTIMISE: Creating new vector
347354
return Phaser.TransformUtils.unrotate(this.transform, v);
348355
}
@@ -430,21 +437,28 @@ module Phaser.Physics.Advanced {
430437
}
431438
}
432439

433-
public cacheData() {
440+
public cacheData(source:string = '') {
434441

435-
//console.log('Body cacheData', this.name, 'len', this.shapes.length);
442+
Manager.write('cacheData -- start');
443+
Manager.write('p: ' + this.position.toString());
444+
Manager.write('xf: ' + this.transform.toString());
436445

437446
this.bounds.clear();
438447

439448
for (var i = 0; i < this.shapes.length; i++)
440449
{
441-
var shape = this.shapes[i];
450+
var shape: IShape = this.shapes[i];
442451
shape.cacheData(this.transform);
443452
this.bounds.addBounds(shape.bounds);
444453
}
445454

446-
}
455+
Manager.write('bounds: ' + this.bounds.toString());
447456

457+
Manager.write('p: ' + this.position.toString());
458+
Manager.write('xf: ' + this.transform.toString());
459+
Manager.write('cacheData -- stop');
460+
461+
}
448462

449463
public updateVelocity(gravity, dt, damping) {
450464

@@ -461,14 +475,44 @@ module Phaser.Physics.Advanced {
461475
// v2 = exp(-c * dt) * v1
462476
// Taylor expansion:
463477
// v2 = (1.0f - c * dt) * v1
464-
this.velocity.scale(this.game.math.clamp(1 - dt * (damping + this.linearDamping), 0, 1));
465-
this.angularVelocity *= this.game.math.clamp(1 - dt * (damping + this.angularDamping), 0, 1);
478+
this.velocity.scale(this.clamp(1 - dt * (damping + this.linearDamping), 0, 1));
479+
this.angularVelocity *= this.clamp(1 - dt * (damping + this.angularDamping), 0, 1);
466480

467481
this.force.setTo(0, 0);
468482
this.torque = 0;
469483

470484
}
471485

486+
public inContact(body2: Body): bool {
487+
488+
if (!body2 || this.stepCount == body2.stepCount)
489+
{
490+
return false;
491+
}
492+
493+
if (!(this.isAwake && this.isStatic == false) && !(body2.isAwake && body2.isStatic == false))
494+
{
495+
return false;
496+
}
497+
498+
if (this.isCollidable(body2) == false)
499+
{
500+
return false;
501+
}
502+
503+
if (!this.bounds.intersectsBounds(body2.bounds))
504+
{
505+
return false;
506+
}
507+
508+
return true;
509+
510+
}
511+
512+
public clamp(v, min, max) {
513+
return v < min ? min : (v > max ? max : v);
514+
}
515+
472516
public updatePosition(dt) {
473517

474518
//console.log('body update pos', this.position.y);
@@ -489,7 +533,7 @@ module Phaser.Physics.Advanced {
489533
this.torque = 0;
490534
}
491535

492-
public applyForce(force, p) {
536+
public applyForce(force:Phaser.Vec2, p:Phaser.Vec2) {
493537

494538
if (this.isDynamic == false)
495539
{
@@ -511,7 +555,7 @@ module Phaser.Physics.Advanced {
511555

512556
}
513557

514-
public applyForceToCenter(force) {
558+
public applyForceToCenter(force:Phaser.Vec2) {
515559

516560
if (this.isDynamic == false)
517561
{
@@ -527,7 +571,7 @@ module Phaser.Physics.Advanced {
527571

528572
}
529573

530-
public applyTorque(torque) {
574+
public applyTorque(torque:number) {
531575

532576
if (this.isDynamic == false)
533577
{
@@ -543,7 +587,7 @@ module Phaser.Physics.Advanced {
543587

544588
}
545589

546-
public applyLinearImpulse(impulse, p) {
590+
public applyLinearImpulse(impulse:Phaser.Vec2, p:Phaser.Vec2) {
547591

548592
if (this.isDynamic == false)
549593
{
@@ -560,6 +604,7 @@ module Phaser.Physics.Advanced {
560604
// this.angularVelocity += vec2.cross(vec2.sub(p, this.position), impulse) * this.inertiaInverted;
561605

562606
Phaser.Vec2Utils.subtract(p, this.position, this._tempVec2);
607+
563608
this.angularVelocity += Phaser.Vec2Utils.cross(this._tempVec2, impulse) * this.inertiaInverted;
564609

565610
}
@@ -611,7 +656,7 @@ module Phaser.Physics.Advanced {
611656

612657
}
613658

614-
public isCollidable(other) {
659+
public isCollidable(other:Body) {
615660

616661
if (this == other)
617662
{
@@ -632,12 +677,7 @@ module Phaser.Physics.Advanced {
632677
{
633678
var joint = this.joints[i];
634679

635-
if (!joint)
636-
{
637-
continue;
638-
}
639-
640-
if (!joint.collideConnected && other.jointHash[joint.id] != undefined)
680+
if (!this.joints[i] || (!this.joints[i].collideConnected && other.jointHash[this.joints[i].id] != undefined))
641681
{
642682
return false;
643683
}

0 commit comments

Comments
 (0)