Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use Python plz and given instructions- to make code send me exact code from these instructions and A screenshot of output of code running. 3.7

Use Python plz and given instructions- to make code send me exact code from these instructions and A screenshot of output of code running. image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
3.7 "Ball in a Box" Assignment In this exercise, you will create an animation of a ball bouncing in a box. You can finish this at home if you run out of time in your lab section. The program is due at the beginning of your next lab period. Submit your py file as well as images showing). You may complete this assignment outside of class and turn in the assignment for individual credit. 1. Create a new program and make a sphere object: ball sphere(pos-vector(-5,0,0), radius 05, color-color.red) 2. Create a "wall" by making a box object: wallR box(pos vector(6,0,0), size-(0.2,44), color-color green) 3. Animating the ball .We would like to make the red ball move across the screen and bounce off the green wall. We can think of this as displaying "snapshots" of the position of the ball at successive times as it moves across thoe screen. To specify how fast the ball moves, we need to specify its velocity and how much time has clapsed .First, we'll define a short time interval. We'll call it "dt. Type this line at the end of your program: dt = 0.05 .Now define the ball's velocity. Add the following line: ball.velocity = vector(2,0,0) Notice that we have defined the velocity as a vector. In this case, we've specified that the ball moves only in the x-direction (the "y" and "z" components of the velocity are zero). Run your program. Nothing happens! That's because we havent yet given the computer instructions on how to use the velocity to update the ball's position. Since distance-speed*time, we can calculate how far the ball moves (its displacement) in the time interval, dt. To find the ball's new position, we add the displacement to the old position ball.pos ball.pos + ball.velocity'dt (Note that, since ball.velocity is a vector, ball position is also a vector.) The last line you added to your code may look a bit odd to you. How can we set ball pos to ball pos plus something else? Isn't that like saying, "1=1+2" which is clearly false? NO! The meaning of the equal sign here is different than its meaning in arithmetic. In the program, the equal sign means, "assign a value to this variable". You are really giving the following instructions: "Find the location in memory where the valuc of the variable ball pos is stored. Read up that value, add the value ball.velocity "d to it, and re- store the new value in memory." In other words, we are telling the computer to update the value of . . ball pos to a new value. This is how we animate the bell. We give the computer successive updates to the ball's position which is changing in time as the ball moves Run your program. Again not much happens! The problem is that the program only took one time step. We told it to update the ball's position only once. But, we want the program to take many timesteps so the ball keeps moving. To accomplish this, we use a while loop. . . Delete the last line of your program and type while (1 1): Indented under this"while statement, type: ball.pos ball.pos +ball.velocity'dt Note: the IDLE editor should automatically indent the cursor for you when you press enter after the "while" statement. Alternatively, you can press the TAB key. The indentation is important. It tells the computer which statements are "inside the loop. All indented statements after the "while" statement will be executed every time the loop is exccuted Your program should now look like this: from visual import ball sphere(posevector(-5,0,0), radius-05, color-color red) wallR box(pos-vector 6.0,0), size (02,44), color-color green) dt:0.05 ball.velocity vector(2,00) while 1: . ball.pos ball.pos + ball.velocity dt . Run your program. You may see the hall moving very rapidly across the screen. Oa my computer, it's too fast to see. To slow it down a little, insert the following statement inside the loop (after the"while" statement) rate(100) You should now see the ball move from left to right across the screen 4. Making the ball bounce: Logical tests When you ran your program, you probably noticed that the ball didnt hounce off the wall at all I just kept going left to right. That's because we have to tell the computer to make the hall bounce off the wall. First, we nced to detect a "collision" between the ball and the wall. A simple approach is to compare the x-coordinate of the ball to the x-coordinate of the wall, and reverse the x-component of the ball's at the moment they are equal. We can use a logical test to do this if ballx > wallR.x: . ball.velocityx--ball.velocity a The indented line after the ."ir statement is only executed if the statement is true. If the result of the logical test is false (that is. if the x-coordinate of the ball is not greater than the x-coordinase of the wall). the indented line will be skipped. We would like this logical test to be caried out at each time step (every time the ball is at a new position Therefore, we must insert this statement inside the "while" loop. Your program should now look like this: from visual import ball = sphere(pogrvector(-5,0,0), radiusas, color-olorred) wallR boxipos-vector 6,00), size (0.2,44), color color green) dt-0.05 ball.velocity-vector(2,00) while 1: . rate(100) ball.pos ball pos + ball.velocity dt if ballx> wallR.x: ball.velocityx--ball.velocity Run your program. The ball should now reverse direction when it encounters the wall. Notice that our logical test is not very sophisticated since the ball penetrates the wall part way before reversing direction. We could fix this but won't for now. 5. Add another wall at the left side of the display and make the ball bounce off that wall also so it bounces between the two wails. 6. Make the ball move at an angle by changing ball. velocity such that it has a non-zero y-component. Now that the ball moves at an angle, it might be missing the wall! Make the walls bigger (change size (2, 12, 12) in the "box statements). Wetl also fix this later by adding a horizontal wall above the other walls. 7. Visualizing velocity and leaving a trail Before adding more walls, let's first add an arrow to show the velocity of the ball. Create an arrow before the "while statement whose tail is at the ball location to represent the ball's velocity: bv e arrow(pos ball.pos, axis ball.velocity, color-color .yellow) Update the position and axis of the arrow by adding the following lires inside the "while loop bv pos # ball.pos bv axis ball.velocity Run your program. ball hits a wall. The arrow should now move with the bell and should change direction every time the . To visualize the trajectory of the ball, we can make it leave a trail. To do this we create a "curve" object (before the "while" loop): ball.trail = curve(coloraball.color) At the end of the while loop (inside the loop), add the line: ball.trailappend(pos-ball.pos) This will add a point to the trail at the latest ball position Add top and bottom walls to your program so that they touch the horizontal walls. Give these a different color to make the 3D nature of the object more visible. Make the ball bounce off to top and bottom walls by adding the appropriate "if statements in the "while loop 8. Add a back wall and an "invisible" front wall. That is, for the front wall, don't create an actual wall object, but include an "if statement to prevent the ball from coming through the front. 9. 10. Give your ball components of velocity in the z-direction as well to make it bounce off all six walls. Important!! Don't forget to finish the "ball in a box" assignment before your next lab session. You will have to copy or email your program file to transfer it to your own computer if you did it on a lab computer. You will need to submit all portions of the lab and required programs to get full c submissions must be uploaded to Desire2L earn(BrightSpace) before the due time. No late assignments will be accepted. redit. All If you need help finishing it or installing VPython during the week, you can see me during office hours! This assignment counts for 40% of your lab 4 grade

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

3. What should a contract of employment contain?

Answered: 1 week ago

Question

1. What does the term employment relationship mean?

Answered: 1 week ago