Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

Sprite Kit Gravity 101

Sprite Kit Gravity 101
May 18, 2014 Justin

For those of us that didn’t really pay attention in high school physics, the term “vector” might sound fancy. It was for me at least. Try mentally replacing “vector” with this term instead: “direction”.  Ahhh, much easier on the ol’ noggin right.

Sprite Kit originally set vectors like so…

Then did a little switcheroo at the last minute and understandably decided to use this instead…

 

Notice the subtle difference? Vector instead of Point.

So what’s up with the 0, -1 ? Well those numbers could be anything, but in this example, the 0 means there is 0 pull on the x axis, and  the -1 means there is a pull of -1 on the y axis (or a downward pull of 1 degree). Exactly how much -1 affects the objects in your Sprite Kit scene depends on a whole slew of factors: their size, mass, density, whether they are affected by gravity at all, etc.  In keeping this article simple, I’ll just stick to talking about gravity though.

So if -1 isn’t enough of a pull downward for your tastes, then try -3, or -5, or -10, and so on.

Here’s a little vector / directional diagram I did for the iBook documentation of my latest Starter kit that should help…

Vectors in Sprite Kit Explained

 

The arrows point in the general direction those hypothetical values would pull objects. You might notice I’m using brackets instead of parentheses in the diagram, which is because my starter kit uses a property list to define things like gravity, and in defining CGPoints in a property list, the format is always { x, y } where the x and y are comma separated within opening and closing brackets.

It’s worth noting that Sprite Kit doesn’t (yet) have a CGVectorFromString method, but programmers can still pull vector values from a property list with CGPointFromString. For example, you might do something like this…

 

Well I hope that helps someone out there!

 

Justin Dike is the owner and lead developer at CartoonSmart.com. He's taught hundreds of hours of game development video tutorials, covering everything from coding to art. These days he's working mostly on Swift and Sprite Kit tutorials, which often lead into highly polished iOS / tvOS Starter Kits, which require no programming at all, but are capable of making nearly any type game! Yeah, you read that write. You can also find Justin at the official CartoonSmart Podcast

0 Comments

Leave a reply

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com