user6361496
user6361496

Reputation:

SpriteKit & Swift Ball Speed

I have made a game using SpriteKit and Swift 3 and have figured out all aspects of the game except the speed of the ball node in the game. Im confused with the different function applyImpulse() and ball.physicsBody.velocity, as I have tested both and don't seem to really understand what the speed I'm actually programatically settings is. Any clarification on what I should be using would be great?

Also whilst testing (by printing the ball's velocity to the console every collision) I would see sometimes the ball's speed would simply go to some long and random decimal value when it hit other nodes such as a paddle which I hadn't specifically coded anything to happen with the ball's speed in the case of a collision with it.

In summary I would appreciate:

  1. Just general clarification regarding speed of the ball in SpriteKit and how I should approach it (what method/function I should use)

  2. How I would make it so the ball's speed doesn't got to these very long random decimals

Thanks

Upvotes: 1

Views: 1308

Answers (1)

crashoverride777
crashoverride777

Reputation: 10664

In regards to the values, there is not really a set rule of what the values are for impulses and forces. It depends on how big your sprites physics body are etc. An impulse of 80 might be a perfect jump value for 1 sprite size, but than make it half the size and that 80 is suddenly way to high. There are also factors such as gravity, mass etc than can have an effect on this. So you usually just play around with the values until you get the desired result.

In regards to the collision with the paddle, you need to check your bit mask values and your dynamic properties. SpriteKit by default sets collisions to all objects, so if you dont specifically tell your paddle/ball to ignore each other they will collide. There are also things such as restitution, friction, damping etc that can have an effect on how you sprites behave when colliding. There are loads of tutorials on google about SpritKit physic/collisions or read the apple documentation.

In regards to the difference between velocity and impulses/forces, as per apples documentation

"First, you can control a physics body’s velocity directly, by setting its velocity and angularVelocity properties. As with many other properties, you often set these properties once when the physics body is first created and then let the physics simulation adjust them as necessary. For example, assume for a moment you are making a space-based game where a rocket ship can fire missiles. When the ship fires a missile, the missile should have a starting velocity of the ship plus an additional vector in the direction of the launch.

When a body is in the simulation, it is more common for the velocity to be adjusted based on forces applied to the body. Another source of velocity changes, collisions, is discussed later."

https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Physics/Physics.html

So basically the general rule of thumb is this:

1) Only set the velocity property when you create the physics body. I never needed to do this for my games yet. The only time I really use the velocity property is for things such as double jumping where I need to set it to 0 to have a consistent double jump

 ...velocity.dy = 0
 ...applyImpulse( 

2) When you are playing the game already than

a) If you are trying to continuously move your ball you should use

applyForce...

in something like the update method of your SKScene.

b) If you want to make your ball jump, so basically a short 1 time thing, you should use

applyImpulse...

Hope this helps

Upvotes: 1

Related Questions