Can anyone explain what I’m doing wrong or why this isn’t working. With a real Newtons cradle, only the left and right balls should be moving. The 3 middle balls should remain stationary. I can’t figure out why 4 balls are moving and what I need to adjust.
--# Main
-- Newtons cradle
function setup()
supportedOrientations(LANDSCAPE_LEFT)
displayMode(FULLSCREEN)
-- create 5 hanger points, a1-a5
a1=physics.body(CIRCLE,0)
a1.type=STATIC
a1.x=WIDTH/2-200
a1.y=700
a2=physics.body(CIRCLE,0)
a2.type=STATIC
a2.x=WIDTH/2-100
a2.y=700
a3=physics.body(CIRCLE,0)
a3.type=STATIC
a3.x=WIDTH/2
a3.y=700
a4=physics.body(CIRCLE,0)
a4.type=STATIC
a4.x=WIDTH/2+100
a4.y=700
a5=physics.body(CIRCLE,0)
a5.type=STATIC
a5.x=WIDTH/2+200
a5.y=700
-- create 5 balls, c1-c5
c1=physics.body(CIRCLE,50)
c1.type=DYNAMIC
c1.restitution=1
c1.x=WIDTH/2-200
c1.y=200
-- give the left ball a velocity to the left
c1.linearVelocity=vec2(-200,0)
c2=physics.body(CIRCLE,50)
c2.type=DYNAMIC
c2.restitution=1
c2.x=WIDTH/2-100
c2.y=200
c3=physics.body(CIRCLE,50)
c3.type=DYNAMIC
c3.restitution=1
c3.x=WIDTH/2
c3.y=200
c4=physics.body(CIRCLE,50)
c4.type=DYNAMIC
c4.restitution=1
c4.x=WIDTH/2+100
c4.y=200
c5=physics.body(CIRCLE,50)
c5.type=DYNAMIC
c5.restitution=1
c5.x=WIDTH/2+200
c5.y=200
-- create a joint for each ball and hanger
physics.joint(REVOLUTE,c1,a1,a1.position)
physics.joint(REVOLUTE,c2,a2,a2.position)
physics.joint(REVOLUTE,c3,a3,a3.position)
physics.joint(REVOLUTE,c4,a4,a4.position)
physics.joint(REVOLUTE,c5,a5,a5.position)
end
function draw()
background(40, 40, 50)
-- draw each ball
fill(255)
ellipse(c1.x,c1.y,c1.radius*2)
ellipse(c2.x,c2.y,c2.radius*2)
ellipse(c3.x,c3.y,c3.radius*2)
ellipse(c4.x,c4.y,c4.radius*2)
ellipse(c5.x,c5.y,c5.radius*2)
-- draw a line from each hanger to each ball
stroke(255, 255, 255)
strokeWidth(3)
line(c1.x,c1.y,a1.x,a1.y)
line(c2.x,c2.y,a2.x,a2.y)
line(c3.x,c3.y,a3.x,a3.y)
line(c4.x,c4.y,a4.x,a4.y)
line(c5.x,c5.y,a5.x,a5.y)
end