Ball with the new Sprite kit

前端 未结 2 995
深忆病人
深忆病人 2020-12-09 07:13

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);
相关标签:
2条回答
  • 2020-12-09 07:19

    it's strange but if i set:

     ball.physicsBody.linearDamping=0.02f;
    

    It seems that ball don't change its bounce height.

    0 讨论(0)
  • 2020-12-09 07:43

    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);
    
    0 讨论(0)
提交回复
热议问题