How to use usesPreciseCollisionDetection

Ok so I posted a question about this topic before and I was told to go more in-depth about my problem. I recently ran into an issue in Spritekit where a node would fly through another node in less than a frame so the system wouldn't catch it and it wouldn't bounce like I wanted it to. So I was told to use "usesPreciseCollisionDetection". I tried using the method you will see in my code snippet (line 27).



This is what my game is:

I have a line bouncing up a down the screen and there is a zone (I refer to it as "area") that you are supposed to get the line inside of. As the game progresses, the line gets faster and the zone gets smaller. Also at every multiple of 5, the line restarts from the bottom of the screen.

import SpriteKit
import GameplayKit
var impulseLevel = 30
var score = 0
var scoreString = String(score)
class GameScene: SKScene {
    var line = SKSpriteNode()
    var area = SKSpriteNode()
    var label = SKLabelNode(text: scoreString)
    override func didMove(to view: SKView) {
        line = self.childNode(withName: "line") as! SKSpriteNode
        area = self.childNode(withName: "area") as! SKSpriteNode
        score = 139
        scoreString = String(score)
        //label
        addChild(label)
        label.position = CGPoint(x: 0 , y: 400)
        label.fontColor = UIColor.black
        label.text = scoreString
        label.fontSize = 200
        label.fontName = "HelveticaNeue-Bold"
        //area position variables
        line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30  ))


        line.physicsBody?.usesPreciseCollisionDetection = true



        //border
        let border = SKPhysicsBody(edgeLoopFrom: self.frame)
        border.friction = 0
        border.restitution = 0
        self.physicsBody = border
        }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //variables
        
        var HalfHeight = area.size.height / 2
        var upperhalf = area.position.y + HalfHeight
        var lowerhalf = area.position.y - HalfHeight
    
        for touch in touches{
            if line.position.y >= lowerhalf && line.position.y <= upperhalf{
                print("Works")
                score = score + 1
                scoreString = String(score)
                label.text = scoreString
                var random = -550 + Int(arc4random_uniform(UInt32(550+550+1)))
                print(random)
                area.position = CGPoint(x: 0, y: random)
                
                if score == 5{
                    area.size.height = 285
                    line.position = CGPoint(x:0,  y: -600)
                    line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
                    line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30))
                }
                if score == 10{
                    area.size.height = 270
                    line.position = CGPoint(x:0,  y: -600)
                    line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
                    line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30))
                }
                if score == 15 {
                    area.size.height = 250
                    line.position = CGPoint(x:0,  y: -600)
                    line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
                    line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30))
                }
                if score == 20 {
                    line.position = CGPoint(x:0,  y: -600)
                    line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
                    line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 35))
                }
                if score == 25 {
                    area.size.height = 225
                    line.position = CGPoint(x:0,  y: -600)
                    line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
                    line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 35))
                }
                if score == 30 {
                    area.size.height = 205
                    line.position = CGPoint(x:0,  y: -600)
                    line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
                    line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 35))
                }
                if score == 35 {
                    line.position = CGPoint(x:0,  y: -600)
                    line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
                    line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 40))
                }
                var adding = impulseLevel/20
                var added = impulseLevel + adding
                
                if score > 35 && score % 5 == 0 {
                    adding = impulseLevel/20
                    added = impulseLevel + adding
                    line.position = CGPoint(x:0,  y: -600)
                    line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
                    impulseLevel = added
                    line.physicsBody?.applyImpulse(CGVector(dx: 0, dy:impulseLevel))
                }
            }else{
                print("Gay")
            }
        }
    }
    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
    }
}

Sometimes this works but not all the time. When the impulse gets higher than 120, the line will go through the border anyway.

First of all, you should better post your question in an appropriate topic area: SpriteKit Graphics framework for 2D games


And please show your ways you have tried as code. Or else you may need to see what you have tried again.

Well I have tried "line.usesPreciseCollisionDetection = true" but this acts very weird and erratic. I set the borders of the screen to be the borders that the line bounces off of. This works when the speed is somewhat low but when it gets higher it goes through the screen. So that's when I set usesPreciseCollisionDetection to true. But now it bounces off the top but not the bottom of the screen and sometimes it will bounce off nodes that I put on the screen but only in certain places and only when they are certain sizes.

`line.usesPreciseCollisionDetection = true` is the only way to make SpriteKit use preciseCollisionDetection.

And as far as I have tested till now, preciseCollisionDetection can be little help for high-speed objects.


I have never seen such a weird behavior as you describe, which should be reported as a bug.

When I type this in I get an error saying, “Value of type ‘SKSpriteNode’ has no member ‘usesPreciseCollisionDetection’ “

You say I have tried "line.usesPreciseCollisionDetection = true" but this acts very weird.


Which means you know how to use `usesPreciseCollisionDetection` on SKPhysicsBody. What do you really want to ask?

Well I actually used, line.physicsBody?.usesPreciseCollisionDetection = trie because I got the error. Sorry...

I see. You should better clarify what your current problem is.


You now know `line.physicsBody?.usesPreciseCollisionDetection = true` is the right way to enable precise collision detection, and still you may find some weird behaviors.


For me, it seems your code is just hitting the limitation of current SpriteKit, though I'm not sure. If you can describe your issue better, some experts may find it and show you how to improve it. Showing the whole code which can reproduce the issue will help to involve many readers.

Ok soon I’ll post a in depth post with all my code and a screen record of what happens

How to use usesPreciseCollisionDetection
 
 
Q