@@ -5,6 +5,7 @@ Example.manipulation = function() {
55 Render = Matter . Render ,
66 Runner = Matter . Runner ,
77 Body = Matter . Body ,
8+ Common = Matter . Common ,
89 Events = Matter . Events ,
910 MouseConstraint = Matter . MouseConstraint ,
1011 Mouse = Matter . Mouse ,
@@ -42,8 +43,8 @@ Example.manipulation = function() {
4243 bodyE = Bodies . rectangle ( 550 , 200 , 50 , 50 ) ,
4344 bodyF = Bodies . rectangle ( 700 , 200 , 50 , 50 ) ,
4445 bodyG = Bodies . circle ( 400 , 100 , 25 ) ,
45- partA = Bodies . rectangle ( 600 , 200 , 120 , 50 ) ,
46- partB = Bodies . rectangle ( 660 , 200 , 50 , 190 ) ,
46+ partA = Bodies . rectangle ( 600 , 200 , 120 * 0.8 , 50 * 0.8 ) ,
47+ partB = Bodies . rectangle ( 660 , 200 , 50 * 0.8 , 190 * 0.8 ) ,
4748 compound = Body . create ( {
4849 parts : [ partA , partB ] ,
4950 isStatic : true
@@ -59,48 +60,46 @@ Example.manipulation = function() {
5960 Bodies . rectangle ( 0 , 300 , 50 , 600 , { isStatic : true } )
6061 ] ) ;
6162
62- var counter = 0 ,
63- scaleFactor = 1.01 ;
63+ var lastTime = 0 ,
64+ scaleRate = 0.6 ;
6465
6566 Events . on ( engine , 'beforeUpdate' , function ( event ) {
66- counter += 1 ;
67+ var timeScale = event . delta / 1000 ;
6768
68- if ( counter === 40 )
69- Body . setStatic ( bodyG , true ) ;
70-
71- if ( scaleFactor > 1 ) {
72- Body . scale ( bodyF , scaleFactor , scaleFactor ) ;
73- Body . scale ( compound , 0.995 , 0.995 ) ;
69+ if ( scaleRate > 0 ) {
70+ Body . scale ( bodyF , 1 + ( scaleRate * timeScale ) , 1 + ( scaleRate * timeScale ) ) ;
7471
7572 // modify bodyE vertices
76- bodyE . vertices [ 0 ] . x -= 0.2 ;
77- bodyE . vertices [ 0 ] . y -= 0.2 ;
78- bodyE . vertices [ 1 ] . x += 0.2 ;
79- bodyE . vertices [ 1 ] . y -= 0.2 ;
73+ bodyE . vertices [ 0 ] . x -= 0.2 * timeScale ;
74+ bodyE . vertices [ 0 ] . y -= 0.2 * timeScale ;
75+ bodyE . vertices [ 1 ] . x += 0.2 * timeScale ;
76+ bodyE . vertices [ 1 ] . y -= 0.2 * timeScale ;
8077 Body . setVertices ( bodyE , bodyE . vertices ) ;
8178 }
8279
8380 // make bodyA move up and down
84- // body is static so must manually update velocity for friction to work
8581 var py = 300 + 100 * Math . sin ( engine . timing . timestamp * 0.002 ) ;
86- Body . setVelocity ( bodyA , { x : 0 , y : py - bodyA . position . y } ) ;
87- Body . setPosition ( bodyA , { x : 100 , y : py } ) ;
82+ Body . setPosition ( bodyA , { x : 100 , y : py } , true ) ;
8883
8984 // make compound body move up and down and rotate constantly
90- Body . setVelocity ( compound , { x : 0 , y : py - compound . position . y } ) ;
91- Body . setAngularVelocity ( compound , 0.02 ) ;
92- Body . setPosition ( compound , { x : 600 , y : py } ) ;
93- Body . rotate ( compound , 0.02 ) ;
85+ Body . setPosition ( compound , { x : 600 , y : py } , true ) ;
86+ Body . rotate ( compound , 1 * Math . PI * timeScale , null , true ) ;
87+
88+ // after first 0.8 sec (simulation time)
89+ if ( engine . timing . timestamp >= 800 )
90+ Body . setStatic ( bodyG , true ) ;
9491
95- // every 1.5 sec
96- if ( counter >= 60 * 1.5 ) {
92+ // every 1.5 sec (simulation time)
93+ if ( engine . timing . timestamp - lastTime >= 1500 ) {
9794 Body . setVelocity ( bodyB , { x : 0 , y : - 10 } ) ;
9895 Body . setAngle ( bodyC , - Math . PI * 0.26 ) ;
9996 Body . setAngularVelocity ( bodyD , 0.2 ) ;
10097
101- // reset counter
102- counter = 0 ;
103- scaleFactor = 1 ;
98+ // stop scaling
99+ scaleRate = 0 ;
100+
101+ // update last time
102+ lastTime = engine . timing . timestamp ;
104103 }
105104 } ) ;
106105
0 commit comments