1

I’m trying to accurately determine the point at which a node moving with velocity hits the very edge of the screen. At the moment, the node’s position registers later, beyond the screen edge (and more so when travelling faster) when checking if its position is >= to self.frame.maxX (using == doesn't register at all).

Having initially tried using bit masks applied to the sprite and a border physics body acting as a wall - and coming across well known issues with collision detection in SpriteKit - I decided to opt for the default update function where I check for the x position instead.

However, both approaches seem problematic. From what I’ve read, update is called a maximum of 60 times per second, so in between frames, the node’s position registers later.

For the same reason I suppose, collision detection using didBegin suffers from the same problem, and even when the physics body has its collisionBitMask not set to zero - meaning even when the node bounces off it.

And checking for position in a custom function called by a timer with a time interval of say 0.00001 isn't accurate either.

As it stands there is no acceptable implementation - how can I precisely check for position?

Tim
  • 583
  • 1
  • 6
  • 18

1 Answers1

0

SKPhysicsBody isn't that precise and sometimes little buggy, you can try to use this in function didBegin. This always helped me, maybe it will work for you too.

if contact.contactPoint.x >= self.frame.maxX - 5 { 
    //do something
}
if contact.contactPoint.x <= self.frame.minX + 5{
    //do something

I don't know what do you mean by "travelling faster", this example was not having problem registering velocity just under 400, which is kinda fast.

dyouteotyi
  • 27
  • 4
  • This is one of the failed approaches I've tried, as you'll see above. The problem isn't that the position isn't detected, it's that it's detected beyond the specified point. The faster the node is moving when passing a point, the greater the distance past the point the node is when detected. – Tim Nov 05 '18 at 05:59