Skip to content

Commit 4c21ac0

Browse files
committed
Tilemap collision is now working but all Camera following seems to be broken as a result. Awesome.
1 parent db9b8ec commit 4c21ac0

15 files changed

Lines changed: 11718 additions & 4040 deletions

File tree

Phaser/Collision.ts

Lines changed: 46 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ module Phaser {
9292
*/
9393
public static OVERLAP_BIAS: number = 4;
9494

95+
/**
96+
* This holds the result of the tile separation check, true if the object was moved, otherwise false
97+
* @type {boolean}
98+
*/
99+
public static TILE_OVERLAP: bool = false;
100+
101+
/**
102+
* A temporary Quad used in the separation process to help avoid gc spikes
103+
* @type {Quad}
104+
*/
105+
public static _tempBounds: Quad;
95106

96107
/**
97108
* Checks for Line to Line intersection and returns an IntersectResult object containing the results of the intersection.
@@ -634,10 +645,10 @@ module Phaser {
634645
* @param tile The Tile to separate
635646
* @returns {boolean} Whether the objects in fact touched and were separated
636647
*/
637-
public static separateTile(object:GameObject, tile): bool {
648+
public static separateTile(object:GameObject, x: number, y: number, width: number, height: number, mass: number, collideLeft: bool, collideRight: bool, collideUp: bool, collideDown: bool): bool {
638649

639-
var separatedX: bool = Collision.separateTileX(object, tile);
640-
var separatedY: bool = Collision.separateTileY(object, tile);
650+
var separatedX: bool = Collision.separateTileX(object, x, y, width, height, mass, collideLeft, collideRight);
651+
var separatedY: bool = Collision.separateTileY(object, x, y, width, height, mass, collideUp, collideDown);
641652

642653
return separatedX || separatedY;
643654

@@ -649,37 +660,34 @@ module Phaser {
649660
* @param tile The Tile to separate
650661
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
651662
*/
652-
public static separateTileX(object, tile): bool {
663+
public static separateTileX(object:GameObject, x: number, y: number, width: number, height: number, mass: number, collideLeft: bool, collideRight: bool): bool {
653664

654-
// Can't separate two immovable objects
655-
if (object.immovable && tile.immovable)
665+
// Can't separate two immovable objects (tiles are always immovable)
666+
if (object.immovable)
656667
{
657668
return false;
658669
}
659670

660-
// First, get the two object deltas
671+
// First, get the object delta
661672
var overlap: number = 0;
662673
var objDelta: number = object.x - object.last.x;
663-
var tileDelta: number = 0;
664674

665-
if (objDelta != tileDelta)
675+
if (objDelta != 0)
666676
{
667677
// Check if the X hulls actually overlap
668678
var objDeltaAbs: number = (objDelta > 0) ? objDelta : -objDelta;
669-
var tileDeltaAbs: number = (tileDelta > 0) ? tileDelta : -tileDelta;
670679
var objBounds: Quad = new Quad(object.x - ((objDelta > 0) ? objDelta : 0), object.last.y, object.width + ((objDelta > 0) ? objDelta : -objDelta), object.height);
671-
var tileBounds: Quad = new Quad(tile.x - ((tileDelta > 0) ? tileDelta : 0), tile.y, tile.width + ((tileDelta > 0) ? tileDelta : -tileDelta), tile.height);
672680

673-
if ((objBounds.x + objBounds.width > tileBounds.x) && (objBounds.x < tileBounds.x + tileBounds.width) && (objBounds.y + objBounds.height > tileBounds.y) && (objBounds.y < tileBounds.y + tileBounds.height))
681+
if ((objBounds.x + objBounds.width > x) && (objBounds.x < x + width) && (objBounds.y + objBounds.height > y) && (objBounds.y < y + height))
674682
{
675-
var maxOverlap: number = objDeltaAbs + tileDeltaAbs + Collision.OVERLAP_BIAS;
683+
var maxOverlap: number = objDeltaAbs + Collision.OVERLAP_BIAS;
676684

677685
// If they did overlap (and can), figure out by how much and flip the corresponding flags
678-
if (objDelta > tileDelta)
686+
if (objDelta > 0)
679687
{
680-
overlap = object.x + object.width - tile.x;
688+
overlap = object.x + object.width - x;
681689

682-
if ((overlap > maxOverlap) || !(object.allowCollisions & Collision.RIGHT) || !(tile.allowCollisions & Collision.LEFT))
690+
if ((overlap > maxOverlap) || !(object.allowCollisions & Collision.RIGHT) || collideLeft == false)
683691
{
684692
overlap = 0;
685693
}
@@ -688,11 +696,11 @@ module Phaser {
688696
object.touching |= Collision.RIGHT;
689697
}
690698
}
691-
else if (objDelta < tileDelta)
699+
else if (objDelta < 0)
692700
{
693-
overlap = object.x - tile.width - tile.x;
701+
overlap = object.x - width - x;
694702

695-
if ((-overlap > maxOverlap) || !(object.allowCollisions & Collision.LEFT) || !(tile.allowCollisions & Collision.RIGHT))
703+
if ((-overlap > maxOverlap) || !(object.allowCollisions & Collision.LEFT) || collideRight == false)
696704
{
697705
overlap = 0;
698706
}
@@ -709,26 +717,9 @@ module Phaser {
709717
// Then adjust their positions and velocities accordingly (if there was any overlap)
710718
if (overlap != 0)
711719
{
712-
var objVelocity: number = object.velocity.x;
713-
var tileVelocity: number = 0;
714-
715-
if (!object.immovable && !tile.immovable)
716-
{
717-
overlap *= 0.5;
718-
object.x = object.x - overlap;
719-
720-
var objNewVelocity: number = Math.sqrt((tileVelocity * tileVelocity * tile.mass) / object.mass) * ((tileVelocity > 0) ? 1 : -1);
721-
var tileNewVelocity: number = Math.sqrt((objVelocity * objVelocity * object.mass) / tile.mass) * ((objVelocity > 0) ? 1 : -1);
722-
var average: number = (objNewVelocity + tileNewVelocity) * 0.5;
723-
objNewVelocity -= average;
724-
object.velocity.x = average + objNewVelocity * object.elasticity;
725-
}
726-
else if (!object.immovable)
727-
{
728-
object.x = object.x - overlap;
729-
object.velocity.x = tileVelocity - objVelocity * object.elasticity;
730-
}
731-
720+
object.x = object.x - overlap;
721+
object.velocity.x = -(object.velocity.x * object.elasticity);
722+
Collision.TILE_OVERLAP = true;
732723
return true;
733724
}
734725
else
@@ -744,57 +735,53 @@ module Phaser {
744735
* @param tile The second GameObject to separate
745736
* @returns {boolean} Whether the objects in fact touched and were separated along the Y axis.
746737
*/
747-
public static separateTileY(object, tile): bool {
738+
public static separateTileY(object: GameObject, x: number, y: number, width: number, height: number, mass: number, collideUp: bool, collideDown: bool): bool {
748739

749-
// Can't separate two immovable objects
750-
if (object.immovable && tile.immovable) {
740+
// Can't separate two immovable objects (tiles are always immovable)
741+
if (object.immovable)
742+
{
751743
return false;
752744
}
753745

754746
// First, get the two object deltas
755747
var overlap: number = 0;
756748
var objDelta: number = object.y - object.last.y;
757-
var tileDelta: number = 0;
758749

759-
if (objDelta != tileDelta)
750+
if (objDelta != 0)
760751
{
761752
// Check if the Y hulls actually overlap
762753
var objDeltaAbs: number = (objDelta > 0) ? objDelta : -objDelta;
763-
var tileDeltaAbs: number = (tileDelta > 0) ? tileDelta : -tileDelta;
764754
var objBounds: Quad = new Quad(object.x, object.y - ((objDelta > 0) ? objDelta : 0), object.width, object.height + objDeltaAbs);
765-
var tileBounds: Quad = new Quad(tile.x, tile.y - ((tileDelta > 0) ? tileDelta : 0), tile.width, tile.height + tileDeltaAbs);
766755

767-
if ((objBounds.x + objBounds.width > tileBounds.x) && (objBounds.x < tileBounds.x + tileBounds.width) && (objBounds.y + objBounds.height > tileBounds.y) && (objBounds.y < tileBounds.y + tileBounds.height))
756+
if ((objBounds.x + objBounds.width > x) && (objBounds.x < x + width) && (objBounds.y + objBounds.height > y) && (objBounds.y < y + height))
768757
{
769-
var maxOverlap: number = objDeltaAbs + tileDeltaAbs + Collision.OVERLAP_BIAS;
758+
var maxOverlap: number = objDeltaAbs + Collision.OVERLAP_BIAS;
770759

771760
// If they did overlap (and can), figure out by how much and flip the corresponding flags
772-
if (objDelta > tileDelta)
761+
if (objDelta > 0)
773762
{
774-
overlap = object.y + object.height - tile.y;
763+
overlap = object.y + object.height - y;
775764

776-
if ((overlap > maxOverlap) || !(object.allowCollisions & Collision.DOWN) || !(tile.allowCollisions & Collision.UP))
765+
if ((overlap > maxOverlap) || !(object.allowCollisions & Collision.DOWN) || collideUp == false)
777766
{
778767
overlap = 0;
779768
}
780769
else
781770
{
782771
object.touching |= Collision.DOWN;
783-
//tile.touching |= Collision.UP;
784772
}
785773
}
786-
else if (objDelta < tileDelta)
774+
else if (objDelta < 0)
787775
{
788-
overlap = object.y - tile.height - tile.y;
776+
overlap = object.y - height - y;
789777

790-
if ((-overlap > maxOverlap) || !(object.allowCollisions & Collision.UP) || !(tile.allowCollisions & Collision.DOWN))
778+
if ((-overlap > maxOverlap) || !(object.allowCollisions & Collision.UP) || collideDown == false)
791779
{
792780
overlap = 0;
793781
}
794782
else
795783
{
796784
object.touching |= Collision.UP;
797-
//tile.touching |= Collision.DOWN;
798785
}
799786
}
800787
}
@@ -805,35 +792,9 @@ module Phaser {
805792
// Then adjust their positions and velocities accordingly (if there was any overlap)
806793
if (overlap != 0)
807794
{
808-
var objVelocity: number = object.velocity.y;
809-
var tileVelocity: number = 0;
810-
811-
if (!object.immovable && !tile.immovable)
812-
{
813-
overlap *= 0.5;
814-
object.y = object.y - overlap;
815-
//tile.y += overlap;
816-
817-
var objNewVelocity: number = Math.sqrt((tileVelocity * tileVelocity * tile.mass) / object.mass) * ((tileVelocity > 0) ? 1 : -1);
818-
var tileNewVelocity: number = Math.sqrt((objVelocity * objVelocity * object.mass) / tile.mass) * ((objVelocity > 0) ? 1 : -1);
819-
var average: number = (objNewVelocity + tileNewVelocity) * 0.5;
820-
objNewVelocity -= average;
821-
//tileNewVelocity -= average;
822-
object.velocity.y = average + objNewVelocity * object.elasticity;
823-
//tile.velocity.y = average + tileNewVelocity * tile.elasticity;
824-
}
825-
else if (!object.immovable)
826-
{
827-
//console.log('y sep', overlap, object.y);
828-
object.y = object.y - overlap;
829-
object.velocity.y = tileVelocity - objVelocity * object.elasticity;
830-
// This is special case code that handles things like horizontal moving platforms you can ride
831-
if (tile.active && tile.moves && (objDelta > tileDelta))
832-
{
833-
//object.x += tile.x - tile.x;
834-
}
835-
}
836-
795+
object.y = object.y - overlap;
796+
object.velocity.y = -(object.velocity.y * object.elasticity);
797+
Collision.TILE_OVERLAP = true;
837798
return true;
838799
}
839800
else

Phaser/Group.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ module Phaser {
474474

475475
}
476476

477-
public forEachAlive(callback, recursive: bool = false) {
477+
public forEachAlive(context, callback, recursive: bool = false) {
478478

479479
var basic;
480480
var i: number = 0;
@@ -487,11 +487,11 @@ module Phaser {
487487
{
488488
if (recursive && (basic.isGroup == true))
489489
{
490-
basic.forEachAlive(callback, true);
490+
basic.forEachAlive(context, callback, true);
491491
}
492492
else
493493
{
494-
callback.call(this, basic);
494+
callback.call(context, basic);
495495
}
496496
}
497497
}

Phaser/gameobjects/Emitter.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ module Phaser {
174174
*
175175
* @return This Emitter instance (nice for chaining stuff together, if you're into that).
176176
*/
177-
public makeParticles(Graphics, Quantity: number = 50, BakedRotations: number = 16, Multiple: bool = false, Collide: number = 0.8): Emitter {
177+
public makeParticles(Graphics, Quantity: number = 50, BakedRotations: number = 16, Multiple: bool = false, Collide: number = 0): Emitter {
178178

179179
this.maxSize = Quantity;
180180

@@ -236,8 +236,10 @@ module Phaser {
236236

237237
if (Collide > 0)
238238
{
239-
particle.width *= Collide;
240-
particle.height *= Collide;
239+
particle.allowCollisions = Collision.ANY;
240+
particle.elasticity = Collide;
241+
//particle.width *= Collide;
242+
//particle.height *= Collide;
241243
//particle.centerOffsets();
242244
}
243245
else

Phaser/gameobjects/GameObject.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ module Phaser {
2929
this.last = new MicroPoint(x, y);
3030
this.origin = new MicroPoint(this.bounds.halfWidth, this.bounds.halfHeight);
3131
this.align = GameObject.ALIGN_TOP_LEFT;
32-
this.mass = 1.0;
33-
this.elasticity = 0.0;
32+
this.mass = 1;
33+
this.elasticity = 0;
3434
this.health = 1;
3535
this.immovable = false;
3636
this.moves = true;
@@ -52,7 +52,7 @@ module Phaser {
5252
this.maxAngular = 10000;
5353

5454
this.cameraBlacklist = [];
55-
this.scrollFactor = new MicroPoint(1.0, 1.0);
55+
this.scrollFactor = new MicroPoint(1, 1);
5656

5757
}
5858

@@ -245,17 +245,6 @@ module Phaser {
245245

246246
}
247247

248-
/*
249-
if (typeof ObjectOrGroup === 'Tilemap')
250-
{
251-
//Since tilemap's have to be the caller, not the target, to do proper tile-based collisions,
252-
// we redirect the call to the tilemap overlap here.
253-
return ObjectOrGroup.overlaps(this, InScreenSpace, Camera);
254-
}
255-
*/
256-
257-
//var object: GameObject = ObjectOrGroup;
258-
259248
if (!InScreenSpace)
260249
{
261250
return (ObjectOrGroup.x + ObjectOrGroup.width > this.x) && (ObjectOrGroup.x < this.x + this.width) &&
@@ -308,20 +297,6 @@ module Phaser {
308297
return results;
309298
}
310299

311-
/*
312-
if (typeof ObjectOrGroup === 'Tilemap')
313-
{
314-
//Since tilemap's have to be the caller, not the target, to do proper tile-based collisions,
315-
// we redirect the call to the tilemap overlap here.
316-
//However, since this is overlapsAt(), we also have to invent the appropriate position for the tilemap.
317-
//So we calculate the offset between the player and the requested position, and subtract that from the tilemap.
318-
var tilemap: Tilemap = ObjectOrGroup;
319-
return tilemap.overlapsAt(tilemap.x - (X - this.x), tilemap.y - (Y - this.y), this, InScreenSpace, Camera);
320-
}
321-
*/
322-
323-
//var object: GameObject = ObjectOrGroup;
324-
325300
if (!InScreenSpace)
326301
{
327302
return (ObjectOrGroup.x + ObjectOrGroup.width > X) && (ObjectOrGroup.x < X + this.width) &&

Phaser/gameobjects/Particle.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module Phaser {
4343
* be dead yet, and then has some special bounce behavior if there is some gravity on it.
4444
*/
4545
public update() {
46+
4647
//lifespan behavior
4748
if (this.lifespan <= 0)
4849
{

0 commit comments

Comments
 (0)