0:18

The complexity of the game at first, when you saw the introduction,

Â might look a little daunting, but it's not as hard as you think.

Â We're going to provide you with lots of code that's going to help you

Â in building your implementation.

Â For week 7, the critical task that you have to do is you have to actually

Â implement the control scheme for the spaceship in Asteroids.

Â So in this lecture, I'm going to go through and play Asteroids a little bit,

Â explain how the control scheme works.

Â Then I'm going to spend most of the lecture in one note kind of going over

Â some of the basic math used in building the control scheme.

Â So let's go to it.

Â 0:51

So I'm going to give you a demonstration of the spaceship for

Â our version of Asteroids.

Â I'll just pop it up here so you can kind of see it running while I talk.

Â So here we have the spaceship sitting on our nice background and

Â I'll move it around in a second.

Â Unfortunately, I can't give you this URL because that kind of defeats the purpose

Â of the whole project, you'd automatically have the code.

Â But I can recommend if you want to get a feel for

Â the physics of how spaceship should fly, just simply Google Asteroids and

Â arcade and you'll see lots of links that are kind of demo versions of Asteroids.

Â Try it out.

Â I think you'll get a good feel for how the game should play.

Â Let me just demonstrate it real quick here on screen.

Â So the controls for your spaceship have essentially a left arrow key rotates

Â you counterclockwise, right arrow key rotates you clockwise.

Â And then the main thing is you can hit the up arrow key and thrust forward.

Â [SOUND] And so as you see,

Â you move in a straight long line as long as you're pressing forward.

Â Now, one thing you can do is,

Â while you're thrusting forward you can change your direction.

Â And you notice that you'll always accelerate in the direction

Â of your forward vector, the forward direction for the ship, so

Â that your velocity vector is independent of the direction you're accelerating in.

Â So in fact, the control scheme for the ship, you're not actually controlling

Â the velocity, now, you're actually controlling the acceleration.

Â So I might need to walk you through a little bit of math to setup

Â a second update that will update the velocity in terms of acceleration.

Â 2:21

Another thing to notice here is that as we accelerate, it should start to move and

Â eventually its velocity kind of caps out and the ship slowly slows down and

Â comes to a halt.

Â Well in space, there's not supposed to be friction but you can just imagine we

Â have a lot of debris here and the debris is slowing you down.

Â So in fact in our version of our game, we're going to actually have friction.

Â We're going to model the ship slowing down, this has two nice effects.

Â First is, it allows you to stop easier,

Â the second thing is it capture maximum velocity.

Â So, I'll show you how to add friction to your model.

Â So, with that, let's go off and

Â then work on the mathematics of how to model acceleration and friction.

Â 3:06

Okay, now that we see the demo of the spaceship,

Â let's talk about how to build some code that allows you to

Â control the spaceship in the manner we just demonstrated.

Â Now, I know doing this kind of of math I'm going to talk about

Â here kind of makes some of you anxious so my suggestion is just bear with me.

Â I'll go into a fair bit of detail.

Â We'll get some Python code and I think you'll find that this is very tractable.

Â 3:29

So let's start off with kind of the easiest thing which is actually how to

Â control the orientation of your spaceship.

Â So there's actually two variables in the class definition for the spaceship.

Â There are two fields.

Â Here is self.angle.

Â This controls the orientation of the spaceship.

Â And then there's self.angular velocity, which is going to control how fast your

Â spaceship will actually rotate whenever you hit the left and right arrow keys.

Â So self.angle's probably the most important thing to understand, and

Â it's the angle between the horizontal axis and the direction the ship is pointing.

Â So for example, here's the horizontal axis.

Â Let me position our ship right here, and the ship has a forward vector

Â that's kind of pointing in the direction the ship is facing.

Â Call that the forward vector.

Â 4:33

Okay, so if we want to go through and draw the ship, there's an option inside

Â draw_image to actually specify an orientation for the image of your ship.

Â I'm drawing it right down here at the bottom.

Â We can provide a final perimeter which is a rotation and

Â this will actually tell us kind of how to orient our image of the spaceship so

Â it looks like it's pointing in a particular direction.

Â So the other thing we have to do is we have to control how the ship rotates using

Â the left and right arrow keys.

Â And to do that you're

Â going to do something very similar to what you did in Pong.

Â We're going to have a velocity that we're going to control inside the key handler.

Â So the key handler's going to actually control self.angle_vel.

Â And then inside your update method, you're going to go through and

Â update the angle by the angular velocity.

Â That gives us kind of a quick overview about how we'll control the orientation of

Â your spaceship All right, let's move on to understanding how the position,

Â the velocity and the acceleration of a spaceship relate to each other.

Â In particular, our goal is going to be to derive some code in Python that'll allow

Â us to control and manipulate these three quantities.

Â So the first thing to remember is that if we have our spaceship here.

Â 6:02

The spaceship has a velocity, and

Â one of the critical things we saw was that the velocity, the kind of

Â direction that the ship is moving doesn't have to be in the direction it's facing.

Â So the velocity can be something like this, maybe pointing off to the side.

Â And then finally, we had the ability to make the ship accelerate, so

Â we saw a little thrust triangle that got drawn down here,

Â whenever our ship was accelerating.

Â And it always kind of in this case,

Â accelerated in a direction corresponding to the way the ship was facing.

Â So we can model those as kind of three quantities over here.

Â Each of these quantities is two dimensional.

Â And so if you recall the relationship between position and

Â velocity was really straightforward.

Â In Pong, we did an update which looks something like this,

Â new position was equal to the old position plus the velocity vector.

Â Now notice I've done kind of updates for

Â both the zero in the first coordinates here at the same time.

Â So this is not Python code yet.

Â If we want to update the velocity in terms of acceleration,

Â turns out you can do the same thing.

Â We can make the new velocity equal to the old velocity plus acceleration.

Â So again, we have an equation.

Â It looks something like this down here.

Â Again, this is not Python because I'm kind of doing both components at once.

Â But, let's move on and now I'll actually show you how to do this in Python.

Â All right, let's build some Python code now to control the position,

Â the velocity and the acceleration of our spaceship.

Â So if you take a look at the ship class that we provided for you,

Â there are four fields that are involved to what we're going to do.

Â We have a position for the ship.

Â We have a velocity for the ship.

Â We have an angle for the ship, we just talked about that a few minutes ago.

Â And then finally we have this flag called thrust that essentially keeps track of

Â whether the ship is thrusting.

Â If we're holding down the up arrow key, the ship is thrusting.

Â If the arrow key is up, we're not holding it down, then it's false.

Â 9:05

And if you remember this angle right here, self.angle

Â was the angle that the ship made with the horizontal axis.

Â So if you haven't ever done trig, you just have to take on faith what I'm going to

Â say here but if we have self.angle, we can compute the length of this

Â piece of the vector, kind of the horizontal component.

Â It's the cosine of self.angle.

Â And likewise, this vertical component here is

Â 9:54

So we get math.cos.self.angle, and math.sin.self.angle.

Â So, that's the acceleration vector.

Â So, we're going to make the ship always accelerate in the direction it's

Â facing, okay?

Â So, what's the last thing we need to do?

Â Well, last thing we need to do is we need to go through an update the velocity

Â vector by the acceleration vector.

Â In this case of the spaceship, that's the forward vector.

Â So, we want to do something like this.

Â So, remember this was kind of our update.

Â The new velocity is the old velocity plus an acceleration.

Â Now there are two things to remember here.

Â Notice that I've conditioned this by self.thrust.

Â So if we're holding down the up arrow key,

Â we update the velocity according to this relation.

Â If the up arrow key is not pressed then this conditional is false and

Â we just leave things alone.

Â So your key handler is going to have to go through and

Â actually set self.thrust appropriately.

Â The second thing to note here is I've kind of been a little quick here.

Â 10:49

This is always a unit velocity vector here.

Â This has length 1 to get the acceleration just right,

Â you may need to go through here and multiply the forward vector by a constant.

Â So you need to multiply by a constant.

Â So, you'll need to do a little bit of experimentation here to

Â get the thrust just right for your spaceship.

Â Okay, let's finish things off now, I'm going to talk about how to do friction.

Â 11:21

Okay, let's finish off our lecture and

Â talk about how to add friction to our model of the motion of the spaceship.

Â So the first thing you might ask is why do we bother putting friction in, I mean,

Â it's a vacuum in space, there shouldn't be any friction.

Â Well, there are a couple of reasons we're going to add in friction.

Â One is it makes controlling the spaceship a little easier.

Â If you take your finger off the up arrow key,

Â the ship stops thrusting and eventually friction will bring the ship to a stop.

Â Second thing is, if you don't have friction, you can make this ship leave as

Â fast as you want by just continuing holding down the up arrow key.

Â If there's friction, it actually caps the velocity of the spaceship.

Â If you have a physical thought, you can think about there's a little bit of debris

Â out there, and the debris is kind of slowing down the ship.

Â 12:05

So, how can we model friction in a way that fits in what we've done before?

Â Well, the critical thing to observe here is that

Â the friction basically induces a deceleration on this spaceship.

Â And that deceleration is kind of in the opposite direction

Â to the velocity of the spaceship.

Â So here we have the velocity, and

Â we multiply it by a small constant, negative of that.

Â What that does, it takes the velocity back, scales it down a lot,

Â points in the opposite direction, and that's going to be an acceleration.

Â 12:35

It's going to correspond to kind of how much the ship is going to decelerate on

Â a particular step.

Â So, now in our model, we're going to have two sources of acceleration for our ship.

Â There's going to be thrust in the four direction and there's going to be friction

Â in kind of the opposite of the direction of the velocity vector.

Â So, if you go through and you do a little, do a little simple algebra here.

Â You actually come up with a new update which says,

Â the way we get the new velocity is we take a multiple of the old velocity but

Â just a little bit less than one.

Â So, we kind of scale the velocity down every time we do an update.

Â And then if the ship is thrusting while we add in kind of

Â an acceleration vector in the forward direction for the ship.

Â 13:15

So the change to our code is really simple, if you go over here,

Â here's some Python code and what we see is that we have the old update we had

Â before which is the new position is equal to the old position plus the velocity.

Â Down here, we still have the corresponding acceleration and

Â the forward direction if we're holding down the thrust key, the up arrow.

Â And it'll all be done, here we kind of tucked in an update for

Â the velocity vector that says take the whole velocity vector,

Â scale by something that just a little less than one.

Â That's what going to star equals 1- c does.

Â This is going to be a number just less than one.

Â We kind of scale it by that number.

Â That's going to update the velocity.

Â So what happen is we provide no acceleration.

Â What's going to happen is friction is going to eventually make the velocity

Â vector go to zero.

Â So these three components here actually form our entire model for

Â how the spaceship should behave.

Â 14:16

Take a look at the code, try to understand it, don't just cut and paste it in there.

Â I think that you'll find the math is not as complicated as you might think.

Â The only thing we're really using is kind of trig functions, for getting the forward

Â vector and you can just kind of take us and our word on those.

Â So play around with this, I think this will get you kind of a good start toward

Â getting a good model for the motion of your spaceship.

Â Okay, I'll see you back in a couple of lectures.

Â