Wednesday, February 19, 2014

Another LiveCode First Project: Follow the Bouncing Ball

I continue to introduce faculty and students at UGA to LiveCode. For a recent workshop, I came up with another first project example, which I call "Follow the Bouncing Ball." This is the one that the attendees voted to have me build for them during the workshop.

It's a very graphical project where an animated soccer ball bounces around the screen based on some information provided by the user. This information is simply the horizontal and vertical speed of the ball.

I made a YouTube video of me demonstrating how to build this project from scratch, so I'm not bothering to go into any detail here on how it works.

Here is a snapshot of the project:

[ Get the free LiveCode Community version. ]

What is rather cool about this project is that it shows some very fundamental and important physics concepts and principles. It's a fun way to show the hierarchical relationship between an object's location, distance traveled, velocity of the object, and acceleration of the object. The study of a moving object is a great example of rate of change problems. The distance an object travels shows its change in position. The velocity (speed and direction) of an object shows its change in distance over time, and acceleration denotes the change in velocity. This project didn't go as far as demonstrating acceleration, but it did a good job of showing velocity as consisting of both the speed the soccer ball is traveling and direction in which it is moving. Of course, I know I run the risk of alienating people if I go too far with the physics explanations! 

(But, the history of the discovery of calculus is closely tied to dynamic problems such as this. Velocity is the first derivative of distance and acceleration is the second derivative of distance. Ergo, acceleration is the first derivative of velocity. How cool is that!)

I built this project only using local variables in order to try to show the difference between global and local variables. 

Here is the script of the button "Bounce":

on mouseUp
   hide me
   put item 1 of the location of button "ball" into x
   put item 2 of the location of button "ball" into y
   put line 1 of field "speed" into varSpeedx
   put line 2 of field "speed" into varSpeedy
   repeat until the mouseclick
      if x > 320 then put -1*varSpeedx into varSpeedx
      if x < 0 then put -1*varSpeedx into varSpeedx
      if y > 480 then put -1*varSpeedy into varSpeedy
      if y < 0 then put -1*varSpeedy into varSpeedy
      wait 1 millisecond
      add varSpeedx to x
      add varSpeedy to y
      set the location of button "ball" to x, y

   end repeat
   show me
end mouseUp

Sir Isaac Newton would be proud.

No comments:

Post a Comment