Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that we have set up our physics world we can add movement to our enemies by using physics. In this video, we will define our physics bodies for the Space dog node class.
-
0:00
Before we add physics to our Space Dog, let's go
-
0:04
ahead and make our physics configuration a bit more modular.
-
0:08
Which means, that I just want to put all this physics set up in its own method.
-
0:13
We'll say, setupPhysicsBody.
-
0:19
And let's copy all the lines related to the physics set up in it's own method.
-
0:26
So
-
0:30
instead of ground, it will be self, because this is
-
0:34
an instance method, which is indicated by this minus sign here.
-
0:40
And then the body of rectangle with size.
-
0:43
This would simply be self.frame.size.
-
0:49
And now here, we would simply call ground setupPhysicsBody.
-
0:54
[BLANK_AUDIO]
-
0:58
So, similarly for spaceDog node, we'll go ahead and create a method.
-
1:06
We'll say setupPhysicsBody, and then physicsBody.,
-
1:14
is equal to SKPhysicsBody bodyWithRectangleOfSize
-
1:21
and then we'll give it self.frame.size.
-
1:28
So, let's try this for now, going to the previous method,
-
1:31
which is spaceDogOfType, in that we will call our physicsBody setup method.
-
1:38
So, we'll say spaceDog setupPhysicsBody.
-
1:42
All right, so, let's run our game.
-
1:50
And if you'll notice that the two dogs fell towards the moon and
-
1:53
they took the ground with them, that's crazy, the ground shouldn't have moved.
-
1:59
So, what we are missing with the ground
-
2:01
is, let's go over to the ground implementation.
-
2:04
There's a property called dynamic which means that
-
2:10
the ground should not be affected by the physics.
-
2:15
If I option click on dynamic, I'll say that a Boolean value
-
2:19
that indicates whether the physics body is moved by the physics simulation.
-
2:24
We do not want it to be moved.
-
2:26
So that's why we have to set it to no.
-
2:28
It also goes on to say, if the value is known,
-
2:31
then the physics body ignores all forces and impulses applied to it.
-
2:35
And that's exactly what we want.
-
2:38
So, let's run our game once again.
-
2:40
And now you'll notice that both the Space Dogs, they just fall to the ground.
-
2:46
So, that's all well and good.
-
2:48
But, the Space Dogs have space suits, which
-
2:51
means that they're moving at a constant speed.
-
2:53
They're not just hurtling towards the ground.
-
2:56
So, let's go over to our spaceDog implementation and in the
-
3:00
setupPhysicsBody, we'll also set our affectedByGravity property to no.
-
3:06
We'll say like, well, how will they move towards the ground?
-
3:09
Well, what we're going to do is, we're going to define a velocity.
-
3:13
Because after all, the space dogs in their space suits are
-
3:16
moving at a certain velocity towards the surface of the moon.
-
3:20
At least, that's what we want the user to get the feel of.
-
3:25
So, what we'll do is we'll say, self.physicsBody.velocity is equal to.
-
3:32
And remember, CG vector from our gravity?
-
3:35
Well, once again we can define the delta x and delta y.
-
3:39
The delta x will be zero, and the delta y has to be a little faster.
-
3:45
So I'm going to set it at minus 50.
-
3:47
And now let's run our game.
-
3:52
And you'll see that it's moving at a pretty decent pace.
-
3:56
And we can shoot it too.
-
4:00
So, now that we've added physics to our ground, to
-
4:02
our enemies, the only thing that's missing is our projectile.
-
4:06
So, let's go ahead and create a method inside our projectile node.
-
4:12
We'll say, setupPhysicsBody
-
4:16
self.physicsBody is equal to SKPhysicsBody,
-
4:23
bodyWithRectangleOfSize, self.frame.size.
-
4:30
You should be able to recite this in your sleep now.
-
4:33
Self.physicsBody.affectedByGravity is equal to NO.
-
4:39
We just wanna give our projectile a physics body.
-
4:42
We don't want it to be affected by gravity.
-
4:44
And once again, in our projectile position method, we'll call the setupPhysicsBody.
-
4:53
All right, let's run our game.
-
4:59
So, now you'll notice if you keep consistently hitting our
-
5:02
enemy with our projectile, it has some weird effects to it.
-
5:06
It actually moves it and this is due to collision in our physics engine.
-
5:11
When two nodes collide, it impacts the velocity of the two nodes.
-
5:17
So our projectile's velocity is not effected
-
5:19
as much because it's small, but our enemies,
-
5:24
our space dogs, are effected a bit more because their, their mass is much larger.
-
5:30
So, in the next video, we'll find out what collisions are and
-
5:33
what contacts are and how we can define collisions and contact masks.
You need to sign up for Treehouse in order to download course files.
Sign up