SpriteKit SKPhysicsBody broken collisions
Asked Answered
A

1

1

I have a SKSpriteNode which has a static body, generated from concave path with edgeChainFromPath. Let's call this node a house.

I also have another kind of SKSpriteNodes which have a dynamic square body, generated with rectangleOfSize. Let's call this node a box.

In my scene, the boxes are thrown aka impulsed into the house and since both of them have bodies, the boxes should collide with the house and bounce away.

I'm scrolling the scene and therefore creating new instances of EXACTLY THE SAME house described above.

The problem I'm having is that collisions are completely or partially broken for some houses. Some houses collide with boxes exactly as they should, some collide with boxes only on some random segments of the body, and some completely ignore collisions with the boxes.

This problem only happens on iPad and never on iPhone. Happens both on iPad Simulator and real iPad device.

The problem occurs completely random and on random houses.

I also tried creating house body with edgeLoopFromPath but the problem remains.

Also, I'm drawing the physics and can see that the physics body of the house is exactly as it should be.

There is nothing else special about the scene or nodes that would be worth telling about.

I'm using SpriteKit with Swift in iOS 9.


Update:

Seems like the issue is in setting the scale on house nodes. I have to do that in order to have universal look on all screen sizes.

Still looking for solution/workaround...

Avie answered 18/5, 2016 at 16:46 Comment(11)
Have you enabled visual physics representation and checked that everything looks fine?Dunaville
@Dunaville like I said: "I'm drawing the physics". So yes. :)Avie
Do you have fast moving objects on the scene? Also if you are manually changing positions of nodes which can collide, then you can get weird behaviour...Dunaville
@Dunaville Yes, boxes are fast moving objects, since they are impulsed but it doesn't matter how fast they move. I've tried changing the velocity and I still get the same results. Talking about manually changing positions: I'm not manually changing the position of any node in the scene. I'm scrolling the scene by moving the new camera node, introduced in iOS 9.Avie
1) Try to set usesPreciseCollisionDetection on fast moving objects, just see if changes anything. Double check if you don't manually change the position on any node which participates in physics simulation. By manually changing I mean changing position property directly, or by using SKaction.moveXxx methods.Dunaville
@Dunaville Actually, I'm using usesPreciseCollisionDetection on boxes right now. The issue remains if I turn this off as well. I'm 100% sure I'm not changing the position of any node after it's added to the scene in any way. I've also tried changing the size of boxes to make them really big but that doesn't matter either. :/Avie
Well the you can upload a git repo which can reproduce the issue you are talking about and hopefully somebody will look at it.Dunaville
@Dunaville will try to do that, yes. Thanks for your time so far! +1Avie
@Dunaville Seems like it is scale that I'm applying to house nodes that messes up their physics body. Do you know any way to get around that? Definitely seems like a bug in SpriteKit since it works sometimes...Avie
Well personally I've seen that scaling a physics body sometimes works and sometimes doesn't. But it was like, it just works, or it doesn't work at all (this was based mostly on iOS version). Your case is specific. Never seen that, so I can't comment any further. In general, scaling physics bodies was often a problematic goal.Dunaville
@Dunaville yes, seems like that. Thanks!Avie
F
0

This is likely due to edgeLoopFromPath creating a hollow box with an edge for your house, not one with volume.

I would try using bodyWithPolygonFromPath as well as adding preciseCollisionDetection to both your boxes and houses.

Fajardo answered 24/5, 2016 at 23:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.