Modeling Motion!
Goals:
- Develop algorithms to simulate realistic motion
Coding Snippets:
- An effective technique for modeling motion involves focusing on a sprite's coordinates.
- The key is to track not only the coordinates (
x
andy
), but also the rate of change of those coordinates (dx
anddy
). - Computing
x
andy
anddx
anddy
separately simplifies the conditional logic needed when a sprite reaches a boundary (such as the edge of the screen). - Different formulas for computing
dx
anddy
result in different types of movements.
Math and Computer Concepts:
- There are many ways to model motion, each with its own advantages and disadvantages. This lesson focuses on an approach that models motion based on position and change in position.
- The variable names
dx
anddy
used in the coding snippet come from math. These names stand for "delta x" and "delta y", math-speak for "change in x" and "change in y".
Activities:
Save each of the following in a folder called Motion.
- Make an image of a Balloon gradually float up and across the screen. Add some randomness to make the motion more realistic.
- Acceleration: Instruct a rocket to accelerate as it moves up the screen. Do this by adding ever-increasing changes to its height. The code used to accomplish this need not be complicated. In fact, something as simple as the snippet on the right will get the job done.
- BouncingTurtle: Write a script that simulates a turtle being dropped from the top of the screen. The sprite should accelerate as it falls, until it reaches the bottom of the screen, at which point it should reverse direction. As it rises, it should gradually lose speed, until it ultimately stops rising and starts falling again.
- UsingObjects: Make a shark swim back and forth across the screen, reversing direction each times it moves out of the visible area. The point of this activity is to stay organized by saving properities of the sprite in an object. These properties include the
x
andy
values of its location, the rate at which its position changes (i.e.,dx
), and itsmirror
status (i.e., whether it is set totrue
orfalse
). - Sharks: Use the code from the previous activity to make multiple sharks move about the screen. Each sprite should have its own characteristics, i.e., location, speed, size, and so on.