Question: C++/PHYSICS Step 3. Give the rocket an initial velocity Add a variable for the initial velocity near the beginning of the code vinit = 65;
C++/PHYSICS
Step 3. Give the rocket an initial velocity
Add a variable for the initial velocity near the beginning of the code
vinit = 65;
Then add two lines after g = 9.8 to set the initial velocity:
if (keyIsPressed && key == ' ') { // spacebar g = 9.8; vx = ?????; vy = ?????; } It is up to you to figure out what goes in the question marks (think: trigonometry). When finished your program should behave like this.
Note: For a more authentic bellicose birds experience you can turn off the thrusters by setting Fthrust = 0.0; at the top of the page.
Step 4: Show the expected trajectory
You should be able to calculate the trajectory of the rocket from the kinematics equations we used towards the beginning of the course. In this step you will calculate this trajectory and draw it on the screen using drawPoint(xdraw,ydraw); You may recognize this as the same function we used to do the projectile in the planetoids lab.
Just after drawLine(0,0,width,0); add these variables which are the initial x and y position of the rocket:
x0 = 0.2*width; y0 = 0.05*height; Immediately after this write:
Npoints=1000; for(i=1;i<=Npoints;i+=1) { t = (i-1)*dt; xdraw = x0 + ?????; ydraw = y0 + ?????; drawPoint(xdraw,ydraw); } Fill in the ???? with the terms that give the right trajectory. These will of course depend on t the time variable. Note that to square a variable in this context simply multiply it by itself (for example: t*t). Expressions like t^2 won't work in this case (or in any other C/C++ context).
Once you have figured this out your game should behave like this.
Step 5. Check that 45 degrees gives the longest distance
Add these lines after the "Game Over" but before the noLoop()
drawText(x,width/2,height/2 + 20); drawText(theta,width/2,height/2 + 40);
This will show two pieces of information when the rocket falls below the page. The first line tells you the x value where the rocket landed. The second line gives the angle in radians.
Modify the code to write the angle in degrees instead of radians! Then check to see that 45 degrees gives the farthest distance. (Helpful hint: change the line where the angle of the ship changes so that it doesn't rotate so much every time you press the arrow.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
