Question

I'm wondering why the following thing does not work correctly.

Before the nodes are drawn, I analyze if two specific nodes intersect by using:

[[self playerSpriteNode] intersectsNode: [self pSKLabelNode]]

When pSKLabelNode touches desiredSpriteNode it works perfect! (By returning true, or false when it doesn't intersect)

But when it "passes" by a few pixels away from the SKLabel it still intersects and returns true.

Is there some setup that are recommended to fix the frame size of the nodes, or solutions that you think that will fix the problem?

Was it helpful?

Solution

I have the same problem when I try to intersects static node (that located left) with node that have rotation (and flying from the right side). I fix it like this

if ([ninja intersectsNode:node] &&
    CGRectGetMinX(node.frame) <= CGRectGetMaxX(ninja.frame) &&
    CGRectGetMaxX(node.frame) >= CGRectGetMinX(ninja.frame))
{
    //and here I have intersects
}

So I fix it by adding additional parameters

OTHER TIPS

The intersectNode method is optimized for running fast on devices with lots of iterations per second. Due to this, it actually "estimates" collision based on math, which sometimes goes wrong at a margin of a few pixels, specially when we are speaking of square corners of PNGs.

I had this problem once too, and since i used circles I calculated distance between circles as a second verification.

So, what you can do is a custom verification INSIDE the intersectsNode if case. Assuming you handle squares, you could verify wether the x or y collides after the intersectNode. It could be something like like:

    if([[self playerSpriteNode] intersectsNode: [self pSKLabelNode]]){
        if(distance between x1 and x2 < size1.width/2 + size2.width/2 || distance between y1 y2 < size1.height/2 + size2.height/2){
            //Your code goes here
        }
    }

Note that we compare central x distances with half each widths summed. This is only an example that works with squares, and most generic sprites.

I would like to point out that, while intersectsNode is slightly imprecise, this is NEEDED in order to run your game swiftly, as perfect and precise calculations per update can be very exhaustive to your device. So, should you do a custom verification, ALWAYS call it after intersectsNode returns true, as a second verification rather than the only one.

The answer provided above by Roman pretty much does what i said, in shorter code; I just wanted to leave an explanation about why.

Swift 2.1:

For increased results do your check in the update( _:) loop, is where things happen before the next drawing:

override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */

        if goodDude.intersectsNode(badDude){

            print("bad dude v.s. Ninja")

        }else{

            print("not dude at all")
        }

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top