Oct 19 / dominicscaife

Flash Newton’s Cradle

I did a few online Flash training courses and to reinforce my learnings I built a Flash logo/toy for my companies website. Check out my flash creation here:
Website Design Studio.

My notes are below, not written up properly yet.

Newton’s Cradle documentation

Variables
angle = angle ball dropped from
damping = damping coefficient
fac = frequency of the harmonic motion
t = time
xo
yo
aa = when draggin the mouse this is the distance the mouse is from the ‘at rest’ position for the ball you are dragging
it is used to calculate the angle of rotation of the pendulum
cst
i
to = pi*t/180 – the rotation of the pendulum in radians at time t.
ball1 = is ball1 moving boolean 0 or 1
ball2 = is ball2 moving boolean 0 or 1
ball3
ball4
ball5

balls – this variable determines the intial state of the balls possible values are:

1 – leftmost ball moved left so 1 ball moving
50 – leftmost ball moved right so 5 balls moving
2 – penultimate left ball moved left so 2 balls moving
40 penultimate left ball moved right so 4 balls moving
3 – middle ball moved left so 3 balls moving
30 – middle ball moved right so 3 balls moving
4 – and so on
20
5
0 – continuing the pattern would expect this to be 10 but its 0 – no idea why this assume inconsistant prgramming

pendulum.isw – a boolean that determines if the pendulum is moving freely or being moved by the user

The rotation of each pendulum is defined by

ball1*(Math.exp(-damping*to))*angle*Math.sin(fac*to)

ball_n *( Exponential -damping*to)*angle Sin (fac* to)

ball_n: this is 0 or 1 dependant on whther it has motion
damping = damping coefficient
to = time
fac = frequency of the sine curve

The impact point is done via if statements it’s rather inelegant code but works great!
At present this uses 5 variables ball1,ball2,3,4,5 etc to store the state of the ball. If the ball is moving the variable is 1 if not it’s 0.
Impact occurs when the pendulum.rotation variable reaches <0.01 (changed to 0.5 by me)
The ball1,ball2,ball3,4,5, variables are reset when this condition is reached as per classical physics conservation of momentum
i.e. if ball1=1,ball2=1,ball3=0,ball4=0,ball5=0 when condition is met they are changed to
ball1=0,ball2=0,ball3=0,ball4=1,ball5=1. and the animation continues. I think a matrix multiplication to reflect the array of ball
states on the y axis would be more elegant.

The user interface code resides within the movieclip objects (pendulum1,2,3, etc.
We only test the mouse x position to determine how the balls react to the user.
When you click the mouse the animation stops and the mouse position is stored in old_mouse var
isw is set to 0. this variable is 1 when the pend is moving freely and 0 when under user control

We then process any movement of the mouse. if the mouse is moving right xmouse>old_mouse and all balls will be moved
so the ball1,ball2,ball3 variable states are set to 1 as they are all moving. Also balls is set to 50 this variable tracks te initia state of the balls.
The angle of rotation is determined by using the aa variable which is the x distance from the current mousex to the at rest postion of the ball we are moving.

So if we need to change the size of our balls we need to adjust the values in the actionscript within each movieclip.

Code Changes

Cleaned up collision code to make it more consistant
Changed the integer pixel value used in the object code to determine how the ball is being dragged from an integer to the mouse_down value.
Changed the integer pixel value used in the object code to determine which ball is being clicked from a integer value to a value which uses the objects properties

Bottom line of changes: Now if we change the size of the objects we don’t need to change the code.so we have a solution that works at whatever resolution you need.

Leave a Comment