This course will be retired on January 24, 2020.
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
In order to have physics first a world must exists and then that world must have bodies inside it. You will begin to understand how simple it is to use Sprite Kit’s built in physics engine and have real world reactions in a few quick steps.
-
0:00
When creating a game, there are some things
-
0:02
that are very close to the real world.
-
0:05
You have gravity, objects colliding with each other,
-
0:08
or maybe just coming in contact with each other.
-
0:11
That's why the best gaming frameworks have a physics engine
-
0:15
to help you mimic the physics of the real world.
-
0:19
Let's take a look at how we can set up our physics world.
-
0:23
The existence of a physics world allows us to start adding physics bodies.
-
0:29
A physics body gives us a mass and shape to our nodes.
-
0:34
This allows them to interact with each other
-
0:37
and be affected by forces such as gravity.
-
0:41
The larger the node, the more mass it will have.
-
0:45
We will be adding physics bodies to both our enemies and projectiles.
-
0:50
The physics body will be invisible to the user,
-
0:53
but we can define it to surround the node.
-
0:57
That way, if one node collides with another, we can be notified.
-
1:02
We also need a physics body to represent the ground so that
-
1:06
we can be notified when an enemy comes in contact with it.
-
1:10
This will mean the player loses a life and the enemy has done his job.
-
1:16
Armed with this knowledge, let's go define our physics world.
-
1:19
[BLANK_AUDIO]
-
1:23
So, now that we understand a bit about physics engines and
-
1:26
gaming frameworks, let's go over to our gameplay scene and nvinitwithsize method.
-
1:33
Right after we call this add space dog
-
1:35
method we'll refer to our self dot physics world.
-
1:41
Now, if I option click on the physics world, you'll
-
1:43
notice that the property belongs to a class called SK
-
1:46
physics world, and it says every scene automatically creates a
-
1:50
physics world object to simulate physics on node in the scene.
-
1:54
And it says you can use this property
-
1:57
to access scenes global physics properties, such as gravity.
-
2:01
So, let's go ahead and set up gravity.
-
2:04
So, we'll say physics world dot gravity, and gravity is a CG
-
2:09
vector, so we can use the function CG vector make to create a vector.
-
2:15
And a vector basically takes in delta x and delta y.
-
2:19
It's the change in x and the change in y.
-
2:23
So, there's not gonna be any change in
-
2:25
x, because gravity for us goes vertically downwards.
-
2:29
We're going to mimic Earth's gravity.
-
2:32
So, Earth's gravity is minus 9.8 meters per second squared.
-
2:37
I understand that Space Cat is on the moon but we are going to use Earth's gravity.
-
2:42
So, let me run our game
-
2:46
and you'll notice that it really didn't change our game.
-
2:48
That's because we need to set up physics
-
2:50
on individual nodes and we haven't done that yet.
-
2:54
So, before I do anything with the space dogs,
-
2:57
what we need is a ground node, so that when
-
3:00
the dogs hit the ground we can detect collision and
-
3:04
we can do all kinds of physics with the ground.
-
3:06
So, let's right click in our project navigator and
-
3:10
we'll select new file, and we'll create a ground node.
-
3:14
[BLANK_AUDIO]
-
3:18
Now, with the ground note, we need to create a method that takes in the size.
-
3:24
So, let's say, instance type, ground with size, and it'll be a CGSize.
-
3:34
Let's copy this method declaration and we'll go over to our implementation.
-
3:41
It says here we'll be at THGroundnode ground equals self.
-
3:48
So, we've already defined spikeit nodes with
-
3:51
images and we've also defined them with colors.
-
3:55
So, for testing purpose, we're going to give it a color.
-
3:59
So, we'll say sprite node with color colon, sk color.
-
4:06
Green color, and then size colon size.
-
4:14
So, we need to give our ground node a name so we'll give it a name.
-
4:22
Next, we need to define a physics body.
-
4:25
We'll say ground dot physics body and that will be S K physics body.
-
4:35
Now, SKPhysicsBody has several different methods, so
-
4:40
let's look at the documentation for SKPhysicsBody.
-
4:43
You can see, you can create
-
4:45
volume-based physics bodies,
-
4:47
so bodyWithCircle, bodyWithRectangle, bodyWithOtherBodies.
-
4:51
And then you can give it different pats too if you wanted to.
-
4:57
So, we'll just define a simple physics body, so we'll say bodyWithRectangleOfSize
-
5:06
and then we just pass it the size.
-
5:10
What we are missing is the position
-
5:12
so we'll say ground.position is equal to CGPointMake.
-
5:19
And this will be size dot width divided by two comma size dot height divided by two.
-
5:31
And now we simply return ground.
-
5:34
So, let's run our game.
-
5:38
Oops, I am missing an asterisk there.
-
5:40
All right.
-
5:40
So, now, let's go over back our game play scene
-
5:43
and we need to add our ground node to our scene.
-
5:46
So, I'll do import TH ground node
-
5:52
and then back in our init method, I'll declare a new ground node,
-
5:58
TH ground node, ground with size.
-
6:02
We'll say CGSizeMake and for the width, we'll give it the width of the frame,
-
6:08
so we say self.frame.width and for the height, we'll give it 22.
-
6:15
Then, we'll do self.addChildground.
-
6:18
So, let's run our game,
-
6:24
and as you noticed that the ground
-
6:26
appeared and then it quickly just disappeared.
-
6:29
So, let's stop our game and I'm going to change the physics world gravity, instead
-
6:34
of negative 9.8, I'll change it to a
-
6:36
positive and then, let's run our game again.
-
6:41
And you see that the ground actually floats upwards because
-
6:45
now we're declaring the physics to be a positive delta y.
-
6:51
We're gonna make this -9.8.
-
6:53
So, as you can see how physics has an
-
6:55
effect on knowns once the physics body has been defined.
-
6:59
As for the ground let's go over back to the implementation.
-
7:03
What we can do with the ground
-
7:04
is we can say ground.physicsBody.affectedByGravity
-
7:13
equals NO.
-
7:15
So, this way it's going to prevent the ground from
-
7:18
being affected by gravity, and prevent it from moving about.
-
7:23
So, now you can see that the ground stays in position.
You need to sign up for Treehouse in order to download course files.
Sign up