2
votes

I'm working for a small bouncing ball game using the SpriteKit physics engine. My problem is:

When I apply a huge impulse on the bouncing ball to have it fall on ground fast, sometimes it may pass through the ground (very thin, height=2).

I find this in the Apple document, but it doesn't work.

Specify High Precision Collisions for Small or Fast-Moving Objects When Sprite Kit performs collision detection, it first determines the locations of all of the physics bodies in the scene. Then it determines whether collisions or contacts occurred. This computational method is fast, but can sometimes result in missed collisions. A small body might move so fast that it completely passes through another physics body without ever having a frame of animation where the two touch each other.

If you have physics bodies that must collide, you can hint to Sprite Kit to use a more precise collision model to check for interactions. This model is more expensive, so it should be used sparingly. When either body uses precise collisions, multiple movement positions are contacted and tested to ensure that all contacts are detected.

ship.physicsBody.usesPreciseCollisionDetection = YES;

1
I found a same question: stackoverflow.com/questions/24838465/…noodles
It's 2018 and still there are issues with fast-moving physics bodies passing through each other. As @sangony points out in his answer, you can slow down your nodes to prevent this. But in cases where design requires fast-moving bodies, it seems there is still no guaranteed solution. I recommend submitting a bug report to Apple about this issue. I just submitted one and attached a demo project where this issue was manifesting consistently. All we can do is speak up. Apple can choose to improve their engine or not.peacetype

1 Answers

3
votes

You can set your sprite's node.physicsBody.usesPreciseCollisionDetection = YES; but this will not guarantee a collision flag all the time as your sprite's velocity could simply be just too high.

You should apply a speed limit to your nodes as to prevent them from going too fast. Something like this:

if(node.physicsBody.velocity.dx > 200)
    node.physicsBody.velocity = CGVectorMake(200, node.physicsBody.velocity.dy);

The above code example will limit the right movement, dx, of your node to 200 while keeping the dy (up/down) velocity as is.