球用新的Sprite套件 [英] Ball with the new Sprite kit

查看:134
本文介绍了球用新的Sprite套件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ios 7上的新精灵套件制作一个简单永远的弹跳球。
我设置重力:

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);

对于我设置的球体:

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

这里的球只是SKSpriteNode。
但我有一个问题:每次它反弹它的位置变得有点高一点。几分钟后,球几乎就在屏幕的顶部。
任何人都可以帮助解决这个问题。我只是想让球每次都弹回到相同的位置(例如屏幕中间)。
谢谢。

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.

推荐答案

这里可能有两个问题:


  1. 当球反弹时,两个物体参与碰撞 - 球和它反弹的边缘环。因为在碰撞中没有能量损失,你可能想要在两个物体上都没有摩擦和恢复原状。

  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.

即使你这样做,很多物理引擎(包括SpriteKit)由于浮点舍入而遇到类似情况的问题错误。我发现当我想要一个身体在碰撞后保持恒定速度时,最好强制它 - 使用 didEndContact: didSimulatePhysics 处理程序重置移动体的速度,使其速度与碰撞前的速度相同(但方向相反)。

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);

如果你从一个倾斜的表面反弹,它会稍微复杂一些:将速度矢量标准化(即缩放它,使其大小为1.0)以获得碰撞后的方向,然后对其进行缩放以使其达到您想要的速度。当我在做那种矢量数学时,我喜欢将矢量转换为 GLKVector2 所以我可以使用GLKit框架中的快速数学函数:

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);

这篇关于球用新的Sprite套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆