SpriteKit节点坚持场景/不弹跳的边缘

时间:2023-01-22 21:25:39

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

我搜索了这个,我认为必须有一些参数来解决这个问题,但我还没有找到它。

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.

我在SpriteKit中有一个场景,我想要一些圆/球反弹并保持它们无限期的任何速度。它们应该从场景的边缘反弹。

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

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

我已经尝试过使用大量的PhysicsBody设置,但无法让事情停止坚持。

Thanks!

2 个解决方案

#1


3  

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.

正如这些家伙所提到的,这个答案可以直观地看作是朝着正确方向迈出的一步,但问题在于整个SpriteKit物理引擎。它是非确定性的,并且在计算中分数会丢失,从而导致模拟不精确。

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

简短的回答是使用Box2D。黑客的回答是在相反的方向上施加冲动。

All details are highlighted in my other answer.

所有细节都在我的其他答案中突出显示。

@Jurik Glad I could help :)

@Jurik很高兴我可以帮忙:)

#2


0  

Wrong initializer. There are 2 kinds of SKPhysicsBody - Volume-based and Edge-based. You are using the exactly wrong kind, which does not participant movement.

错误的初始化程序。有两种SKPhysicsBody - 基于卷和基于边缘。您使用的是完全错误的类型,而不是参与者的移动。

Please check out SKPhysicsBody Documentation.

请查看SKPhysicsBody文档。

#1


3  

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.

正如这些家伙所提到的,这个答案可以直观地看作是朝着正确方向迈出的一步,但问题在于整个SpriteKit物理引擎。它是非确定性的,并且在计算中分数会丢失,从而导致模拟不精确。

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

简短的回答是使用Box2D。黑客的回答是在相反的方向上施加冲动。

All details are highlighted in my other answer.

所有细节都在我的其他答案中突出显示。

@Jurik Glad I could help :)

@Jurik很高兴我可以帮忙:)

#2


0  

Wrong initializer. There are 2 kinds of SKPhysicsBody - Volume-based and Edge-based. You are using the exactly wrong kind, which does not participant movement.

错误的初始化程序。有两种SKPhysicsBody - 基于卷和基于边缘。您使用的是完全错误的类型,而不是参与者的移动。

Please check out SKPhysicsBody Documentation.

请查看SKPhysicsBody文档。