CS 101 - Problem Solving and Object-Oriented Programming Lab …blerner/cs101/Labs/Lab2/Lab2.pdf ·...

10
CS 101 - Problem Solving and Object-Oriented Programming Lab 2 - Methods, Variables and Functions in Alice Due: September 23/24 Pre-lab Preparation Before coming to lab, you are expected to have: Read Adams, Chapters 2-3. Used Alice to do the marching toy soldier problem in the book in Section 2.2.2. Used Alice to do the wizard and troll example in the book in Section 2.4.1 and 2.4.2. Used Alice to do the girl and horse example in the book in Section 3.1.1 and 3.4. Introduction to the Assignment Here is the English description of the world you will create and program for parts 1 and 2 of this lab assignment: Two people play volleyball. Generally, they volley the ball nicely back and forth directly to each other. Sometimes one player will hit the ball into the net. When that happens, the other player will get the ball and start a new volley. In your animation, each player should use body motions to animate the movement neces- sary to hit the ball. You can decide when a player should hit into the net. Every time that you play the animation, it should do the same thing. Your animation should also use at least 2 camera views. There are a few simplifications I encourage you to make, compared to a real game of volley- ball: Players always hit the ball either directly to the other player or into the net. A player does not need to move to get into position to hit the ball back. The ball always follows the same arc as it goes over the net. Here are some snapshots from the animation. 1

Transcript of CS 101 - Problem Solving and Object-Oriented Programming Lab …blerner/cs101/Labs/Lab2/Lab2.pdf ·...

  • CS 101 - Problem Solving and Object-Oriented Programming

    Lab 2 - Methods, Variables and Functions in AliceDue: September 23/24

    Pre-lab PreparationBefore coming to lab, you are expected to have:

    • Read Adams, Chapters 2-3.• Used Alice to do the marching toy soldier problem in the book in Section 2.2.2.• Used Alice to do the wizard and troll example in the book in Section 2.4.1 and 2.4.2.• Used Alice to do the girl and horse example in the book in Section 3.1.1 and 3.4.

    Introduction to the AssignmentHere is the English description of the world you will create and program for parts 1 and 2 of this lab assignment:

    Two people play volleyball. Generally, they volley the ball nicely back and forth directly to each other. Sometimes one player will hit the ball into the net. When that happens, the other player will get the ball and start a new volley.

    In your animation, each player should use body motions to animate the movement neces-sary to hit the ball. You can decide when a player should hit into the net. Every time that you play the animation, it should do the same thing.

    Your animation should also use at least 2 camera views.

    There are a few simplifications I encourage you to make, compared to a real game of volley-ball:

    • Players always hit the ball either directly to the other player or into the net. A player does not need to move to get into position to hit the ball back.

    • The ball always follows the same arc as it goes over the net.

    Here are some snapshots from the animation.

    1

  • I have animated the scientist by having him bend his arms to hit the ball. The rest of his body is sta-tionary.

    I have animated the zombie by having him use his legs to get more underneath the ball. This ani-mation is much more complicated as it in-volves more body parts and motion.

    2

  • This is a view of the 2 players with the ball in midair. Both players stay in their ready position the entire time.

    Here is the ball when the scientist hits it into the net.

    3

  • After hitting the net, the ball falls to the ground.

    A view from the sci-entist’s side.

    4

  • A view from the zombie’s side.

    For this assignment, it is best to approach the problem one step at a time, introducing a new ob-ject and then programming the animations for that object.

    Step 1: Posing the mad scientistThe mad scientist can be found in the People category. Feel free to choose a different person if you like. Your first step should be to pose the mad scientist in such a way that he is prepared to hit the ball. This involves:

    • Raising the scientist’s arms• Turning / rolling the arms so that the hands are close enough together to hit the ball.

    To ensure that the scientist’s arms look ready to hit the ball, you should also add the volleyball to the world. You can find the volleyball in the Sports category.

    Tips on positioning the scientist’s arms. I generally find it easier to change the pose of an object by executing methods on the object rather than trying to use the mouse controls. For ex-ample, to raise the scientist’s left arm do the following:

    • Click on the plus sign next to madScientist in the object tree to see the scientist’s parts.• Select leftArm in the object tree.• Right-click (or ctrl-click on a Mac) on leftArm.• Select “methods” in the menu that pops up.• Select “turn” then “BACKWARD” and then ¼ revolution. Look at the result. You will

    probably find ¼ revolution to be too little. Use the Undo button and try again, turning the arm a bit more.

    Once you find a revolution distance you like, rotate the right arm the same amount.

    Next, you should make sure the hands are close enough together to hit the ball. Use the follow-ing steps to accomplish that. First, we’ll get the mad scientist to face us:

    5

  • • Right click on madScientist in the object tree.• Select “methods”, then “turn to face” and then “camera”.

    Next, we will move the volleyball to the scientist’s hands:

    • Select the volleyball in the object tree.• Right-click on the volleyball.• Select “methods”, then “move to”, then use the cascading menus until you can select one

    of the scientist’s hands. This will center the ball on the scientist’s hand, not really what we want.

    • Click on the arrow in the top right to allow you to drag the volleyball left or right. Drag it until it is centered between the hands.

    • Next experiment with the turn and roll methods on the scientist’s arms until you get the arms in a good position.

    Step 2: Animating the mad scientistAt this point, we have the mad scientist in the pose we want for the start of the animation. This is also the pose that he should be in when he is not actively hitting the ball.

    To work on the animation of the scientist hitting the ball, it is probably best to have a side view of the scientist. So, use the methods as above to turn the scientist left or right ¼ revolution.

    Note that at this point, we are not trying to make the ball move. You should probably just move the ball out of the way for now. All we are concerned about is animating the scientist so he moves his arms in a way that looks “realistic” to hit the ball.

    To do the animation, we need to write a method. First, we should think about how we want the animation to look and then come up with an algorithm for it. Start simply and if you don’t like the result, try something else. Also, I would like each character to have a different animation, so don’t try to get the scientist to do everything.

    First, write down in English the steps you want to take. It’s easiest just to move the arms and leave the rest of the body stationary. Once, you have an idea what you want to do, you need to create a new method:

    • Select madScientist in the object tree• In the details pane (bottom left), click on the methods tab.• Click on the “Create new method” button and give the method a name, like “hitBall”. A

    good method name should be descriptive of what the method does, should have no spaces in it, should use lowercase letters except that the first letter of each word other than the first word should be capitalized.

    • Find the instructions you want to use in the animation (probably just involving the arms) and drag those tiles into the method editor (bottom right). Menus will pop up for argu-ments you need to provide, like the number of revolutions to turn, for example.

    • When you think you have something you like, click on the “world.my first method” tab in the method editor.

    • Click on madScientist in the object tree.• Drag the “hitBall” tile (or whatever you called your new method) into “my first method”.• Click the Play button in the top left corner of the window to see how the animation looks.

    It’s probably not exactly what you want, so go back to the hitBall method in the method editor and try something else.

    6

  • Step 3: Posing and Animating the zombieNext, pose and animate the zombie (choosing a different character if you like). You should try for a different effect, but the basic procedure should be similar.

    • Use the methods menu to adjust the zombie’s body parts to get the initial pose.• Write in English the steps you want to take for this animation.• Create a new method for the zombie to do the animation.• Call this new method from world.my first method.• Keep modifying your zombie’s method until you get an effect that you like and that is sig-

    nificantly different from what the scientist does.

    Step 4: Volleying the ballNow, you are ready to actually volley the ball between the two players. First, you need to up-date your starting world:

    • Add a volleyball net from the Sports category.• Position one player on each side of the net, roughly centered left-to-right and facing each

    other.

    To get the ball to move from the proper location in one player’s hands to the proper location in the other player’s hands, we will use Dummy objects:

    • Position the ball in one of the player’s hands the way you would like it to be when it arrives at that player. Do this using a combination of move methods on the volleyball and per-haps just using the mouse controls a bit, too.

    • Once you have the ball where you want it, create a Dummy object at that location:• On the right side of the world editor, click the “more controls” button.• Select the volleyball• Click the “drop dummy at selected object” button.

    This will add a Dummy Objects folder in the object tree. Open that up and you should see Dummy. Rename that to something more meaningful, like “ballAtScientist”. This will be very convenient because now as the players volley the ball back and forth, one of your move com-mands will simply be to move the ball to “ballAtScientist”.

    Create a similar dummy for the proper position of the ball at the zombie.

    Now you are ready to define a method “hit” on the ball to animate the ball’s motion between the players. Note that this method will only move the ball. The players will not look like they are hitting it. After you get the ball’s motion correct, you will then write another method to put the pieces together.

    The ball should move in a smooth arc over the net from one player’s hands to the other. The first half the ball’s horizontal motion it should be moving up, while it should be moving down for the second half. Rather than using a lot of trial and error to figure out how far to move the ball horizontally, you should use a function to find out how far apart the 2 dummy objects are and move ½ that distance as the ball goes up.

    Create a new variable in the hit method by clicking on the button on the right. This variable will be used to hold the distance between its starting location and its destination. This variable will therefore be a Number and should be given a name like “distance”.

    7

  • Next, we will want to give this variable a value. Drag the variable declaration from the top of the method into the section where the instructions are. A menu will pop up where you should select “set value”. From the next menu, select any value. You are using this value as a placeholder.

    Next, click on Volleyball in the object tree and the functions tab in the details pane. Drag the tile for “distance to” over the placeholder value in your set instruction. In the next menu, select the destination. If the ball is starting in the scientist’s hands, the destination should be the dummy that you placed at the zombie’s hands. You are now assigning a value to the distance variable, where the value is the distance from the ball’s current location to its final destination. You really only want to move it ½ that distance as the ball is going up, so divide this value by 2. To do this, click on the triangle that the follows (volleyball.distanceTo (volleyballAtZombie)). In that menu select “math”, then the division simple and finally the number 2. After doing all this, you should have a statement like this:

    Now, as the ball is going up, it should move toward its destination “distance” amount. When you drag the “move toward” tile into the method editor, for the distance, you should select “expres-sions” and then “distance”.

    As the ball is moving down, it should simply move to its destination. That is, use the “move to” method rather than the “move toward” method. “move to” moves the object so that it is cen-tered on the destination object, in this case, the dummy object at the zombie. In contrast, “move toward” moves the object closer to the destination, but requires an argument indicating how far to move the object toward the destination.

    Put a call to this method into world.my first method to see if the ball moves the way that you expect (again ignoring the fact that it is not yet coordinated with the motion of the players).

    Next, you will want to have the second player hit the ball back. Think about this for a minute and you should realize that the motion will be very similar but in the opposite direction. It would be very convenient if we could use the same method again. As written it won’t quite work be-cause the method refers to a specific destination, the dummy object at the zombie.

    To make this method more general, we should introduce a parameter. In the method editor, click “create new parameter”. Call this parameter destination. We will use this to refer to one of the 2 dummy objects, so the type of this parameter is Object. Now, everywhere in your method that you referred to a specific dummy (like volleyballAtZombie), change this to say “destination”.

    Next, go to world.my first method. You should now see that you need to specify an argument when you call the hit method. The argument needs to be the dummy you want to hit the ball to. So, for the existing call to the hit method, use the dummy at the zombie as the argument. Then add another call to the hit method following this one and pass the dummy at the scientist as the argument this time. Now, when you play your world, the ball should float nicely from the scien-tist to the zombie and then back!

    Now, you are ready to put the pieces together. Create a method in the world called volley. We place this method in world because it manipulates multiple objects: both players and the ball. Move the ball to one of the player’s hands. Then have this method do the first player’s anima-tion, then hit the ball to the other player, the second player’s animation and then hit the ball back. Call this method in world.my first method and remove the instructions that were previ-

    8

  • ously there. Now, when you play the world you should get a nice volley back and forth. If you insert a bunch of calls to volley, you should end up with multiple volleys back and forth!

    There is one more nice finishing touch you can add. When the player animates, the player’s hands probably move away from the ball. A good way to prevent this is to set the ball to have one of the player’s hands as the ball’s vehicle. When the player’s hands move, the ball will move with it. At the end of the player’s animation, you need to set the ball’s vehicle back to the world so that it can move independently.

    To set the ball’s vehicle:

    • Select the ball in the object tree.• Select the properties tab in the details pane• Drag the vehicle tile into the player’s animation method at the beginning and select one of

    the player’s hands.• Drag the vehicle tile into the player’s animation as the last statement and select world as

    the vehicle.

    Do this for both players’ animations and try again. Things should be starting to look pretty good!

    Step 5: Hitting the ball into the netHitting the ball into the net requires you to write another method on the ball. I would again rec-ommend that you create a dummy to represent the location of the ball when it hits the net. Then your animation can just move to or toward that dummy.

    Test this method by calling it from “my first method”. When it works the way you like, modify “my first method” to play the game the way you like mixing up successful volleys with occasional hits into the net. You will also need to move the ball to the winning person’s hands before re-suming the volley.

    Be sure to add comments in your methods to help remember and communicate what your ani-mations are doing.

    Step 6: Changing the camera viewAs one final touch, I would like you to introduce multiple camera angles and to switch angles at points of your choosing during the animation.

    First, drop a dummy at the current camera location and give it a suitable name. Then, move the camera either using methods as for the other objects or camera controls until you find another camera angle you like. Drop a dummy there.

    Next, modify your animation to set the camera’s point of view at some point during the anima-tion. Remember to set the duration to 0 for a better effect.

    9

  • Grading

    5 Step 1: Posing the first player

    5 Step 2: Animating the first player

    10 Step 3: Posing and animating the second player

    10 Step 4: Volleying the ball

    5 Step 5: Hitting the ball into the net

    5 Step 6: Changing the camera view

    5 Putting the animation pieces together

    5 Comments

    Turning in Your WorkGo to ella. Click on COMSC 101 in the toolbar menu across the top. Then click on Assignments in the left column. Click the submit link for this assignment. Click the Add Attachment button. Use the Browse… button to upload a local file. Click Continue. Then click the Submit button.

    10