Simulated Pachinko Machine

By Dustin Rhodes

email: dcrhodes@ucsc.edu

class: cmps161 Winter 2010


For my final project I simulated a Pachinko Machine. Balls are dropped from the top and trickle down bouncing off of various pins and bumpers until they land somewhere along the bottom.



Executable: Download
Source: Download




Controls:
a = add new ball
+/- = increase/decrease gravity
space = pause simulation and set v=0
click and drag to move any pegs,balls, or polygon points
reopen for a new layout of pegs








Necessary Tasks (completed)



Plan for Execution


To implement collision between balls and the various parts of the playing field I used the equation of a circle because the spheres and pegs are both circular on the 2D field of the playing area. Once a collision is detected a force is calculated based on the collision direction and that force is applied to either just the ball in case of a ball on peg collision or both balls in case of a ball on ball collision. All collisions are mostly elastic but do contain some inelasticity and momentum is conserved. A small amount of friction was added. To add friction we apply a small force in the opposite direction of ball movement.




Timeline


Week 1: Complete program 3 in an easily extensible way. IE. Walls can be drawn anywhere and it is easy to add a peg element.


Week 2: Transform program 3 into a ball falling through many many pegs with walls at the bottom and sides.


Week 3: Add friction if necessary. Fix any imperfections with collisions between balls and pegs or balls and each other. Start working on different elements.


Week 4: Work on adding extras and adding extras that make the program look nice.










Physics


Ball vs. Ball

A collision between two balls occurs when the distance between their centers is less then their combined radii. After a collision is detected the new velocities of the two balls must be calculated. To calculate this the collision is broken up into two 1 dimensional collisions one of which is parallel to the radii and the other is perpendicular. In other words for any two dimensional collision we can rotate our access so that it can be viewed as a one dimensional collision. Once we have the 1 dimensional equation we must solve for the new velocities using conservation of momentum and conservation of energy...


m1*v1+m2*v2 (before)=m1*v1+m2*v2(after)

1/2m1*v1^2+1/2m2*v2^2 (before) = 1/2m1*v1^2+1/2m2*v2^2 (after)


for my program I set m=1 for each circle. This simplifies the equation into...


v1+v2 (before) = v1+v2 (after)

v1^2+v2^2 (before) = v1^2+v2^2 (after)


We know v1 and v2 before so we can plug these in and solve for the two unknowns v1 and v2 after. This will give us the new velocities parallel to the collision and the ones perpendicular to the collision remain unchanged.


Ball vs. Peg

To detect a collision between a ball and a peg is the same as between a ball and a ball. Once a collision is detected we solve the same equations as before but the peg is given an infinite mass. This means that the peg will not move no matter how fast the ball is coming at it.


m1*v1+m2*v2 (before)=m1*v1+m2*v2(after)

1/2m1*v1^2+1/2m2*v2^2 (before) = 1/2m1*v1^2+1/2m2*v2^2 (after)


plugging in 1 for m1 and infinite for m2 we see that v2 must remain 0


v1+infinite*0 (before) = v1+infinite*v2 (after)

v1 = v1 +infinite*v2

v1/infinite = v1/infinite + v2

0 = 0 +v2

v2=0


after solving for v2 we must calculate the new v1


1/2m1*v1^2+1/2m2*v2^2 (before) = 1/2m1*v1^2+1/2m2*v2^2 (after)

v1^2 + 0 (before) = v1^2 + 0 (after)


therefore we see that the magnitude of v1 remains the same but the velocity is negated. We now convert back to our original coordinates just like in the previous case.


Ball vs. Polygon

To detect a collision between a ball and a polygon we find the closest point on the lines that make up the polygon. If this point is closer then the balls radius we have a collision. Again we can assume that the mass of the polygon is infinite so we find that the new velocity is negated in the direction of the collision and stays the same perpendicular to the collision.