@@ -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
0 commit comments