Lesson 8

Realistic Tire Movement

Pre-Requisites:Lesson 7

Topics:

1.  Do-together

2.  Object sub-part movements

3.  Events

4.  Duration

Introduction:

In the previous lesson, we updated our world to use the keys W, A, S, D to control the car. This allowed us to more realistically control the car’s movement. Today we will add even more realism to the world by making the front tires of the car turn when the car turns. What else should move when the car turns? The steering wheel should but we can’t see that so we will only worry about making the tires turn.

Tire Movement:

Sequential vs. parallel: When the tires turn should the tires turn at the same time or should one turn then the other? By default Alice interprets our instructions sequentially or in the order we specify them. We can explicitly tell Alice to behave this way using a “do-in-order” block. If we want instructions to run at the same time we place them in a “do-together” block. Which block should we use to make the tires turn?

Fill in methods: Click on “convertibleCorvette” in the object tree then click on the “methods” tab. Find “tiresLeft” and click the “edit” button next to it. Drag a “do-together” block into the method “tiresLeft” replacing the “do nothing” statement. Now we want to make the tires turn. How can we do that? Click on the little plus sign next to “convertibleCorvette” in the object tree. You should see a list of sub-parts that belong to the convertibleCorvette. Click on “leftFrontWheel” then make sure the “methods” tab is showing. Drag “leftFrontWheel turn” into the “do-together” we added a minute ago. Pick “left” in the menu because this is a left turn, then select “1/4” revolution. Now click on “rightFrontWheel” and make it turn just like you did the “leftFrontWheel”. That finishes the movement for the left turn.

Now repeat the previous instructions but for “tiresRight”. Hint: Which way should the tires turn?

Call movement methods from events: We now have our methods written. What will happen if we run our world now? Nothing yet, we didn’t call the methods. We want the tires to turn when the car turns, where should we call the methods from? Hint: What makes the car turn? If you look at the events for the A and D keys you will see there are three sections in each: “begin”, “during” and “end”. What do these mean? Beginning/During/Ending what? Beginning means when the key is first pushed run this command once. During means while the key is held down repeat this command as many times as possible. End means when the key is released do this command once. Where should we call “tiresLeft”? Click on “convertibleCorvette” in the object tree then make sure the “methods” tab is selected. Drag “tiresLeft” in the “Begin” action for the “A” event. Drag “tiresRight” into the “Begin” action for the “D” event.

Now we have the tires turning when we first press a key. Let’s try it out. Press play and see what happens.

Did you notice that our tires don’t return to straight after a turn? How can we fix that? To fix it we need to put actions in the “End” parts of the events for “A” and “D”. What method can we put in the end action for the “A” key? Hint: What is the opposite of the begin action for that key?

Click on “convertibleCorvette” in the object tree then make sure the “methods” tab is selected. Drag “tiresRight” into the “End” action for the “A” event. Drag “tiresLeft” into the “End” action for the “D” event.

Making a smooth turn: Press play and try driving the car around. The tires turn just was we wanted them too but the car doesn’t. There is a long pause between our tires turning and our car moving. That is because the car doesn’t start to turn until the tires have completely finished turning. To make the tires turn faster we need to adjust the duration.

Go into the “tiresLeft” method. It should be a tab in the center portion of your screen. For each tire click on the “More” button then move your mouse to duration. Select “Other” and type “0.1” then press “Okay”. Now go into the “tiresRight” method and set the duration for the tire movements to “0.1” as well. Why did I pick 0.1? How would it be different than 0 or 0.2? 0 would make the tires teleport to a new position, which might look weird. 0.2 would be just a bit slower but it would work. I prefer 0.1.

Press play and run the world again. The car turns and the tires do too. Next we may want to tweak the tires because it looks like they turn too far.

Option: Depending upon time we can learn how to use variables to fix the tires or if we do not have time we can change the tires to 0.125 revolution manually.