SLIDE 1 Unit 5: More Collisions 1
Unit5: More Collisions
Mike Chantler, 31/8/2008 See Peters “ActionScript 3.0 Animation” 3D Modelling & Animation Module F21MA
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!"
SLIDE 2 Unit 5: More Collisions 2
Detecting collisions
3D Modelling & Animation Module F21MA
Collisions between two objects
- Flash’s ‘hit test’
- Distance based
SLIDE 3 Unit 5: More Collisions 3
Flash’s Hit Test
if(blueBall.hitTestObject(blackBall)) {…….
Perform hitTest in event handler for Event.ENTER_FRAME
Distance Based
d=sqrt( (x1-x2)2 + (y1-y2)2 ) limit2 = (r1 + r2)2 if (x1-x2)2 + (y1-y2)2 < limit2 { …..
SLIDE 4
Unit 5: More Collisions 4
Springing off a fixed ball
3D Modelling & Animation Module F21MA
Springing
ball 1 ball 2
SLIDE 5
Unit 5: More Collisions 5
Detect onset of Springing
dmin = r1 + r2
d = Math.sqrt( (x1-x2)2 + (y1-y2)2 )
if (d < dmin) { …. }
(x1, y1) (x2, y2)
Calculate Springing Point
dmin = r1 + r2 θ = Math.atan2((y2-y1), (x2-x1))
tx = x1 + dmin * Math.cos(θ) ty = y1 + dmin * Math.sin(θ) t = (tx, ty)
θ
(x1, y1) (x2, y2)
SLIDE 6
Unit 5: More Collisions 6
Calculate Springing Point
t = (tx, ty)
Calculate acceleration ball 2
t = (tx, ty) ax2 = (tx – x2) * cspring ay2 = (ty – y2) * cspring (x2, y2)
SLIDE 7
Unit 5: More Collisions 7
Calculate new ball 2 position
t = (tx, ty) vx2 += ax2; x2 += vx2 vy2 etc. (x2, y2)
Until Spring Stops
t = (tx, ty) (x2, y2)
SLIDE 8 Unit 5: More Collisions 8
Calculate new position as before
vx2 = (tx – x2) * cspring vy2 = (ty – y2) * cspring (x2, y2)
Example
- See bubble.as in ch09 of Peters’ download
– http://www.friendsofed.com/downloads/1590597 915/FoundationAS3Animation.zip
SLIDE 9
Unit 5: More Collisions 9
Note
Blue ball remains stationary. To calculate movement in blue ball we need momentum/energy conservation rules [see Peters].
Billiard Ball Physics
3D Modelling & Animation Module F21MA
SLIDE 10 Unit 5: More Collisions 10
Basic Equations
p = m * v (mass * velocity)
(& note that p is a vector)
Total p before collision = total p after m0*v0 + m1*v1 = m0*v0final + m1*v1final
b0 b1 b0 b1
Basic Equations
KE = ½ * m * v 2
Total KE collision = KE after m0*v0
2 + m1*v1 2 = m0*v0final2 + m1*v1final2
b0 b1 b0 b1
SLIDE 11 Unit 5: More Collisions 11
Basic Equations
v0final = v1final = (m0 - m1)*v0 + 2*m1*v1 m0+ m1 (m1 – m0)*v1 + 2*m0*v0 m0+ m1
b0 b1 b0 b1
Implementation
- Simple to implement in 1 dimension
v0final = v1final = (m0 - m1)*v0 + 2*m1*v1 m0+ m1 (m1 – m0)*v1 + 2*m0*v0 m0+ m1
b0 b1
y-axis
SLIDE 12 Unit 5: More Collisions 12
Implementation
b0 b1
y-axis
- See Billiard2.as and Ball.as in ch11 of
Peters’ download
– http://www.friendsofed.com /downloads /1590597915 /FoundationAS3Animation.zip
Sanity check
v0final = v1final = (m0 - m1)*v0 + 2*m1*v1 m0+ m1 (m1 – m0)*v1 + 2*m0*v0 m0+ m1
b0
b1
SLIDE 13 Unit 5: More Collisions 13
Sanity check
v0final = v1final = v1final = (m0)*v0 m0 (– m0)*v1 + 2*m0*v0 m0
b0
b1
b0
b1
–v1 + 2*v0
2D Billiard Ball Physics
3D Modelling & Animation Module F21MA
SLIDE 14
Unit 5: More Collisions 14
The 2D case At point of collision
SLIDE 15
Unit 5: More Collisions 15
Rotate axis of collision to x-axis Rotate axis of collision to x-axis
SLIDE 16 Unit 5: More Collisions 16
Rotate axis of collision to x-axis
Resolve velocities into x & y components
vx = vsin θ vy = vcos θ
θ
SLIDE 17 Unit 5: More Collisions 17
Remember the Coord-system
θ (x1,y1)
x y
x1 = rsinθ y1 = rcosθ r
second quadrant x1 is negative and y1 is positive
Resolve velocities into x & y components for other ball
vx = vsin θ vy = vcos θ
θ
SLIDE 18
Unit 5: More Collisions 18
Consider only x components
Apply 1D equations to x final components
v0final = v1final = (m0 - m1)*v0 + 2*m1*v1 m0+ m1 (m1 – m0)*v1 + 2*m0*v0 m0+ m1
b0 b1
SLIDE 19
Unit 5: More Collisions 19
Result: final x components
Add original y components back in
SLIDE 20 Unit 5: More Collisions 20
Rotate everything back Implementation
- See Billiard4.as in ch11 of Peters’ download
– http://www.friendsofed.com /downloads /1590597915 /FoundationAS3Animation.zip
SLIDE 21 Unit 5: More Collisions 21
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.
Lab: ex5.1
3D Modelling & Animation Module F21MA
SLIDE 22 Unit 5: More Collisions 22
Create a Test
- For a stationary ball and a draggable ball
if(blueBall.hitTestObject(blackBall)) {…….
Perform hitTest in event handler for Event.ENTER_FRAME
Lab: ex5.2
3D Modelling & Animation Module F21MA
SLIDE 23
Unit 5: More Collisions 23
Implement spring based collisions
t = (tx, ty) ax2 = (tx – x2) * cspring ay2 = (ty – y2) * cspring (x2, y2)
End
3D Modelling & Animation Module F21MA