7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 22Length: 2.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Introducing Gravity

Now that we have our basic scene and a few sprites set up, we wanna introduce the concept of movement. Now, as far as this game is going to be concerned, the movement we're going to be interested in has to do with physics. Now, I know that can carry with it a lot of thoughts of difficulties in school and, and all those types of things. But I'm gonna boil this down to the absolute basics so you have a fairly good understanding of what physics is in respect to Sprite Kit. Now, there's two basic concepts that you have to understand, one dealing with the concept of physics within Sprite Kit, and that has to do with the concept of a world and of a body. Now, the world, you can think of as the world that we live in. The world that we live in is governed by certain rules, and one of those rules is gravity, and our Sprite Kit game is going to be no different. Now, within the concept of our game here, the world is going to be represented each time by our scene. So our scene here has this yellow border around it. So if I select that, you can see here that we have a property on the right hand side here called Gravity. Now, Gravity is given the default characteristics that gravity has in the real world. As you can see, it moves 0 in the X direction, so there's no left to right movement as far as gravity is concerned. But there is a movement of negative 9.8 in the Y direction. So if I were to start my sprite right here, I would expect that if I were to just hold that there and let go, it would then fall at a rate of negative 9.8. So that's the basic concept of gravity. But you'll see here that this all seems to be set up in the world, or in the scene, of our Sprite Kit game, but nothing's actually happening. And the reason for that is because we haven't actually told our sprites that they need to be governed by gravity, or by the rules that are inherent within the world that they are present. So now, these sprites, or bodies, can now be governed by certain properties as well. So if I were to select this ball and then scroll down to the bottom of my properties, you're gonna see I have Physics Definition. Now, the one thing you can see right now is this Body Type set to None. So if I were to come in here and select one of these other options, either Bounding rectangle or Bounding circle, and we'll just stick with rectangle for the time being, I now get additional characteristics, or addition properties, that I can set as far as physics are concerned for this particular body inside the world. As you can see here, we have some options. So we have Dynamic, which means that this particular object is going to be governed by the rules that are set upon it by the world. So gravity will have an effect on it. And Allows Rotation means this object can rotate. Pinned, and Affected By Gravity, obviously that's going to mean that this object is going to be affected by gravity as well. And then you have some other properties here, like Friction. So friction, meaning that aspects of the outside world can have an effect on it's velocity and it's direction. Then we have Restitution. Restitution simply means kind of how this thing can bounce. Is it bouncy? So the lower the number, it's less bouncy, and the higher the number, the more bouncy it is. Then we have Linear and Angular Dampening, meaning how is the friction affecting the velocity and the restitution of this particular body inside this world in a linear direction, meaning a straight direction, and an angular direction if it was going in speeds, or in velocity with direction in a particular angle. So for the time being, let's leave all those things to the default, and we'll just go ahead and save this. Now before I run this, note that once I actually run this application, it's gonna happen fairly quickly. So you need to kind of watch very closely. If I were to run my application now, the simulator will start up. And if you saw there, the ball kind of fell right through my paddle and right through the screen of our simulator, so that's not very helpful. So in order to really be able to see the ball move and get some life in this world, then we need to provide a border around our screen, or around our world. So let's go ahead and do that now. So I'll stop this. And for the time being, I'm actually going to remove the paddle, as this is more of a, a, an interesting concept to learn in order to put a border around this so that our ball will actually respond to gravity, but then also play within the, a certain set of bounds within our application. So the way that we're gonna do that is actually through our game scenes. So we're gonna go into GameScene into, then move to view, and we're going to do three simple things. We're going to create a border. So we're gonna say, let border, and this is actually going to be a body. So we're gonna say an SKPhysicsBody. And we're going to use the edgeLoopFromRect option here, and we're simply going to provide it the bounds, or the frame, of our actual screen. So we have a border now, and this border, because this is a physics body, has the same properties that we saw as our ball, so we can set the friction. And we're gonna say that the friction is actually gonna be equal to 0 because we don't want the border to influence the speed or direction necessarily, at least yet, from the, of the ball. And then we're going to simply add this body of, this border body to our physics body of our actual screen. So we'll go ahead and save that. Now if I run my application, we're gonna see that the ball will drop, and it actually stops right here at the bottom. So that's not very exciting. It just kind of stops. But at least we have the, the concept of bounce. Now, if I were to come back in and take a look at my scene, so if I were now to select my ball and I would come down to my Physics Definition, and maybe play around with these values a little bit. So I'm gonna change my Friction to 0, which means there's going to be no friction on this ball, and I'm gonna change my Restitution to 1, which means it's very, very bouncy. And then the Linear Dampening, I'm gonna set both of these to 0, both Linear and Angular. So we're gonna save those. Now if I were to run my application again, you're gonna see the ball drop, but then it's also going to bounce. And because there's no friction and no dampening, this is going to bounce like this forever. So this is a fairly interesting concept in creating our game, as we're going to be utilizing some of these characteristics. But the problem we're gonna have here is that in the game of Breakout, there is no concept of gravity. The ball just kind of moves, but it does play by some of these friction and linear dampening rules. But what we want to do is, we want to be able to play with this in such a way that we don't want gravity to affect it. So how can we do that? Well, this will be the one, the final thing that we're going to play around and touch on a few concepts here in this lesson. So if i were to go into my GameScene.swift file, and so now I have this border here. But what I also wanna do is, I wanna play around with this ball, as well as the physics that are attached to the world around the ball. So as you see here, we have the concept within a scene of a physics body, but we also have a physics world. So that allows us to play around with the physics that are happening within that, within the particular world we're in. So what I can do here is, I can take my gravity, as you can see, this is a vector. So we're gonna set this gravity equal to CGVectorMake, and we're gonna make this have 0 gravity, so there's gonna be no gravity in our world. Then we wanna get ahold of the ball. So I wanna get access to the ball that's in this world. But in order for us to do that from code, we'll need to come into our scene, touch the ball, and give it a name. So we're just gonna call this ball for now, so we'll go ahead and save that. So what I wanna do is, I want to, I wanna use a little helper method called childNodeWithName. And so since I called this ball, I'm going to ask for the child with node name ball. Now, this is an SKSpriteNode, so we're gonna cast that, so now I can play with this ball as a node. And I, what I want to do here is, I want to access it's physicsBody. Now, we're going to get this, and we're going to apply an impulse. So what an impulse is going to allow us to do is to kind of push this ball in a particular direction. So we'll pass in a CGVectorMake, and we'll just give this some direction. So we'll say, 30 in the x, and say, negative 30 in the y. And you can also play around with the properties of this physicsBody, just like we did before. So I can also come in here and I can set the different properties that we saw within the GameScene.sks file. So we'll say, allowsRotation is gonna be equal to false. And to save some time, I'm just going to copy this. We'll say, copy, and we'll paste. And we can also say that the friction is gonna be equal to 0, and, the restitution is going to be equal to 1. And the linear dampening will also be set to 0, and of course, we'll also set the angular dampening equal to 0 as well. So we'll go ahead and save that. So as you can see, we have access to all these same things that we had within the Designer within code. So now I can go ahead and run this, and let's see what happens. So we see the ball moving down towards the corner, and now it's gonna start bouncing off the walls, and so it's gonna bounce off the top. And as you can see here, it's playing by the rules that a, a basic object would have in a world like this where it's going to apply a vector in a certain direction against these walls. And those walls are going to change the vector, or the trajectory, of this particular ball, or physics no, or sprite, within this world. So, I know that's kind of a lot to take in in a, in one lesson, but these are all gonna become very, very important aspects to your Sprite Kit development. And as well as within the rest of the lessons in this course. So now that we have a nice little world here, and we have a body bouncing around the world, it would be nice to be able to reintroduce the concept of that paddle that we had before. So in the next lesson, we're going to bring in the paddle and start to talk about collisions.

Back to the top