SpriteKit SKPhysicsJointFixed odd behaviour

青春壹個敷衍的年華 提交于 2019-12-22 10:51:52

问题


I am trying to connect together two SKNodes which both have equal size circular physics bodies. They are positioned so as to be touching each other, and I want them to be locked together. From the documentation, it sounds like I want a SKPhysicsJointFixed, and I have tried creating one with the anchor point being the midpoint between the two nodes - like this:

let fixedJoint  = SKPhysicsJointFixed.joint(withBodyA: atom1.physicsBody!, bodyB: atom2.physicsBody!, anchor:midPoint)

but this causes an odd behaviour where after the joint is made the top node falls through the bottom node - where before the joint existed the physicsbodies rested against each other.

If I use a pin joint instead with the same code - it works as expected ie:

let pinJoint  = SKPhysicsJointPin.joint(withBodyA: atom1.physicsBody!, bodyB: atom2.physicsBody!, anchor:midPoint)

locks the bodies together as I want them to be. I guess this is a perfectly fine solution - but I'm confused something about what is going on. Why does my pin joint do what I thought the fixed joint would do, and why does the fixed joint not do what I thought it would?


回答1:


Perhaps you were running into the same problem I ran into. I've found that SKPhysicsJointFixed behaves very strangely if the SKNode.zRotation property in the two nodes you're joining is different. Here's the behavior I was trying to get, having nodes of certain types stick together when they collide. Note how, after the collision, they rotate around their common center of mass.

But I was often getting this sort of thing, and often even stranger than this. Notice that not only does it not rotate as expected, it starts to wiggle as it approaches the wall.

As you can see, the difference between the two scenarios is that the zRotations are equal in the first case, unequal in the second case. Seems like a bug in SpriteKit, if you ask me.

One workaround is simply to explicitly set the two zRotations equal before you create the joint, but this was unacceptable for my purposes. Fortunately, as you already found, you can consistently get the expected behavior from SKPhysicsJointPin. Of course, if you want the pin joint to behave like a fixed joint, you'll have to set the joint's shouldEnableLimits property to true.



来源:https://stackoverflow.com/questions/43402230/spritekit-skphysicsjointfixed-odd-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!