Skip to content

Commit 0b2d818

Browse files
committed
CollisionMask up and running
1 parent 05cc32f commit 0b2d818

42 files changed

Lines changed: 3205 additions & 1360 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Phaser/AnimationManager.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ module Phaser {
200200
if (this.currentAnim && this.currentAnim.update() == true)
201201
{
202202
this.currentFrame = this.currentAnim.currentFrame;
203-
this._parent.bounds.width = this.currentFrame.width;
204-
this._parent.bounds.height = this.currentFrame.height;
203+
this._parent.frameBounds.width = this.currentFrame.width;
204+
this._parent.frameBounds.height = this.currentFrame.height;
205205
}
206206

207207
}
@@ -224,8 +224,8 @@ module Phaser {
224224
{
225225
this.currentFrame = this._frameData.getFrame(value);
226226

227-
this._parent.bounds.width = this.currentFrame.width;
228-
this._parent.bounds.height = this.currentFrame.height;
227+
this._parent.frameBounds.width = this.currentFrame.width;
228+
this._parent.frameBounds.height = this.currentFrame.height;
229229
this._frameIndex = value;
230230
}
231231

@@ -241,8 +241,8 @@ module Phaser {
241241
{
242242
this.currentFrame = this._frameData.getFrameByName(value);
243243

244-
this._parent.bounds.width = this.currentFrame.width;
245-
this._parent.bounds.height = this.currentFrame.height;
244+
this._parent.frameBounds.width = this.currentFrame.width;
245+
this._parent.frameBounds.height = this.currentFrame.height;
246246
this._frameIndex = this.currentFrame.index;
247247
}
248248

Phaser/Collision.ts

Lines changed: 231 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,20 @@ module Phaser {
553553

554554
}
555555

556+
/*
557+
public static circleToQuad(circle: Circle, quad: Quad): bool {
558+
559+
// Check if the center of the circle is within the Quad
560+
if (quad.contains(circle.x, circle.y))
561+
{
562+
return true;
563+
}
564+
565+
// Failing that let's check each line of the quad against the circle
566+
return false;
567+
}
568+
*/
569+
556570
/**
557571
* Checks if the Circle object intersects with the Rectangle and returns the result in an IntersectResult object.
558572
* @param circle The Circle object to check
@@ -594,9 +608,10 @@ module Phaser {
594608
* @param object2 The second GameObject or Group to check.
595609
* @param notifyCallback A callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you passed them to Collision.overlap.
596610
* @param processCallback A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then notifyCallback will only be called if processCallback returns true.
611+
* @param context The context in which the callbacks will be called
597612
* @returns {boolean} true if the objects overlap, otherwise false.
598613
*/
599-
public overlap(object1: Basic = null, object2: Basic = null, notifyCallback = null, processCallback = null): bool {
614+
public overlap(object1: Basic = null, object2: Basic = null, notifyCallback = null, processCallback = null, context = null): bool {
600615

601616
if (object1 == null)
602617
{
@@ -612,7 +627,7 @@ module Phaser {
612627

613628
var quadTree: QuadTree = new QuadTree(this._game.world.bounds.x, this._game.world.bounds.y, this._game.world.bounds.width, this._game.world.bounds.height);
614629

615-
quadTree.load(object1, object2, notifyCallback, processCallback);
630+
quadTree.load(object1, object2, notifyCallback, processCallback, context);
616631

617632
var result: bool = quadTree.execute();
618633

@@ -632,6 +647,11 @@ module Phaser {
632647
*/
633648
public static separate(object1, object2): bool {
634649

650+
console.log('sep o');
651+
652+
object1.collisionMask.update();
653+
object2.collisionMask.update();
654+
635655
var separatedX: bool = Collision.separateX(object1, object2);
636656
var separatedY: bool = Collision.separateY(object1, object2);
637657

@@ -825,6 +845,207 @@ module Phaser {
825845
return false;
826846
}
827847

848+
// First, get the two object deltas
849+
var overlap: number = 0;
850+
851+
if (object1.collisionMask.deltaX != object2.collisionMask.deltaX)
852+
{
853+
if (object1.collisionMask.intersects(object2.collisionMask))
854+
{
855+
var maxOverlap: number = object1.collisionMask.deltaXAbs + object2.collisionMask.deltaXAbs + Collision.OVERLAP_BIAS;
856+
857+
// If they did overlap (and can), figure out by how much and flip the corresponding flags
858+
if (object1.collisionMask.deltaX > object2.collisionMask.deltaX)
859+
{
860+
overlap = object1.collisionMask.right - object2.collisionMask.x;
861+
862+
if ((overlap > maxOverlap) || !(object1.allowCollisions & Collision.RIGHT) || !(object2.allowCollisions & Collision.LEFT))
863+
{
864+
overlap = 0;
865+
}
866+
else
867+
{
868+
object1.touching |= Collision.RIGHT;
869+
object2.touching |= Collision.LEFT;
870+
}
871+
}
872+
else if (object1.collisionMask.deltaX < object2.collisionMask.deltaX)
873+
{
874+
overlap = object1.collisionMask.x - object2.collisionMask.width - object2.collisionMask.x;
875+
876+
if ((-overlap > maxOverlap) || !(object1.allowCollisions & Collision.LEFT) || !(object2.allowCollisions & Collision.RIGHT))
877+
{
878+
overlap = 0;
879+
}
880+
else
881+
{
882+
object1.touching |= Collision.LEFT;
883+
object2.touching |= Collision.RIGHT;
884+
}
885+
886+
}
887+
888+
}
889+
}
890+
891+
// Then adjust their positions and velocities accordingly (if there was any overlap)
892+
if (overlap != 0)
893+
{
894+
var obj1Velocity: number = object1.velocity.x;
895+
var obj2Velocity: number = object2.velocity.x;
896+
897+
if (!object1.immovable && !object2.immovable)
898+
{
899+
overlap *= 0.5;
900+
object1.x = object1.x - overlap;
901+
object2.x += overlap;
902+
903+
var obj1NewVelocity: number = Math.sqrt((obj2Velocity * obj2Velocity * object2.mass) / object1.mass) * ((obj2Velocity > 0) ? 1 : -1);
904+
var obj2NewVelocity: number = Math.sqrt((obj1Velocity * obj1Velocity * object1.mass) / object2.mass) * ((obj1Velocity > 0) ? 1 : -1);
905+
var average: number = (obj1NewVelocity + obj2NewVelocity) * 0.5;
906+
obj1NewVelocity -= average;
907+
obj2NewVelocity -= average;
908+
object1.velocity.x = average + obj1NewVelocity * object1.elasticity;
909+
object2.velocity.x = average + obj2NewVelocity * object2.elasticity;
910+
}
911+
else if (!object1.immovable)
912+
{
913+
object1.x = object1.x - overlap;
914+
object1.velocity.x = obj2Velocity - obj1Velocity * object1.elasticity;
915+
}
916+
else if (!object2.immovable)
917+
{
918+
object2.x += overlap;
919+
object2.velocity.x = obj1Velocity - obj2Velocity * object2.elasticity;
920+
}
921+
922+
return true;
923+
}
924+
else
925+
{
926+
return false;
927+
}
928+
929+
}
930+
931+
/**
932+
* Separates the two objects on their y axis
933+
* @param object1 The first GameObject to separate
934+
* @param object2 The second GameObject to separate
935+
* @returns {boolean} Whether the objects in fact touched and were separated along the Y axis.
936+
*/
937+
public static separateY(object1, object2): bool {
938+
939+
// Can't separate two immovable objects
940+
if (object1.immovable && object2.immovable) {
941+
return false;
942+
}
943+
944+
// First, get the two object deltas
945+
var overlap: number = 0;
946+
947+
if (object1.collisionMask.deltaY != object2.collisionMask.deltaY)
948+
{
949+
if (object1.collisionMask.intersects(object2.collisionMask))
950+
{
951+
// This is the only place to use the DeltaAbs values
952+
var maxOverlap: number = object1.collisionMask.deltaYAbs + object2.collisionMask.deltaYAbs + Collision.OVERLAP_BIAS;
953+
954+
// If they did overlap (and can), figure out by how much and flip the corresponding flags
955+
if (object1.collisionMask.deltaY > object2.collisionMask.deltaY)
956+
{
957+
overlap = object1.collisionMask.bottom - object2.collisionMask.y;
958+
959+
if ((overlap > maxOverlap) || !(object1.allowCollisions & Collision.DOWN) || !(object2.allowCollisions & Collision.UP))
960+
{
961+
overlap = 0;
962+
}
963+
else
964+
{
965+
object1.touching |= Collision.DOWN;
966+
object2.touching |= Collision.UP;
967+
}
968+
}
969+
else if (object1.collisionMask.deltaY < object2.collisionMask.deltaY)
970+
{
971+
overlap = object1.collisionMask.y - object2.collisionMask.height - object2.collisionMask.y;
972+
973+
if ((-overlap > maxOverlap) || !(object1.allowCollisions & Collision.UP) || !(object2.allowCollisions & Collision.DOWN))
974+
{
975+
overlap = 0;
976+
}
977+
else
978+
{
979+
object1.touching |= Collision.UP;
980+
object2.touching |= Collision.DOWN;
981+
}
982+
}
983+
}
984+
}
985+
986+
// Then adjust their positions and velocities accordingly (if there was any overlap)
987+
if (overlap != 0)
988+
{
989+
var obj1Velocity: number = object1.velocity.y;
990+
var obj2Velocity: number = object2.velocity.y;
991+
992+
if (!object1.immovable && !object2.immovable)
993+
{
994+
overlap *= 0.5;
995+
object1.y = object1.y - overlap;
996+
object2.y += overlap;
997+
998+
var obj1NewVelocity: number = Math.sqrt((obj2Velocity * obj2Velocity * object2.mass) / object1.mass) * ((obj2Velocity > 0) ? 1 : -1);
999+
var obj2NewVelocity: number = Math.sqrt((obj1Velocity * obj1Velocity * object1.mass) / object2.mass) * ((obj1Velocity > 0) ? 1 : -1);
1000+
var average: number = (obj1NewVelocity + obj2NewVelocity) * 0.5;
1001+
obj1NewVelocity -= average;
1002+
obj2NewVelocity -= average;
1003+
object1.velocity.y = average + obj1NewVelocity * object1.elasticity;
1004+
object2.velocity.y = average + obj2NewVelocity * object2.elasticity;
1005+
}
1006+
else if (!object1.immovable)
1007+
{
1008+
object1.y = object1.y - overlap;
1009+
object1.velocity.y = obj2Velocity - obj1Velocity * object1.elasticity;
1010+
// This is special case code that handles things like horizontal moving platforms you can ride
1011+
if (object2.active && object2.moves && (object1.collisionMask.deltaY > object2.collisionMask.deltaY))
1012+
{
1013+
object1.x += object2.x - object2.last.x;
1014+
}
1015+
}
1016+
else if (!object2.immovable)
1017+
{
1018+
object2.y += overlap;
1019+
object2.velocity.y = obj1Velocity - obj2Velocity * object2.elasticity;
1020+
// This is special case code that handles things like horizontal moving platforms you can ride
1021+
if (object1.active && object1.moves && (object1.collisionMask.deltaY < object2.collisionMask.deltaY))
1022+
{
1023+
object2.x += object1.x - object1.last.x;
1024+
}
1025+
}
1026+
1027+
return true;
1028+
}
1029+
else
1030+
{
1031+
return false;
1032+
}
1033+
}
1034+
1035+
/**
1036+
* Separates the two objects on their x axis
1037+
* @param object1 The first GameObject to separate
1038+
* @param object2 The second GameObject to separate
1039+
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
1040+
*/
1041+
public static OLDseparateX(object1, object2): bool {
1042+
1043+
// Can't separate two immovable objects
1044+
if (object1.immovable && object2.immovable)
1045+
{
1046+
return false;
1047+
}
1048+
8281049
// First, get the two object deltas
8291050
var overlap: number = 0;
8301051
var obj1Delta: number = object1.x - object1.last.x;
@@ -922,7 +1143,7 @@ module Phaser {
9221143
* @param object2 The second GameObject to separate
9231144
* @returns {boolean} Whether the objects in fact touched and were separated along the Y axis.
9241145
*/
925-
public static separateY(object1, object2): bool {
1146+
public static OLDseparateY(object1, object2): bool {
9261147

9271148
// Can't separate two immovable objects
9281149
if (object1.immovable && object2.immovable) {
@@ -942,9 +1163,12 @@ module Phaser {
9421163
var obj1Bounds: Quad = new Quad(object1.x, object1.y - ((obj1Delta > 0) ? obj1Delta : 0), object1.width, object1.height + obj1DeltaAbs);
9431164
var obj2Bounds: Quad = new Quad(object2.x, object2.y - ((obj2Delta > 0) ? obj2Delta : 0), object2.width, object2.height + obj2DeltaAbs);
9441165

1166+
console.log(obj1Bounds.toString(), obj2Bounds.toString());
1167+
9451168
if ((obj1Bounds.x + obj1Bounds.width > obj2Bounds.x) && (obj1Bounds.x < obj2Bounds.x + obj2Bounds.width) && (obj1Bounds.y + obj1Bounds.height > obj2Bounds.y) && (obj1Bounds.y < obj2Bounds.y + obj2Bounds.height))
9461169
{
9471170
var maxOverlap: number = obj1DeltaAbs + obj2DeltaAbs + Collision.OVERLAP_BIAS;
1171+
console.log('max33', maxOverlap, obj1Delta, obj2Delta, obj1DeltaAbs, obj2DeltaAbs);
9481172

9491173
// If they did overlap (and can), figure out by how much and flip the corresponding flags
9501174
if (obj1Delta > obj2Delta)
@@ -981,6 +1205,8 @@ module Phaser {
9811205
// Then adjust their positions and velocities accordingly (if there was any overlap)
9821206
if (overlap != 0)
9831207
{
1208+
console.log('y overlap', overlap);
1209+
9841210
var obj1Velocity: number = object1.velocity.y;
9851211
var obj2Velocity: number = object2.velocity.y;
9861212

@@ -1007,6 +1233,7 @@ module Phaser {
10071233
{
10081234
object1.x += object2.x - object2.last.x;
10091235
}
1236+
console.log('y2', object1.y, object1.velocity.y);
10101237
}
10111238
else if (!object2.immovable)
10121239
{
@@ -1017,6 +1244,7 @@ module Phaser {
10171244
{
10181245
object2.x += object1.x - object1.last.x;
10191246
}
1247+
console.log('y3', object2.y, object2.velocity.y);
10201248
}
10211249

10221250
return true;

Phaser/Game.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -729,11 +729,17 @@ module Phaser {
729729
}
730730

731731
/**
732-
* Call this method to see if one object collides with another.
733-
* @return {boolean} Whether the given objects or groups collides.
734-
*/
735-
public collide(objectOrGroup1: Basic = null, objectOrGroup2: Basic = null, notifyCallback = null): bool {
736-
return this.collision.overlap(objectOrGroup1, objectOrGroup2, notifyCallback, Collision.separate);
732+
* Checks for overlaps between two objects using the world QuadTree. Can be GameObject vs. GameObject, GameObject vs. Group or Group vs. Group.
733+
* Note: Does not take the objects scrollFactor into account. All overlaps are check in world space.
734+
* @param object1 The first GameObject or Group to check. If null the world.group is used.
735+
* @param object2 The second GameObject or Group to check.
736+
* @param notifyCallback A callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you passed them to Collision.overlap.
737+
* @param processCallback A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then notifyCallback will only be called if processCallback returns true.
738+
* @param context The context in which the callbacks will be called
739+
* @returns {boolean} true if the objects overlap, otherwise false.
740+
*/
741+
public collide(objectOrGroup1: Basic = null, objectOrGroup2: Basic = null, notifyCallback = null, context? = this.callbackContext): bool {
742+
return this.collision.overlap(objectOrGroup1, objectOrGroup2, notifyCallback, Collision.separate, context);
737743
}
738744

739745
public get camera(): Camera {

Phaser/World.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
/**
44
* Phaser - World
55
*
6+
* "This world is but a canvas to our imagination." - Henry David Thoreau
7+
*
68
* A game has only one world. The world is an abstract place in which all game objects live. It is not bound
79
* by stage limits and can be any size or dimension. You look into the world via cameras and all game objects
810
* live within the world at world-based coordinates. By default a world is created the same size as your Stage.

0 commit comments

Comments
 (0)