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…
1 |
self.physicsWorld.gravity = CGPointMake( 0, -1 ); |
Then did a little switcheroo at the last minute and understandably decided to use this instead…
1 |
self.physicsWorld.gravity = CGVectorMake( 0, -1 ); |
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…
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…
1 2 |
CGPoint grav = CGPointFromString( [levelInfo objectForKey:@"Gravity"] ); self.physicsWorld.gravity = CGVectorMake( grav.x, grav.y); |
Well I hope that helps someone out there!
0 Comments
Leave a reply
You must be logged in to post a comment.