Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ball with the new Sprite kit

I'm trying to make a simple forever bouncing ball with new sprite kit on ios 7. I set gravity:

 scene.physicsWorld.gravity=CGVectorMake(0, -9);

And for the ball body I set:

ball.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:17];
ball.physicsBody.mass=1;
ball.physicsBody.restitution=1;
ball.physicsBody.linearDamping=0;
ball.physicsBody.angularDamping=0;

Ball here is just SKSpriteNode. but i have one problem: every time it bounce back its position become a little bit higher and higher. And in a few minutes ball is almost at the top of the screen. Can anyone help with this issue. I just wanna make ball bounce back to the same position every time (for example middle of the screen). Thanks.

like image 792
Alexander Avatar asked Sep 28 '13 18:09

Alexander


People also ask

What can SpriteKit do?

The SpriteKit framework makes it easy to create high-performance, battery-efficient 2D games. With support for custom OpenGL ES shaders and lighting, integration with SceneKit, and advanced new physics effects and animations, you can add force fields, detect collisions, and generate new lighting effects in your games.

What is the difference between SceneKit and SpriteKit?

You can assume SpriteKit is at Top of the SceneKit, As using SceneKit you can add 3D models into Augmented Reality while SpriteKit is used to add extra sprites onto the model. In short SpriteKit is revolution in Gaming.

Is SpriteKit easy?

SpriteKit is easy to learn because it is a well-designed framework and it is even easier if you have experience with Swift. Even for a beginner, if you want to create your first game, 2D games is without a doubt the best way to transit in this new world.


1 Answers

There are probably two issues in play here:

  1. Two bodies are involved in the collision when the ball bounces -- the ball and the edge loop that it bounces off of. For no energy to be lost in the collision, you probably want zero friction and restitution on both bodies.

  2. Even if you do that, though, many physics engines (including SpriteKit's) have trouble with situations like this because of floating point rounding errors. I've found that when I want a body to keep a constant speed after a collision, it's best to force it to -- use a didEndContact: or didSimulatePhysics handler to reset the moving body's velocity so it's going the same speed it was before the collision (but in the opposite direction).

If you just have a ball bouncing up and down, that velocity reset is easy: just negate the vertical component of the velocity vector:

ball.physicsBody.velocity = CGVectorMake(0, -ball.physicsBody.velocity.dy);

If you're bouncing off an angled surface, it's slightly more complicated: normalize the velocity vector (i.e. scale it so its magnitude is 1.0) to get the direction after the collision, then scale that to make it the speed you want. When I'm doing that kind of vector math, I like to convert the vectors to GLKVector2 so I can use the fast math functions from the GLKit framework:

GLKVector2 velocity = GLKVector2Make(ball.physicsBody.velocity.dx, ball.physicsBody.velocity.dy);
GLKVector2 direction = GLKVector2Normalize(velocity);
GLKVector2 newVelocity = GLKVector2MultiplyScalar(direction, newSpeed);
ball.physicsBody.velocity = CGVectorMake(newVelocity.x, newVelocity.y);
like image 80
rickster Avatar answered Sep 26 '22 17:09

rickster