@@ -1394,11 +1394,6 @@ var World = new Class({
13941394 GetOverlapX ( body1 , body2 , false , bias ) ;
13951395 GetOverlapY ( body1 , body2 , false , bias ) ;
13961396
1397- var dx = body2 . center . x - body1 . center . x ;
1398- var dy = body2 . center . y - body1 . center . y ;
1399-
1400- var angleCollision = Math . atan2 ( dy , dx ) ;
1401-
14021397 var overlap = 0 ;
14031398
14041399 if ( body1 . isCircle !== body2 . isCircle )
@@ -1458,94 +1453,28 @@ var World = new Class({
14581453 return ( overlap !== 0 ) ;
14591454 }
14601455
1461- // Transform the velocity vector to the coordinate system oriented along the direction of impact.
1462- // This is done to eliminate the vertical component of the velocity
1463-
1464- var b1vx = body1 . velocity . x ;
1465- var b1vy = body1 . velocity . y ;
1466- var b1mass = body1 . mass ;
1467-
1468- var b2vx = body2 . velocity . x ;
1469- var b2vy = body2 . velocity . y ;
1470- var b2mass = body2 . mass ;
1471-
1472- var v1 = {
1473- x : b1vx * Math . cos ( angleCollision ) + b1vy * Math . sin ( angleCollision ) ,
1474- y : b1vx * Math . sin ( angleCollision ) - b1vy * Math . cos ( angleCollision )
1475- } ;
1476-
1477- var v2 = {
1478- x : b2vx * Math . cos ( angleCollision ) + b2vy * Math . sin ( angleCollision ) ,
1479- y : b2vx * Math . sin ( angleCollision ) - b2vy * Math . cos ( angleCollision )
1480- } ;
1481-
1482- // We expect the new velocity after impact
1483- var tempVel1 = ( ( b1mass - b2mass ) * v1 . x + 2 * b2mass * v2 . x ) / ( b1mass + b2mass ) ;
1484- var tempVel2 = ( 2 * b1mass * v1 . x + ( b2mass - b1mass ) * v2 . x ) / ( b1mass + b2mass ) ;
1456+ var dx = body1 . position . x - body2 . position . x ;
1457+ var dy = body1 . position . y - body2 . position . y ;
1458+ var d = Math . sqrt ( Math . pow ( dx , 2 ) + Math . pow ( dy , 2 ) ) ;
1459+ var nx = ( body2 . position . x - body1 . position . x ) / d ;
1460+ var ny = ( body2 . position . y - body1 . position . y ) / d ;
1461+ var p = 2 * ( body1 . velocity . x * nx + body1 . velocity . y * ny - body2 . velocity . x * nx - body2 . velocity . y * ny ) / ( body1 . mass + body2 . mass ) ;
14851462
1486- // We convert the vector to the original coordinate system and multiplied by factor of rebound
14871463 if ( ! body1 . immovable )
14881464 {
1489- body1 . velocity . x = ( tempVel1 * Math . cos ( angleCollision ) - v1 . y * Math . sin ( angleCollision ) ) * body1 . bounce . x ;
1490- body1 . velocity . y = ( v1 . y * Math . cos ( angleCollision ) + tempVel1 * Math . sin ( angleCollision ) ) * body1 . bounce . y ;
1491-
1492- // Reset local var
1493- b1vx = body1 . velocity . x ;
1494- b1vy = body1 . velocity . y ;
1465+ body1 . velocity . x = ( body1 . velocity . x - p * body1 . mass * nx ) * body1 . bounce . x ;
1466+ body1 . velocity . y = ( body1 . velocity . y - p * body1 . mass * ny ) * body1 . bounce . y ;
14951467 }
14961468
14971469 if ( ! body2 . immovable )
14981470 {
1499- body2 . velocity . x = ( tempVel2 * Math . cos ( angleCollision ) - v2 . y * Math . sin ( angleCollision ) ) * body2 . bounce . x ;
1500- body2 . velocity . y = ( v2 . y * Math . cos ( angleCollision ) + tempVel2 * Math . sin ( angleCollision ) ) * body2 . bounce . y ;
1501-
1502- // Reset local var
1503- b2vx = body2 . velocity . x ;
1504- b2vy = body2 . velocity . y ;
1471+ body2 . velocity . x = ( body2 . velocity . x + p * body2 . mass * nx ) * body2 . bounce . x ;
1472+ body2 . velocity . y = ( body2 . velocity . y + p * body2 . mass * ny ) * body2 . bounce . y ;
15051473 }
15061474
1507- // When the collision angle is almost perpendicular to the total initial velocity vector
1508- // (collision on a tangent) vector direction can be determined incorrectly.
1509- // This code fixes the problem
1510-
1511- if ( Math . abs ( angleCollision ) < Math . PI / 2 )
1512- {
1513- if ( ( b1vx > 0 ) && ! body1 . immovable && ( b2vx > b1vx ) )
1514- {
1515- body1 . velocity . x *= - 1 ;
1516- }
1517- else if ( ( b2vx < 0 ) && ! body2 . immovable && ( b1vx < b2vx ) )
1518- {
1519- body2 . velocity . x *= - 1 ;
1520- }
1521- else if ( ( b1vy > 0 ) && ! body1 . immovable && ( b2vy > b1vy ) )
1522- {
1523- body1 . velocity . y *= - 1 ;
1524- }
1525- else if ( ( b2vy < 0 ) && ! body2 . immovable && ( b1vy < b2vy ) )
1526- {
1527- body2 . velocity . y *= - 1 ;
1528- }
1529- }
1530- else if ( Math . abs ( angleCollision ) > Math . PI / 2 )
1531- {
1532- if ( ( b1vx < 0 ) && ! body1 . immovable && ( b2vx < b1vx ) )
1533- {
1534- body1 . velocity . x *= - 1 ;
1535- }
1536- else if ( ( b2vx > 0 ) && ! body2 . immovable && ( b1vx > b2vx ) )
1537- {
1538- body2 . velocity . x *= - 1 ;
1539- }
1540- else if ( ( b1vy < 0 ) && ! body1 . immovable && ( b2vy < b1vy ) )
1541- {
1542- body1 . velocity . y *= - 1 ;
1543- }
1544- else if ( ( b2vy > 0 ) && ! body2 . immovable && ( b1vx > b2vy ) )
1545- {
1546- body2 . velocity . y *= - 1 ;
1547- }
1548- }
1475+ var dvx = body2 . velocity . x - body1 . velocity . x ;
1476+ var dvy = body2 . velocity . y - body1 . velocity . y ;
1477+ var angleCollision = Math . atan2 ( dvy , dvx ) ;
15491478
15501479 var delta = this . _frameTime ;
15511480
0 commit comments