8.5

Problem set 3: Moving rockets

This assignment is due on Wednesday, January 26 at 11:59pm. Submit it using Handin as assignment ps3. Your submission is only accepted if the message “Handin successful” appears. Corrections can be presented until Friday, February 18.

This problem set builds on the skills that we introduced in Lectures 2−4. To encourage you to review those lectures, your grade on this assignment will be limited by your grade on those lectures at the time you submit (or correct) this assignment. Remember that most lecture exercises can be resubmitted at any time.

Ask for help and start early on problem sets!

Important Note: Remember to follow the design recipe whenever you design or write a function. In particular, every type mentioned in a signature must be introduced by a data definition, except for these well-known types: Number, Image, String, Color.

Let’s use what we’ve learned to animate a rocket launch.

We’ll solve this problem in two parts, horizontal and vertical, then combine them. The examples we animated in class and in the textbook prologue can guide us to solve both parts of the problem. This divide-and-conquer approach is possible because force applied along one axis to a moving body only affects motion along that axis and not any other axis. So the horizontal motion of a rocket is decoupled from its vertical motion, and we can write down two completely independent equations. Only the equation governing vertical motion involves gravity.

First make sure that you require the 2htdp/image and 2htdp/universe libraries.

In every problem set, clearly delimit your work for each exercise using a comment like “; Exercise 1”.

Exercise 1 Your program will animate a rocket fired with an initial speed init-speed and initial angle to the horizontal init-angle.

Define init-speed and init-angle as constants. We define the initial speed and angle as numbers rather than functions because they do not change as the rocket flies. Use an initial speed of 1.5 and an initial angle of 0.3 * pi to start. Once your program is running, you can try using different values to see how your animation behaves. Note here that we are using an angle specified in radians (so 0.3 * pi means 54 degrees).

Exercise 2 Since only the initial speed will be specified, you will need to calculate the x and y components of the initial velocity. These may be calculated via

init-x-vel = init-speed * cos(init-angle)

init-y-vel = init-speed * sin(init-angle)

Define the initial horizontal velocity init-x-vel and the initial vertical velocity init-y-vel using the initial speed init-speed and the initial angle init-angle. We define the initial horizontal and vertical velocity as numbers rather than functions because they do not change as the rocket flies.

Making a function is just saying math in a computer's language

Exercise 3 The rocket experiences a gravitational acceleration in the vertical direction. Therefore, the altitude of the rocket is this function of the time t:

altitude = init-y-vel * t - 0.5 * 0.003 * t * t

where init-y-vel is the initial velocity in the vertical direction, and 0.003 is the gravitational acceleration constant.

Design a function y-pos which calculates the altitude as a function of time t. For example, (y-pos 0) should produce 0.

To test more examples, you may need to use check-within instead of check-expect, because the functions cos and sin produce results that are approximate. Recall that check-expect takes two inputs: the expression to test and the expected result. To handle approximate results, check-within needs a third input: a small number like 0.001 that means how much approximation error to allow.
(check-within (sin (* 0.3 pi)) 0.809 0.001)

Exercise 4 The rocket does not experience any acceleration in the horizontal direction. Therefore, it has constant horizontal velocity, and the horizontal position is a simpler function of the time t:

horizontal position = init-x-vel * t

where init-x-vel is the initial velocity in the horizontal direction.

Design a function x-pos that calculates the horizontal position as a function of time t. For example, (x-pos 0) should produce 0.

Exercise 5 Design a function draw-sprite that draws a scene with a ‘sprite’ on it. This function should be called draw-sprite and take two inputs, the horizontal position and the altitude. The term ‘sprite’ is commonly used in computer graphics to refer to a small image that moves around. You may use any small image of your choice, such as a small circle, or even the rocket image. You should use place-image to place a sprite image on a scene that you create using empty-scene. A scene size of 800 wide by 300 tall should work well for the numbers provided above.

Remember, the place-image function measures the Y position from the top down, but the altitude measures the vertical position starting at the bottom. So, you will need to convert the given altitude into a Y position that the place-image function can understand. This conversion can be performed as follows:

image-y = scene-height - altitude

Here scene-height is the height of your scene, altitude is the altitude (the vertical position starting at the bottom), and image-y is the third input you pass to place-image.

Hint: Follow the design recipe. Because draw-sprite produces images, in order to write examples for draw-sprite, you need to first make desired images appear in the Interactions Window. More specifically, (draw-sprite 0 0) should produce an image that places the rocket in the lower-left corner. Also, (draw-sprite 20 50) should produce an image that places the rocket above the lower-left corner and a little bit to the right.

Exercise 6 Finally, use all three functions you designed to design a new function launch that draws an image at the correct horizontal and vertical positions, as a function of time t. For example, (launch 0) should produce an image that places the rocket in the lower-left corner.

Your definition of launch should not mention multiplication (*) or subtraction (-) directly anywhere.

Use your launch function with the animate we discussed in class to produce an animation of a rocket launching with an initial angle to the horizontal and an initial speed.

You should see your rocket starting off at the lower left. It should then move up and right, and eventually, it should fall, but continue moving right, and eventually move off your scene.

Help other students by answering this ungraded question: what did you have to learn to finish this problem set that we didn’t teach? Post your answer to Discord in the #ps3 channel, or put it as a comment at the bottom of your Handin submission.

Challenge The rest of this problem set is not required for C211 students.

The goal of this challenge exercise will be to produce an animation that displays the different trajectories that the rocket might take if we vary the initial angle, starting at 0:

Here are a few suggestions. Design init-x-vel-fn and init-y-vel-fn as functions which take the initial angle and return the appropriate velocity. Also, design new versions of x-pos and y-pos which take the initial angle as an additional argument.

Next design a function overlay-sprite which takes an initial angle, a time and an image and places a rocket onto the given image at the position in which it would be at the given time for the given angle. Finally, design a function draw-trail which takes an initial angle and draws the rocket at several of the positions it would occupy during its flight.

Run your animation. To improve the viewing of the sequence of trails, we suggest “slowing it down” by choosing a smaller unit for the input initial angle (e.g. 1 degree = pi / 180 radians).