@@ -287,6 +287,8 @@ var Body = new Class({
287287 */
288288 this . velocity = new Vector2 ( ) ;
289289
290+ this . prevVelocity = new Vector2 ( ) ;
291+
290292 /**
291293 * Is the Body asleep?.
292294 *
@@ -299,6 +301,8 @@ var Body = new Class({
299301
300302 this . _sleep = 0 ;
301303
304+ this . sleepIterations = 60 ;
305+
302306 this . forcePosition = false ;
303307
304308 /**
@@ -979,6 +983,17 @@ var Body = new Class({
979983 this . prev . y = this . position . y ;
980984 } ,
981985
986+ wake : function ( )
987+ {
988+ if ( this . sleeping )
989+ {
990+ // console.log(this.gameObject.name, 'woken');
991+
992+ this . sleeping = false ;
993+ this . _sleep = 0 ;
994+ }
995+ } ,
996+
982997 /**
983998 * Performs a single physics step and updates the body velocity, angle, speed and other
984999 * properties.
@@ -998,7 +1013,13 @@ var Body = new Class({
9981013 var velocity = this . velocity ;
9991014 var position = this . position ;
10001015
1001- if ( this . moves )
1016+ // Has it been woken up?
1017+ if ( this . sleeping && ! velocity . equals ( this . prevVelocity ) )
1018+ {
1019+ this . wake ( ) ;
1020+ }
1021+
1022+ if ( this . moves && ! this . sleeping )
10021023 {
10031024 this . world . updateMotion ( this , delta ) ;
10041025
@@ -1096,6 +1117,9 @@ var Body = new Class({
10961117 {
10971118 gameObject . x = this . x ;
10981119 gameObject . y = this . y ;
1120+
1121+ dx = 0 ;
1122+ dy = 0 ;
10991123 }
11001124 else
11011125 {
@@ -1125,49 +1149,53 @@ var Body = new Class({
11251149 this . _dx = dx ;
11261150 this . _dy = dy ;
11271151
1128- if ( this . allowRotation )
1152+ if ( this . allowRotation && ! this . sleeping )
11291153 {
11301154 gameObject . angle += this . deltaZ ( ) ;
11311155 }
11321156
1133- // Store collision flags
1134- var wasTouching = this . wasTouching ;
1135- var touching = this . touching ;
1136-
1137- wasTouching . none = touching . none ;
1138- wasTouching . up = touching . up ;
1139- wasTouching . down = touching . down ;
1140- wasTouching . left = touching . left ;
1141- wasTouching . right = touching . right ;
1157+ // Check for sleeping state
11421158
11431159 if ( Math . abs ( dy ) < 1 )
11441160 {
1145- if ( this . _sleep < 60 )
1161+ if ( this . _sleep < this . sleepIterations )
11461162 {
11471163 this . _sleep ++ ;
11481164
1149- if ( this . _sleep >= 60 )
1165+ if ( this . _sleep >= this . sleepIterations )
11501166 {
11511167 this . sleeping = true ;
1168+ this . velocity . set ( 0 ) ;
11521169 }
11531170 }
11541171 }
1155- else
1172+ else if ( this . _sleep > 0 )
11561173 {
1157- if ( this . _sleep > 0 )
1158- {
1159- this . _sleep *= 0.5 ;
1174+ // Waking up? Do it progressively, not instantly, to ensure it isn't just a step fluctuation
1175+ this . _sleep *= 0.5 ;
11601176
1161- if ( this . _sleep <= 0 )
1162- {
1163- this . sleeping = false ;
1164- this . _sleep = 0 ;
1165- }
1177+ if ( this . _sleep <= 0 )
1178+ {
1179+ this . sleeping = false ;
1180+ this . _sleep = 0 ;
11661181 }
11671182 }
11681183
1184+ // Store collision flags
1185+ var wasTouching = this . wasTouching ;
1186+ var touching = this . touching ;
1187+
1188+ wasTouching . none = touching . none ;
1189+ wasTouching . up = touching . up ;
1190+ wasTouching . down = touching . down ;
1191+ wasTouching . left = touching . left ;
1192+ wasTouching . right = touching . right ;
1193+
11691194 this . prev . x = this . position . x ;
11701195 this . prev . y = this . position . y ;
1196+
1197+ this . prevVelocity . x = this . velocity . x ;
1198+ this . prevVelocity . y = this . velocity . y ;
11711199 } ,
11721200
11731201 /**
0 commit comments