带有新雪碧套件的球 [英] Ball with the new Sprite kit

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

问题描述

我正在尝试在 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);

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

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