CrazyBallTossMoveCamera

From MHCGraphics

Jump to: navigation, search
; Mead example file -- a simple animation of a ball
; tossed from one point to another, but with other
; animated features that are, in some cases, a little
; ridiculous
;
; Jim Teresco
; Computer Science 110
; Mount Holyoke College
;
; $Id: CrazyBallTossMoveCamera.scm 625 2008-04-13 21:38:48Z terescoj $
;
(require (lib "Defs.ss" "Mead"))

; Some image settings appropriate for animations
(tell image
      ; set the filename of the images
      (fileName "CrazyBallToss")
      ; frameNumbers must start at 0
      (frameNumber 0)
      ; set to #t for default behavior: view images,
      ; but here we only want to see the final movie
      (viewResult #f)
      )

; And some standard image properties
(tell image
      (background '(.5 .5 1))
      )

; a convenient little function to create a plaster material
; of a given RGB color
(define (plasterMat rgb)
  ; create a new Material in this function
  (let* ([m (new Material)])
    ; set its properties appropriately
    (tell m
          (type 'plaster)
          (color rgb)
          )
    ; return the newly-created Material
    m
    )
  )

; get a new scene with my own light
(object sun Light
        (intensity 1)
        (translate 0 500 -500))

; and the ground
(object ground Plane
        (material greenPlaster)
        )

(object scene Group
        (add sun)
        (add ground)
        )

; The object we will be animating is a white sphere,
; which we create here and add to the scene
(object ball Sphere
        (material whitePlastic)
        ;(scale .5 .5 .5)
        )
(tell scene (add ball))

; A second ball, that will be rolling along the ground
(object rollingBall Sphere
        (material bluePlaster)
)
(tell scene (add rollingBall))

; and a crazy cube that spins and changes size and bounces
(object crazyCube Cube
        (material magentaPlaster)
        )
(tell scene (add crazyCube))

; we'll put in some axes so we can see what's going on
; when the camera moves
(object axis Cylinder
        (scale .02 50 .02)
        )
(tell scene
      (add axis blackPlaster)
      (add axis blackPlaster (zRot 90))
      (add axis blackPlaster (xRot 90))
      )

; next, we define an adjustment function that sets the
; position of the ball for each frame of our film
(define (moveThings ballPos sunIntensity rollPos rollColor
                    cubeSpin cubeScale cubeBounceHt cameraPos)
  ; we first move the ball according to ballPos
  (tell ball
        ; absoluteXform forgets any previous
        ; transformations of the object.
        ; we have been using relativeXform (the default)
        (absoluteXform
         (compose
          (scale .33 .33 .33)
          (translate (car ballPos)
                     (cadr ballPos)
                     (caddr ballPos))
          )
         )
        )
  ; next, we adjust the intensity of the sun
  (tell sun (intensity sunIntensity))
  ; we can also move the sun, relatively (it's sunset)
  (tell sun (translate 0 -5 0))
  ; next, position the rolling ball according to
  ; our third parameter
  (tell rollingBall
        (absoluteXform
         (compose
          (scale .5 .5 .5)
          (translate (car rollPos)
                     (cadr rollPos)
                     (caddr rollPos))
          )
         )
        )
  ; the ball also changes color, provided in HSV by the
  ; fourth parameter -- create a new material of the
  ; given color
  (tell rollingBall
        (material (plasterMat (hsv2rgb rollColor)))
        )
  ; the crazy spinny bouncy resizing cube
  (tell crazyCube
        (absoluteXform (compose
         ; rotation first
         (yRot cubeSpin)
         ; now scale and account for scaling displacement
         (scale cubeScale cubeScale cubeScale)
         (translate 0 (* cubeScale 50) 0)
         ; and translate by its bounce height
         (translate 0 cubeBounceHt 0)
         ))
        )
  (tell camera
        (pos cameraPos)
        )
  )

; tell the camera to film!
(tell camera
      (pos '(0 100 -600))
      (coi '(0 50 0))
      ; we send a film message which uses morph to create
      ; a series of values to pass to the given function.
      ; The parameters to morph are the lists of values
      ; given after the function name.
      ; we now have several parameters generated
      (film 50 moveThings
            ; first, a bezier path for the ball
            '((-200 50 0) (-100 300 0) (100 300 0) (200 50 0))
            ; next, a series of intensities for the sun
            ; (it's going to get dark while the ball is
            ; in the air)
            '(1 .5)
            ; and next, we'll have a ball that "rolls"
            ; along the ground away from us, linearly
            '((-100 25 -100) (100 25 100))
            ; the ball is also going to morph colors - starting
            ; and ending as red, but going through others
            ; along the way -- colors are HSV here
            '((0 1 1) (360 1 1))
            ; and for the crazyCube, we have three more
            ; parameters, how much it spins, how the size
            ; changes, and how it bounces
            '(0 1080) ; spin three times
            '(.5 2 -.5 .5) ; use a Bezier morph to scale
            '(0 200 0) ; bounce height
            ; and move the camera too!
            '((-500 100 -300) (-400 100 -800) (400 100 -800) (500 100 -300))
            )
      ; now tell the camera to take the frames it just filmed
      ; and paste them together into a movie
      (buildMovie)
      )
Personal tools