@@ -11,6 +11,9 @@ var Collider = require('./Collider');
1111var CONST = require ( './const' ) ;
1212var DistanceBetween = require ( '../../math/distance/DistanceBetween' ) ;
1313var EventEmitter = require ( 'eventemitter3' ) ;
14+ var FuzzyEqual = require ( '../../math/fuzzy/Equal' ) ;
15+ var FuzzyGreaterThan = require ( '../../math/fuzzy/GreaterThan' ) ;
16+ var FuzzyLessThan = require ( '../../math/fuzzy/LessThan' ) ;
1417var GetOverlapX = require ( './GetOverlapX' ) ;
1518var GetOverlapY = require ( './GetOverlapY' ) ;
1619var GetValue = require ( '../../utils/object/GetValue' ) ;
@@ -1172,56 +1175,41 @@ var World = new Class({
11721175 {
11731176 if ( body . allowRotation )
11741177 {
1175- var velocityDelta = this . computeVelocity ( 0 , body , body . angularVelocity , body . angularAcceleration , body . angularDrag , body . maxAngular , delta ) - body . angularVelocity ;
1176-
1177- body . angularVelocity += velocityDelta ;
1178- body . rotation += ( body . angularVelocity * delta ) ;
1178+ this . computeAngularVelocity ( body , delta ) ;
11791179 }
11801180
1181- body . velocity . x = this . computeVelocity ( 1 , body , body . velocity . x , body . acceleration . x , body . drag . x , body . maxVelocity . x , delta ) ;
1182- body . velocity . y = this . computeVelocity ( 2 , body , body . velocity . y , body . acceleration . y , body . drag . y , body . maxVelocity . y , delta ) ;
1181+ this . computeVelocity ( body , delta ) ;
11831182 } ,
11841183
11851184 /**
1186- * Calculates a Body's per-axis velocity.
1185+ * Calculates a Body's angular velocity.
11871186 *
1188- * @method Phaser.Physics.Arcade.World#computeVelocity
1189- * @since 3.0 .0
1187+ * @method Phaser.Physics.Arcade.World#computeAngularVelocity
1188+ * @since 3.10 .0
11901189 *
1191- * @param {integer } axis - The velocity axis. 0 for rotation, 1 for x and 2 for y.
11921190 * @param {Phaser.Physics.Arcade.Body } body - The Body to compute the velocity for.
1193- * @param {number } velocity - The velocity component.
1194- * @param {number } acceleration - The acceleration component.
1195- * @param {number } drag - The drag component.
1196- * @param {number } max - The maximum allowed velocity.
11971191 * @param {number } delta - The delta value to be used in the calculation.
1198- *
1199- * @return {number } The new velocity value.
12001192 */
1201- computeVelocity : function ( axis , body , velocity , acceleration , drag , max , delta )
1193+ computeAngularVelocity : function ( body , delta )
12021194 {
1203- if ( axis === 1 && body . allowGravity )
1204- {
1205- velocity += ( this . gravity . x + body . gravity . x ) * delta ;
1206- }
1207- else if ( axis === 2 && body . allowGravity )
1208- {
1209- velocity += ( this . gravity . y + body . gravity . y ) * delta ;
1210- }
1195+ var velocity = body . angularVelocity ;
1196+ var acceleration = body . angularAcceleration ;
1197+ var drag = body . angularDrag ;
1198+ var max = body . maxAngular ;
12111199
12121200 if ( acceleration )
12131201 {
12141202 velocity += acceleration * delta ;
12151203 }
1216- else if ( drag && body . allowDrag )
1204+ else if ( body . allowDrag && drag )
12171205 {
12181206 drag *= delta ;
12191207
1220- if ( velocity - drag > 0 )
1208+ if ( FuzzyGreaterThan ( velocity - drag , 0 , 0.1 ) )
12211209 {
12221210 velocity -= drag ;
12231211 }
1224- else if ( velocity + drag < 0 )
1212+ else if ( FuzzyLessThan ( velocity + drag , 0 , 0.1 ) )
12251213 {
12261214 velocity += drag ;
12271215 }
@@ -1231,16 +1219,121 @@ var World = new Class({
12311219 }
12321220 }
12331221
1234- if ( velocity > max )
1222+ velocity = Clamp ( velocity , - max , max ) ;
1223+
1224+ var velocityDelta = velocity - body . angularVelocity ;
1225+
1226+ body . angularVelocity += velocityDelta ;
1227+ body . rotation += ( body . angularVelocity * delta ) ;
1228+ } ,
1229+
1230+ /**
1231+ * Calculates a Body's per-axis velocity.
1232+ *
1233+ * @method Phaser.Physics.Arcade.World#computeVelocity
1234+ * @since 3.0.0
1235+ *
1236+ * @param {Phaser.Physics.Arcade.Body } body - The Body to compute the velocity for.
1237+ * @param {number } delta - The delta value to be used in the calculation.
1238+ */
1239+ computeVelocity : function ( body , delta )
1240+ {
1241+ var velocityX = body . velocity . x ;
1242+ var accelerationX = body . acceleration . x ;
1243+ var dragX = body . drag . x ;
1244+ var maxX = body . maxVelocity . x ;
1245+
1246+ var velocityY = body . velocity . y ;
1247+ var accelerationY = body . acceleration . y ;
1248+ var dragY = body . drag . y ;
1249+ var maxY = body . maxVelocity . y ;
1250+
1251+ var speed = body . speed ;
1252+ var allowDrag = body . allowDrag ;
1253+ var useDamping = body . useDamping ;
1254+
1255+ if ( body . allowGravity )
1256+ {
1257+ velocityX += ( this . gravity . x + body . gravity . x ) * delta ;
1258+ velocityY += ( this . gravity . y + body . gravity . y ) * delta ;
1259+ }
1260+
1261+ if ( accelerationX )
1262+ {
1263+ velocityX += accelerationX * delta ;
1264+ }
1265+ else if ( allowDrag && dragX )
1266+ {
1267+ if ( useDamping )
1268+ {
1269+ // Damping based deceleration
1270+ velocityX *= dragX ;
1271+
1272+ if ( FuzzyEqual ( speed , 0 , 0.001 ) )
1273+ {
1274+ velocityX = 0 ;
1275+ }
1276+ }
1277+ else
1278+ {
1279+ // Linear deceleration
1280+ dragX *= delta ;
1281+
1282+ if ( FuzzyGreaterThan ( velocityX - dragX , 0 , 0.01 ) )
1283+ {
1284+ velocityX -= dragX ;
1285+ }
1286+ else if ( FuzzyLessThan ( velocityX + dragX , 0 , 0.01 ) )
1287+ {
1288+ velocityX += dragX ;
1289+ }
1290+ else
1291+ {
1292+ velocityX = 0 ;
1293+ }
1294+ }
1295+ }
1296+
1297+ if ( accelerationY )
12351298 {
1236- velocity = max ;
1299+ velocityY += accelerationY * delta ;
12371300 }
1238- else if ( velocity < - max )
1301+ else if ( allowDrag && dragY )
12391302 {
1240- velocity = - max ;
1303+ if ( useDamping )
1304+ {
1305+ // Damping based deceleration
1306+ velocityY *= dragY ;
1307+
1308+ if ( FuzzyEqual ( speed , 0 , 0.001 ) )
1309+ {
1310+ velocityY = 0 ;
1311+ }
1312+ }
1313+ else
1314+ {
1315+ // Linear deceleration
1316+ dragY *= delta ;
1317+
1318+ if ( FuzzyGreaterThan ( velocityY - dragY , 0 , 0.01 ) )
1319+ {
1320+ velocityY -= dragY ;
1321+ }
1322+ else if ( FuzzyLessThan ( velocityY + dragY , 0 , 0.01 ) )
1323+ {
1324+ velocityY += dragY ;
1325+ }
1326+ else
1327+ {
1328+ velocityY = 0 ;
1329+ }
1330+ }
12411331 }
12421332
1243- return velocity ;
1333+ velocityX = Clamp ( velocityX , - maxX , maxX ) ;
1334+ velocityY = Clamp ( velocityY , - maxY , maxY ) ;
1335+
1336+ body . velocity . set ( velocityX , velocityY ) ;
12441337 } ,
12451338
12461339 /**
0 commit comments