Unit 5: More Collisions 3D Modelling & Animation Module F21MA Unit5: More Collisions Mike Chantler, 31/8/2008 See Peters “ActionScript 3.0 Animation” Unit contents • Detecting collisions • Springing off a fixed ball • Billiard Ball Physics • 2D Billiard Ball Physics • Reference: Peters "Foundation Actionscript 3.0 Animation: Making Things Move!" 1
Unit 5: More Collisions 3D Modelling & Animation Module F21MA Detecting collisions Collisions between two objects • Flash’s ‘hit test’ • Distance based 2
Unit 5: More Collisions Flash’s Hit Test • Based on bounding boxes Perform hitTest in event handler for Event.ENTER_FRAME if(blueBall.hitTestObject(blackBall)) {……. Distance Based d=sqrt( (x 1 -x 2 ) 2 + (y 1 -y 2 ) 2 ) limit 2 = (r 1 + r 2 ) 2 if (x 1 -x 2 ) 2 + (y 1 -y 2 ) 2 < limit 2 { ….. 3
Unit 5: More Collisions 3D Modelling & Animation Module F21MA Springing off a fixed ball Springing ball 2 ball 1 4
Unit 5: More Collisions Detect onset of Springing d min = r 1 + r 2 (x 2 , y 2 ) (x 1 , y 1 ) d = Math.sqrt( (x 1 -x 2 ) 2 + (y 1 -y 2 ) 2 ) if (d < d min ) { …. } Calculate Springing Point d min = r 1 + r 2 t = (t x , t y ) (x 2 , y 2 ) θ (x 1 , y 1 ) θ = Math.atan2((y 2 -y 1 ), (x 2 -x 1 )) t x = x 1 + d min * Math.cos( θ ) t y = y 1 + d min * Math.sin( θ ) 5
Unit 5: More Collisions Calculate Springing Point t = (t x , t y ) Calculate acceleration ball 2 t = (t x , t y ) (x 2 , y 2 ) a x2 = (t x – x 2 ) * c spring a y2 = (t y – y 2 ) * c spring 6
Unit 5: More Collisions Calculate new ball 2 position t = (t x , t y ) (x 2 , y 2 ) v x2 += a x2 ; x 2 += v x2 v y2 etc. Until Spring Stops t = (t x , t y ) (x 2 , y 2 ) 7
Unit 5: More Collisions Calculate new position as before (x 2 , y 2 ) v x2 = (t x – x 2 ) * c spring v y2 = (t y – y 2 ) * c spring Example • See bubble.as in ch09 of Peters’ download – http://www.friendsofed.com/downloads/1590597 915/FoundationAS3Animation.zip 8
Unit 5: More Collisions Note Blue ball remains stationary . To calculate movement in blue ball we need momentum/energy conservation rules [see Peters]. 3D Modelling & Animation Module F21MA Billiard Ball Physics 9
Unit 5: More Collisions Basic Equations b0 • Momentum (mass * velocity) p = m * v (& note that p is a vector) b1 • Conservation of Momentum Total p before collision = total p after b0 m 0 *v 0 + m 1 *v 1 = m 0 *v 0 final + m 1 *v 1 final b1 Basic Equations • Kenetic energy b0 KE = ½ * m * v 2 • Conservation of Energy Total KE collision = KE after b1 2 + m 1 *v 1 2 = m 0 *v 0 final 2 + m 1 *v 1 final 2 m 0 *v 0 b0 b1 10
Unit 5: More Collisions Basic Equations • Hence b0 (m 0 - m 1 )*v 0 + 2*m 1 *v 1 v 0 final = m 0 + m 1 b1 (m 1 – m 0 )*v 1 + 2*m 0 *v 0 v 1 final = m 0 + m 1 b0 b1 Implementation • Simple to implement in 1 dimension (m 0 - m 1 )*v 0 + 2*m 1 *v 1 v 0 final = m 0 + m 1 b0 (m 1 – m 0 )*v 1 + 2*m 0 *v 0 b1 v 1 final = m 0 + m 1 y-axis 11
Unit 5: More Collisions Implementation • See Billiard2.as and Ball.as in ch11 of Peters’ download b0 – http://www.friendsofed.com /downloads /1590597915 b1 /FoundationAS3Animation.zip y-axis Sanity check • What if m 0 >> m 1 ? b0 (m 0 - m 1 )*v 0 + 2*m 1 *v 1 v 0 final = m 0 + m 1 b1 (m 1 – m 0 )*v 1 + 2*m 0 *v 0 v 1 final = m 0 + m 1 12
Unit 5: More Collisions Sanity check • What if m 0 >> m 1 ? b0 (m 0 )*v 0 v 0 final = m 0 b0 b1 b1 v 1 final = (– m 0 )*v 1 + 2*m 0 *v 0 m 0 –v 1 + 2*v 0 v 1 final = 3D Modelling & Animation Module F21MA 2D Billiard Ball Physics 13
Unit 5: More Collisions The 2D case At point of collision 14
Unit 5: More Collisions Rotate axis of collision to x-axis Rotate axis of collision to x-axis 15
Unit 5: More Collisions Rotate axis of collision to x-axis Resolve velocities into x & y components v x = v sin θ θ v y = v cos θ 16
Unit 5: More Collisions Remember the Coord-system • In the x 1 = r sin θ second x quadrant x 1 is negative y 1 = r cos θ and y 1 is θ positive r ( x 1 , y 1 ) y Resolve velocities into x & y components for other ball v y = v cos θ v x = v sin θ θ 17
Unit 5: More Collisions Consider only x components Apply 1D equations to x final components (m 0 - m 1 )*v 0 + 2*m 1 *v 1 v 0 final = m 0 + m 1 (m 1 – m 0 )*v 1 + 2*m 0 *v 0 v 1 final = m 0 + m 1 b0 b1 18
Unit 5: More Collisions Result: final x components Add original y components back in 19
Unit 5: More Collisions Rotate everything back Implementation • See Billiard4.as in ch11 of Peters’ download – http://www.friendsofed.com /downloads /1590597915 /FoundationAS3Animation.zip 20
Unit 5: More Collisions Notes on Implementation • Billiard4.as uses flash.geom.Point class The Point object represents a location (x, y) e.g. import flash.geom.Point; private var point2:Point = new Point(6, 8); Its not all that great for us – but its what I ’ d build upon to create a point class for representing positions, velocities, and accelerations. I ’ d add better polar, rotation and other transformation functions. 3D Modelling & Animation Module F21MA Lab: ex5.1 21
Unit 5: More Collisions Create a Test • For a stationary ball and a draggable ball Perform hitTest in event handler for Event.ENTER_FRAME if(blueBall.hitTestObject(blackBall)) {……. 3D Modelling & Animation Module F21MA Lab: ex5.2 22
Unit 5: More Collisions Implement spring based collisions t = (t x , t y ) (x 2 , y 2 ) a x2 = (t x – x 2 ) * c spring a y2 = (t y – y 2 ) * c spring 3D Modelling & Animation Module F21MA End 23
Recommend
More recommend