11import Utils from './Utils'
22
3+ function def ( value , defaultValue ) {
4+ return value === undefined ? defaultValue : value
5+ }
6+
37export default {
48 anchor : {
59 create : ( options , isTemp = false ) => (
6- { x : options . x , y : options . y , priority : 1 , isTemp, type : 'anchor' }
10+ { x0 : options . x , y0 : options . y , priority : 1 , isTemp, type : 'anchor' }
711 ) ,
8- doFrame : ( options , deltaTime , state , target ) => {
9- let { x, y} = target . getTranslation ( )
10-
12+ doFrame : ( options , deltaTime , state , coords ) => {
1113 // Velocity = dx / deltaTime
12- state . vx = ( options . x - x ) / deltaTime
13- state . vy = ( options . y - y ) / deltaTime
14+ state . vx = ( options . x0 - coords . dx ) / deltaTime
15+ state . vy = ( options . y0 - coords . dy ) / deltaTime
1416 }
1517 } ,
1618
1719 bounce : {
1820 create : ( options , isTemp = false ) => ( {
1921 type : 'bounce' ,
20- bounce : options . bounce || .5 ,
22+ bounce : def ( options . bounce , .5 ) ,
2123 minPoint : options . influence . minPoint ,
2224 maxPoint : options . influence . maxPoint ,
2325 priority : 3 ,
2426 isTemp
2527 } ) ,
26- doFrame : ( { minPoint, maxPoint, bounce} , deltaTime , state , target ) => {
27- let { x, y} = target . getTranslation ( )
28-
28+ doFrame : ( { minPoint, maxPoint, bounce} , deltaTime , state , { x, y, dx, dy} , target ) => {
2929 // Apply limits
3030 if ( minPoint . x > x ) target . setTranslationX ( minPoint . x ) ;
3131 if ( minPoint . y > y ) target . setTranslationY ( minPoint . y ) ;
3232 if ( maxPoint . x < x ) target . setTranslationX ( maxPoint . x ) ;
3333 if ( maxPoint . y < y ) target . setTranslationY ( maxPoint . y ) ;
3434
3535 let { vx, vy } = state
36+ console . log ( vx , vy , bounce )
3637
3738 if ( minPoint . x >= x && vx < 0 ) {
3839 state . vx = - vx * bounce
@@ -52,14 +53,13 @@ export default {
5253 friction : {
5354 create : ( options , isTemp = false ) => ( {
5455 type : 'friction' ,
55- damping : options . damping || .7 ,
56+ damping : def ( options . damping , .7 ) ,
5657 influence : Utils . createArea ( options . influenceArea || { } ) ,
5758 priority : 2 ,
5859 isTemp
5960 } ) ,
60- doFrame : ( options , deltaTime , state , target ) => {
61- let pos = target . getTranslation ( )
62- if ( ! Utils . isPointInArea ( pos , options . influence ) ) return ;
61+ doFrame : ( options , deltaTime , state , coords ) => {
62+ if ( ! Utils . isPointInArea ( coords , options . influence ) ) return ;
6363
6464 let pow = Math . pow ( options . damping , 60.0 * deltaTime )
6565 state . vx = pow * state . vx
@@ -70,26 +70,25 @@ export default {
7070 gravity : {
7171 create : ( options , isTemp = false ) => ( {
7272 type : 'gravity' ,
73- x : options . x || Infinity ,
74- y : options . y || Infinity ,
75- strength : options . strength || 400 ,
76- falloff : options . falloff || 40 ,
77- damping : options . damping || 0 ,
78- influence : options . damping ? Utils . createAreaFromRadius ( 1.4 * options . falloff || 40 , options ) : Utils . createArea ( options . influenceArea || { } ) ,
73+ x0 : def ( options . x , Infinity ) ,
74+ y0 : def ( options . y , Infinity ) ,
75+ strength : def ( options . strength , 400 ) ,
76+ falloff : def ( options . falloff , 40 ) ,
77+ damping : def ( options . damping , 0 ) ,
78+ influence : options . damping ? Utils . createAreaFromRadius ( ( 1.4 * options . falloff ) || 40 , options ) : Utils . createArea ( options . influenceArea || { } ) ,
7979 isTemp,
8080 priority : 1
8181 } ) ,
82- doFrame : ( options , deltaTime , state , target ) => {
83- let pos = target . getTranslation ( )
82+ doFrame : ( options , deltaTime , state , coords ) => {
83+ if ( ! Utils . isPointInArea ( coords , options . influence ) ) return ;
8484
85- if ( ! Utils . isPointInArea ( pos , options . influence ) ) return ;
86- let { x, y, falloff, strength} = options
87- let dx = pos . x - x ;
88- let dy = pos . y - y ;
89-
85+ let dx = coords . dx - options . x0 ;
86+ let dy = coords . dy - options . y0 ;
9087 let dr = Math . sqrt ( dx * dx + dy * dy ) ;
9188 if ( ! dr ) return ;
9289
90+
91+ let { falloff, strength } = options
9392 let a = ( - strength * dr * Math . exp ( - 0.5 * ( dr * dr ) / ( falloff * falloff ) ) ) / state . mass ;
9493 let ax = dx / dr * a ;
9594 let ay = dy / dr * a ;
@@ -102,23 +101,23 @@ export default {
102101 spring : {
103102 create : ( options , isTemp = false ) => ( {
104103 type : 'spring' ,
105- x : options . x || 0 ,
106- y : options . y || 0 ,
107- tension : options . tension || 300 ,
104+ x0 : def ( options . x , 0 ) ,
105+ y0 : def ( options . y , 0 ) ,
106+ tension : def ( options . tension , 300 ) ,
108107 influence : Utils . createArea ( options . influenceArea || { } ) ,
109108 isTemp,
110109 priority : 1
111110 } ) ,
112- doFrame : ( options , deltaTime , state , target ) => {
113- let pos = target . getTranslation ( )
114- if ( ! Utils . isPointInArea ( pos , options . influence ) ) return ;
111+ doFrame : ( options , deltaTime , state , coords ) => {
112+ console . log ( coords )
113+ if ( ! Utils . isPointInArea ( coords , options . influence ) ) return ;
115114
116- let { x , y , tension} = options
115+ let { tension} = options
117116
118- let dx = pos . x - x ;
117+ let dx = coords . dx - options . x0 ;
119118 let ax = ( - tension * dx ) / state . mass ;
120119
121- let dy = pos . y - y ;
120+ let dy = coords . dy - options . y0 ;
122121 let ay = ( - tension * dy ) / state . mass ;
123122
124123 state . vx = state . vx + deltaTime * ax
0 commit comments