Newton's Cradle Programming Help!
Processing Programming Media Art Design
Processing Forum
zoho-static/files/fileId=25080000000003086-forumGroupId=25080000000003001.x-icon
I think that there are two ways to approach this:

â¢ animate the left end ball going up and coming down, then do the same with the right end ball

â¢ do the physics properly where each ball acts under gravity and bounces correctly when it hit a neighbouring ball

The latter is much much harder but, if you get it right, much more realistic. It is harder because computer animation is frame-based, whereas real life happens continuously.

So, looking at the first way, you need to have the ball on its string describing a circular arc around its pivot (the top of the string). Any point on an arc has the (x, y) co-ordinates

(stringLength * cos(rotationAngle), stringLength * sin(rotationAngle))

In real life the rotationAngle doesn't change by the same amount from one frame to the next but, for your first go, don't worry about that.

modeling the physics would be preferred as then if you moved 2 balls to start the momentum would be transferred to 2 on collision. 2 stopping and the other 2 moving. If 3 then 1 stopping and 3 still moving, 4 would just be 4 pendulums.

If you raised 1 at each end then they both would just bounce, if timed perfectly.

You might first just try modeling a single ball and

1. raising it and letting it swing

2. applying a force to it and seeing it swing.

Once you have that add some kind of decay due to friction to decrease the amplitude and you have your movement model.

Then you just need to transfer the momentum upon collision, with is part 2 above. Each ball can only transfer/receive momentum equal to its mass so some keep swinging, others start, and some stop.

Note if you raise ball 1 & 2 and hold ball 3 fixed, the momentum transfer to ball 4 is 2x so it would really fly, as no momentum is retained by 3.

see http://en.wikipedia.org/wiki/Pendulum for ideas on the math.