0
votes

I'm building a relatively simple game in SpriteKit where you can bounce a ball based on a user's touch location, using something like ball.physicsBody.applyImpulse(someVector).
I've been trying to understand how to gravity and vectors work by playing with the values of the physicsWorld.gravity vector and the vector of impulse I apply on the ball and I think I need some clarification;

If I use this code:

self.physicsWorld.gravity = CGVectorMake(0, -10)
ball.physicsBody.applyImpulse(CGVectorMake(0, 10))

Shouldn't the ball, based on this code and according to common sense not move at all, as the pulling and pushing forces are equal, or at least go up the exact distance of 10 before starting to fall in this case?
In reality what happens is that the ball does bounce up and is going a distance of over 10 pixels up.
Why is it behaving like that, and what exact value do I need to use for the vectors to cancel out so that the ball remains completely still?
I'd be happy for any sort of explanation, Thanks in advance.

1
The question is totally confusing two unrelated things. "applyImpulse" simply applies impulse for one frame of the game. (So, for about 1/100th of a second!) The physics world setting, ".gravity" is totally unrelated, it creates a field which operates at all times. - Fattie
"In reality what happens is that the ball does bounce up and is going .. a long distance" Very simply that depends on the mass of the ball. You're simply giving it a kick for 1/100th of a second (a short hard kick). The height simply depends on the mass. - Fattie

1 Answers

0
votes

That 10 is not pixels, for gravity, it is meters per second, so you need to take the 10 and divide by FPS to get 1 frame, (1/6 meter) and then you need to figure out what meter to pixel ratio is, and that will get you the number of pixels. For impulse, it is Newton seconds, which is pushing 1 kilogram 1 meter per second. So you need to figure out the mass of the item to figure out how many meters it is really moving per second. So if your objects mass was 1 KG, then it would balance to 0. I am not going to do the actual math to tell you how to calculate this stuff, you will have to research on your own.

In Summary:

Gravity unit is meters per second Impulse unit is newtown second AKA kilogram meter per second

You need to match units in order for things to balance out to 0

See https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/#//apple_ref/occ/instm/SKPhysicsBody/applyImpulse: and https://developer.apple.com/library/mac/documentation/SpriteKit/Reference/SKPhysicsWorld_Ref/index.html#//apple_ref/occ/instp/SKPhysicsWorld/gravity if you want to learn more.