SpriteKit 节点粘在场景边缘/不反弹 [英] SpriteKit Nodes Sticking to Edges of Scene / Not Bouncing

查看:19
本文介绍了SpriteKit 节点粘在场景边缘/不反弹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过这个,我认为必须有一些参数来解决这个问题,但我没有找到它.

I've searched this one and I think there must be some parameter to fix this but I haven't found it.

我在 SpriteKit 中有一个场景,我希望一些圆圈/球在其中弹跳并无限期地保持它们的任何速度.它们应该从场景的边缘反弹.

I have a scene in SpriteKit where I want some circles/balls to bounce around and maintain any velocity they have indefinitely. They should bounce off the edges of the scene.

如果他们移动得足够快,或者以相当尖锐的角度击球,这是有效的,但如果他们走得慢并且靠近边缘平面,他们会继续移动(这很好)但他们坚持到边缘.这种坚持是我不想要的.即使走得很慢,它们也应该会反弹.

This is working if they are moving fast enough, or hit at a fairly sharp angle, but if they are going slower and coming in close to the plane of the edge, they keep moving (which is good) but they "Stick" to the edges. This sticking is what I don't want. They should rebound even if going very slowly.

为了设置边缘,我使用了:

To set up the edges, I used:

SKPhysicsBody *borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody = borderBody;
self.physicsBody.friction = 0.0;
self.physicsBody.restitution = 1.0;
self.physicsBody.linerDamping = 0.0;
self.physicsBody.angularDamping = 0.0;
self.physicsBody.affectedByGravity = NO;

在圆节点上,我有类似的设置,比如:

And on the circle nodes, I have similar settings, like:

ball.usesPresciseCollisionDetection = YES;
ball.dynamic = YES;
ball.restitution = 1.0;
ball.linearDamping = 0.0;
ball.angularDamping = 0.0;
ball.friction = 0.0;

我的场景中的重力为零.我向节点添加了一个脉冲,它们开始弹跳——看起来非常接近,因为东西弹跳,但是如果有任何缓慢移动并以浅角度进入,它们会拥抱"边缘.我将尝试在下面添加一个插图以帮助进行可视化.

I have the gravity in my scene at zero. I add an impulse to the nodes and they start bouncing- It seems very close, as things bounce around, but then if there are any that are moving slowly and come in at a shallow angle, they "hug" the edges. I'll try including an illustration below to help visualize.

http://i.imgur.com/Rpr7luY.png

我尝试了很多 PhysicsBody 设置,但无法停止粘连.

I've tried playing with lots of the PhysicsBody settings, but can't get things to stop sticking.

谢谢!

推荐答案

正如大家所说,这个答案可以直观被视为朝着正确方向迈出的一步,但问题在于整个 SpriteKit 物理引擎.它是不确定的,分数会在计算中丢失,导致模拟不精确.

As the guys mentioned, this answer could intuitively be seen as a step in the right direction, but the problem is with the whole SpriteKit physics engine. It is non-deterministic and fractions get lost in the calculation, causing imprecise simulation.

简短的回答是改用 Box2D.骇人听闻的答案是向相反的方向施加冲动.

The short answer is to use Box2D instead. The hackish answer is to apply an impulse in the opposite direction.

所有详细信息都在我的其他答案中突出显示.

@Jurik 很高兴我能帮上忙 :)

@Jurik Glad I could help :)

这篇关于SpriteKit 节点粘在场景边缘/不反弹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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