@@ -9,6 +9,8 @@ var CircleContains = require('../../geom/circle/Contains');
99var Class = require ( '../../utils/Class' ) ;
1010var CONST = require ( './const' ) ;
1111var Events = require ( './events' ) ;
12+ var FuzzyGreaterThan = require ( '../../math/fuzzy/GreaterThan' ) ;
13+ var FuzzyLessThan = require ( '../../math/fuzzy/LessThan' ) ;
1214var CheckOverlapY = require ( './CheckOverlapY' ) ;
1315var RadToDeg = require ( '../../math/RadToDeg' ) ;
1416var Rectangle = require ( '../../geom/rectangle/Rectangle' ) ;
@@ -812,26 +814,26 @@ var Body = new Class({
812814 this . _bounds = new Rectangle ( ) ;
813815
814816 /**
815- * Flip-flop tracking var .
817+ * The amount of gravity that was applied to the body in the current frame .
816818 *
817- * @name Phaser.Physics.Arcade.Body#_flipflopX
818- * @type {integer }
819+ * @name Phaser.Physics.Arcade.Body#_gx
820+ * @type {number }
819821 * @private
820822 * @default 0
821823 * @since 3.17.0
822824 */
823- this . _flipflopX = 0 ;
825+ this . _gx = 0 ;
824826
825827 /**
826- * Flip-flop tracking var .
828+ * The amount of gravity that was applied to the body in the current frame .
827829 *
828- * @name Phaser.Physics.Arcade.Body#_flipflopY
829- * @type {integer }
830+ * @name Phaser.Physics.Arcade.Body#_gy
831+ * @type {number }
830832 * @private
831833 * @default 0
832834 * @since 3.17.0
833835 */
834- this . _flipflopY = 0 ;
836+ this . _gy = 0 ;
835837 } ,
836838
837839 /**
@@ -1028,6 +1030,26 @@ var Body = new Class({
10281030
10291031 this . log . push ( 'after len: ' + currentBlockers . length ) ;
10301032
1033+ // If we can't move anywhere, we need to kill velocity now
1034+
1035+ var worldBlocked = this . worldBlocked ;
1036+
1037+ if ( worldBlocked . up && worldBlocked . down )
1038+ {
1039+ this . velocity . y = 0 ;
1040+ }
1041+
1042+ if ( worldBlocked . left && worldBlocked . right )
1043+ {
1044+ this . velocity . x = 0 ;
1045+ }
1046+
1047+ if ( this . velocity . x === 0 && this . velocity . y === 0 )
1048+ {
1049+ this . sleeping = true ;
1050+ this . prevVelocity . set ( 0 ) ;
1051+ }
1052+
10311053 this . blockers = currentBlockers ;
10321054 } ,
10331055
@@ -1047,8 +1069,6 @@ var Body = new Class({
10471069 */
10481070 update : function ( delta )
10491071 {
1050- this . log . push ( 'update' ) ;
1051-
10521072 this . updateBlockers ( ) ;
10531073
10541074 var velocity = this . velocity ;
@@ -1058,25 +1078,10 @@ var Body = new Class({
10581078 // Has it been woken up?
10591079 if ( this . sleeping && ! velocity . equals ( this . prevVelocity ) )
10601080 {
1061- this . log . push ( 'wake test' ) ;
1062- this . log . push ( velocity . y ) ;
1063- this . log . push ( worldBlocked . up ) ;
1064- this . log . push ( worldBlocked . down ) ;
1065-
1066- // console.log(this.gameObject.name, 'wake test', velocity.y, worldBlocked.up, worldBlocked.down);
1067- // console.log(this.blockers.length);
1068- // console.log(this.blockers);
1069-
10701081 if ( ( velocity . y < 0 && ! worldBlocked . up ) || ( velocity . y > 0 && ! worldBlocked . down ) )
10711082 {
1072- console . log ( this . log ) ;
1073- // debugger;
10741083 this . wake ( ) ;
10751084 }
1076- else
1077- {
1078- velocity . y = 0 ;
1079- }
10801085 }
10811086
10821087 if ( this . sleeping )
@@ -1099,28 +1104,7 @@ var Body = new Class({
10991104 // World Bounds check
11001105 if ( this . collideWorldBounds )
11011106 {
1102- if ( ! worldBlocked . none )
1103- {
1104- var bx = ( this . worldBounce ) ? - this . worldBounce . x : - this . bounce . x ;
1105- var by = ( this . worldBounce ) ? - this . worldBounce . y : - this . bounce . y ;
1106-
1107- // Reverse the velocity for the bounce
1108-
1109- if ( ( worldBlocked . left && velocity . x < 0 ) || ( worldBlocked . right && velocity . x > 0 ) )
1110- {
1111- velocity . x *= bx ;
1112- }
1113-
1114- if ( ( worldBlocked . down && velocity . y > 0 ) || ( worldBlocked . up && velocity . y < 0 ) )
1115- {
1116- velocity . y *= by ;
1117- }
1118- }
1119-
1120- if ( this . onWorldBounds )
1121- {
1122- this . world . emit ( Events . WORLD_BOUNDS , this , worldBlocked . up , worldBlocked . down , worldBlocked . left , worldBlocked . right ) ;
1123- }
1107+ this . worldReboundCheck ( ) ;
11241108 }
11251109
11261110 this . updateCenter ( ) ;
@@ -1135,6 +1119,68 @@ var Body = new Class({
11351119 // And finally we'll integrate the new position back to the Sprite in postUpdate
11361120 } ,
11371121
1122+ worldReboundCheck : function ( )
1123+ {
1124+ var worldBlocked = this . worldBlocked ;
1125+ var touching = this . touching ;
1126+ var velocity = this . velocity ;
1127+
1128+ var bx = ( this . worldBounce ) ? this . worldBounce . x : this . bounce . x ;
1129+ var by = ( this . worldBounce ) ? this . worldBounce . y : this . bounce . y ;
1130+
1131+ if ( ! worldBlocked . none && ( bx !== 0 || by !== 0 ) )
1132+ {
1133+ // Reverse the velocity for the world bounce?
1134+ if ( by !== 0 && velocity . y !== 0 )
1135+ {
1136+ if ( ( worldBlocked . down && ! touching . up && velocity . y > 0 ) || ( worldBlocked . up && ! touching . down && velocity . y < 0 ) )
1137+ {
1138+ var gravityY = this . _gy ;
1139+ var newVelocityY = velocity . y * by ;
1140+
1141+ if ( gravityY > 0 && newVelocityY > 0 )
1142+ {
1143+ // Gravity is pulling them down
1144+ if ( newVelocityY < gravityY || FuzzyLessThan ( newVelocityY , gravityY , 2 ) )
1145+ {
1146+ velocity . y = 0 ;
1147+ console . log ( 'rebound up too small, zeroing' , newVelocityY , gravityY ) ;
1148+ }
1149+ else
1150+ {
1151+ velocity . y *= - by ;
1152+ console . log ( 'rebounded up' , newVelocityY , gravityY ) ;
1153+
1154+ if ( this . onWorldBounds )
1155+ {
1156+ this . world . emit ( Events . WORLD_BOUNDS , this , worldBlocked . up , worldBlocked . down , worldBlocked . left , worldBlocked . right ) ;
1157+ }
1158+ }
1159+ }
1160+ else if ( gravityY < 0 && newVelocityY < 0 )
1161+ {
1162+ // Gravity is pulling them up
1163+ if ( newVelocityY > gravityY || FuzzyGreaterThan ( newVelocityY , gravityY , 2 ) )
1164+ {
1165+ velocity . y = 0 ;
1166+ console . log ( 'rebound down too small, zeroing' , newVelocityY , gravityY ) ;
1167+ }
1168+ else
1169+ {
1170+ velocity . y *= - by ;
1171+ console . log ( 'rebounded down' , newVelocityY , gravityY ) ;
1172+
1173+ if ( this . onWorldBounds )
1174+ {
1175+ this . world . emit ( Events . WORLD_BOUNDS , this , worldBlocked . up , worldBlocked . down , worldBlocked . left , worldBlocked . right ) ;
1176+ }
1177+ }
1178+ }
1179+ }
1180+ }
1181+ }
1182+ } ,
1183+
11381184 /**
11391185 * Feeds the Body results back into the parent Game Object.
11401186 *
@@ -1179,6 +1225,24 @@ var Body = new Class({
11791225 }
11801226 }
11811227
1228+ if ( dx < 0 )
1229+ {
1230+ this . facing = CONST . FACING_LEFT ;
1231+ }
1232+ else if ( dx > 0 )
1233+ {
1234+ this . facing = CONST . FACING_RIGHT ;
1235+ }
1236+
1237+ if ( dy < 0 )
1238+ {
1239+ this . facing = CONST . FACING_UP ;
1240+ }
1241+ else if ( dy > 0 )
1242+ {
1243+ this . facing = CONST . FACING_DOWN ;
1244+ }
1245+
11821246 if ( this . forcePosition )
11831247 {
11841248 gameObject . x = this . x ;
@@ -1194,24 +1258,6 @@ var Body = new Class({
11941258 }
11951259 }
11961260
1197- if ( dx < 0 )
1198- {
1199- this . facing = CONST . FACING_LEFT ;
1200- }
1201- else if ( dx > 0 )
1202- {
1203- this . facing = CONST . FACING_RIGHT ;
1204- }
1205-
1206- if ( dy < 0 )
1207- {
1208- this . facing = CONST . FACING_UP ;
1209- }
1210- else if ( dy > 0 )
1211- {
1212- this . facing = CONST . FACING_DOWN ;
1213- }
1214-
12151261 this . _dx = dx ;
12161262 this . _dy = dy ;
12171263
@@ -1235,13 +1281,14 @@ var Body = new Class({
12351281 }
12361282 }
12371283 }
1238- else if ( this . _sleep > 0 )
1284+ else if ( this . _sleep > 0 && ! this . worldBlocked . up && ! this . worldBlocked . down )
12391285 {
12401286 // Waking up? Do it progressively, not instantly, to ensure it isn't just a step fluctuation
12411287 this . _sleep *= 0.8 ;
12421288
12431289 if ( this . _sleep <= 0 )
12441290 {
1291+ console . log ( 'body woken from postUpdate' , dy ) ;
12451292 this . wake ( ) ;
12461293 }
12471294 }
0 commit comments