Lesson goal: Animate a projectile in flight with simple physics

Previous: Animate a more complex shape | Home | Next: Object with air-drag

As in this lesson you saw how a single pixel could be moved straight across the screen. Of course one of the challenges of making an animation is in determining what should guide the object in how it moves. In other words, where should the object be drawn from one moment to the next? Well in things like video games and movies, the objects are made to move according to the laws of physics. Let's do that here.

Basic physics allows one to predict how a ball will fly through space (with no air in the way). If you launch the ball from position $(x,y)$ with a speed of $v$, at an angle $\theta$ with the ground, then the ball will be at a new $(x,y)$ coordinate some $\Delta t$ in the future according to (given that $v_x=v\cos\theta$ and $v_y=v\sin\theta$): $$x=x+v_x\Delta t$$ and $$y=y+v_y\Delta t+\frac{1}{2}g\Delta t^2.$$ Although $v_x$ will stay the same throughout the flights, $v_y$ will change via $$v_y=v_y+g\Delta t.$$ In all of these equations, $g$ is the acceleration due to gravity, which is $9.8$m/s$^2$ on earth. Here is some code that will make a pixel fly through an airless world. See if you can fix it up and get the little ball to fly!

Now you try. Try fixing $\theta$, $v$, $g$ and the pset lines.

Type your code here:


See your results here:

This code will not run. You need to fill in some launch angle $\theta$ in the theta= line and some launch speed in the v= line. Next you have to put in some value for $g$, or gravity. Try $9.8$ for here on earth. You might try $g$ for other planets later. Next, you have to fix the pset( ) line. If the ball is at $(x,y)$ at any given time, what will this line be? You'll be sure to test the range for $\theta=30^\circ$ and $\theta=60^\circ$, right? Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.