`Newton's Cradle by Ric
 
set_display()
make_ground()
make_cradle()
 
do
 
  move_cradle()
  move_camera()
  sync
 
loop
 
 
function free_image()
 
  `find a spare image number
  repeat
    inc n
  until image exist(n)=0
 
endfunction n
 
function free_object()
 
  `find a spare object number
  repeat
    inc n
  until object exist(n)=0
 
endfunction n
 
function set_display()
 
  `set the display mode, sync rate and lighting
  set display mode 1024,768,32
  sync on
  sync rate 60
  color backdrop 0
  autocam off
  hide mouse
  make light 1
  position light 1,30,10,-10
  set light range 1,100
 
endfunction
 
function make_ground()
 
  `make a checked texture for the ground
  groundimage=free_image()
  create bitmap 1,99,99
  ink rgb(10,10,10),0
  box 0,0,99,99
  ink rgb(100,100,255),0
  box 0,0,49,49
  box 50,50,99,99
  get image groundimage,0,0,99,99,1
  delete bitmap 1
 
  `make a translucent plane for the ground and texture it with our image
  ground=free_object()
  make object plain ground,100,100
  texture object ground,groundimage
  scale object texture ground,10,10
  pitch object down ground,90
  set alpha mapping on ground,90
 
endfunction
 
function make_cradle()
 
  `make a texture for applying sphere mapping to the balls
  reflection=free_image()
  create bitmap 1,99,99
  box 0,0,99,30,rgb(100,100,100),rgb(50,50,50),rgb(100,100,100),rgb(50,50,50)
  box 0,30,99,99,rgb(200,200,220),rgb(100,100,100),rgb(200,200,220),rgb(100,100,100)
  get image reflection,0,0,99,99,1
  delete bitmap 1
 
  `make a grey texture for the wires
  grey=free_image()
  create bitmap 1,10,10
  ink rgb(80,80,80),0
  box 0,0,10,10
  get image grey,0,0,10,10,1
  delete bitmap 1
 
  `make the frame and its reflection out of boxes
  beam=free_object()
  make object box beam,30,0.5,0.5
  position object beam,0,25.75,-10
  beam=free_object()
  make object box beam,30,0.5,0.5
  position object beam,0,25.75,10
  beam=free_object()
  make object box beam,30,0.5,0.5
  position object beam,0,-25.75,-10
  beam=free_object()
  make object box beam,30,0.5,0.5
  position object beam,0,-25.75,10
  post=free_object()
  make object box post,0.5,26,0.5
  position object post,-15,13,-10
  post=free_object()
  make object box post,0.5,26,0.5
  position object post,15,13,-10
  post=free_object()
  make object box post,0.5,26,0.5
  position object post,-15,13,10
  post=free_object()
  make object box post,0.5,26,0.5
  position object post,15,13,10
  post=free_object()
  make object box post,0.5,26,0.5
  position object post,-15,-13,-10
  post=free_object()
  make object box post,0.5,26,0.5
  position object post,15,-13,-10
  post=free_object()
  make object box post,0.5,26,0.5
  position object post,-15,-13,10
  post=free_object()
  make object box post,0.5,26,0.5
  position object post,15,-13,10
  base=free_object()
  make object box base,0.5,0.5,20
  position object base,-15,0.25,0
  base=free_object()
  make object box base,0.5,0.5,20
  position object base,15,0.25,0
 
  `make some arrays
  dim pivot(5)
  dim ball(5)
  dim reflection(5)
 
  `repeat everything five times for five balls
  for n=1 to 5
    `the pivots, which are hidden, act as the point of rotation.
    `the wires and balls will be glued to the pivots, so that when
    `the pivots are rotated, the wires and balls move accordingly
    pivot(n)=free_object()
    make object cube pivot(n),1
    hide object pivot(n)
    position object pivot(n),5*(n-1)-10,26,0
    `first set of wires
    wire=free_object()
    make object box wire,0.1,20,0.1
    texture object wire,grey
    position object wire,0,-9,-5
    xrotate object wire,-30
    glue object to limb wire,pivot(n),0
    `second set of wires
    wire=free_object()
    make object box wire,0.1,20,0.1
    texture object wire,grey
    position object wire,0,-9,5
    xrotate object wire,30
    glue object to limb wire,pivot(n),0
    `balls
    ball(n)=free_object()
    make object sphere ball(n),5,30,30
    set sphere mapping on ball(n),reflection
    set object specular ball(n),-1
    set object specular power ball(n),100
    position object ball(n),0,-20,0
    glue object to limb ball(n),pivot(n),0
    `copy of each ball, for reflection
    reflection(n)=free_object()
    instance object reflection(n),ball(n)
  next n
 
endfunction
 
function move_cradle()
 
  if 0 `funny bit of code which allows you to globalise a variable from within a function
    global theta#
  endif
 
  `calculate the angle of the pendulum, which increases by a small amount, theta, each loop
  inc theta#,3
  angle#=30*sin(theta#)
 
  `if the angle is negative, apply it to the leftmost pendulum
  if angle#<0
    zrotate object pivot(1),angle#
    zrotate object pivot(5),0
  endif
 
  `if the angle is positive, apply it to the right most pendulum
  if angle#>=0
    zrotate object pivot(1),0
    zrotate object pivot(5),angle#
  endif
 
  `position the ball reflections at the same position as the ball, but with a negative
  `y-value.  Because the balls are glued to limbs, using 'object position' gives a relative
  `rather than an absolute amount - so we use limb position 0 instead.
  for n=1 to 5
    position object reflection(n),limb position x(ball(n),0),-limb position y(ball(n),0),limb position z(ball(n),0)
  next n
endfunction
 
function move_camera()
 
  `moves the camera in a circle
  camradius#=50
 
  if 0
    global camangle#
  endif
 
  inc camangle#,0.1
 
  camx#=camradius#*cos(camangle#)
  camz#=camradius#*sin(camangle#)
  camy#=50
  position camera camx#,camy#,camz#
  point camera 0,0,0
 
endfunction