4

Hi I am getting a weird effect in my SpriteKit game.

I have a wall around the screen, which I had created staticbody by using bodyWithEdgeLoopFromRect.

By doing skView.showsPhysics = YES; all bodies are also shown in the screen.

I have a ball in screen. Which having dynamic body. sometime when I do provide Force to ball it pass throgh that static wall!!!

How is this possible??

Though I can see both bodies on screen, the dynamic ball sometime passes through the static wall.

I also given usesPreciseCollisionDetection = YES; property to both physics bodies. But it doesn't prevent this issue.

How can I prevent this issue?

zeeple
  • 5,509
  • 12
  • 43
  • 71
  • This can happen if the `collisionBitMask` is not set correctly or you apply too much force. – 0x141E Jul 24 '15 at 16:48
  • I think collisionBitMask is already set properly, If i didn't set it properly then this kind of scenario should happen every time, But this only happen when velocity of my ball(Dynamic physics body) is high. –  Jul 25 '15 at 07:44
  • Try applying and impulse instead of a force. Also as a workaround you can manually check the position of the node to prevent it from leaving the bounds of the screen. – Epic Byte Jul 25 '15 at 16:22
  • 1
    You can add a `SKConstraint` to keep your sprites on the screen. See positionX:Y here https://developer.apple.com/library/mac/documentation/SpriteKit/Reference/SKConstraint_Ref/#//apple_ref/occ/clm/SKConstraint/positionX:Y: – 0x141E Jul 25 '15 at 18:26
  • I will check about SKConstraint and let you knew. –  Jul 27 '15 at 07:26

1 Answers1

1

This happens when the velocity of an object is extremely high. For example, if your ball object is traveling at a velocity of X + 300 for each frame, then chances are it will not detect a "wall" and move past it.

Setting usesPreciseCollisionDetection to YES will not solve this if the velocity is too high.

You should apply a speed limit to your object's velocity. Something like this:

if(self.physicsBody.velocity.dx > 100)
    self.physicsBody.velocity = CGVectorMake(100, self.physicsBody.velocity.dy);

The above limits the right movement of your object to 100 while leaving the dy (up & down) velocity as is.

sangony
  • 11,636
  • 4
  • 39
  • 55