我正在评估iOS SpriteKit物理引擎,为了测试,我创建了一个包含两个圆形节点的Xcode的简单场景:
两个节点都有圆形的物理体和5千克的质量。
我用SKPhysicsJointSpring
连接两个节点。这是整个设置(在viewDidLoad()
中):
let path = NSBundle.mainBundle().pathForResource("MyScene", ofType: "sks")!
let scene = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as! SKScene
let border = SKPhysicsBody(edgeLoopFromRect: scene.frame)
border.restitution = 0;
border.friction = 0;
scene.physicsBody = border
let player = scene.childNodeWithName("player")! // large ball
let tail1 = scene.childNodeWithName("tail1")! // smaller ball
player.physicsBody!.usesPreciseCollisionDetection = true
tail1.physicsBody!.usesPreciseCollisionDetection = true
let spring1 = SKPhysicsJointSpring.jointWithBodyA(player.physicsBody!, bodyB: tail1.physicsBody!, anchorA: player.position, anchorB: tail1.position)
spring1.damping = 1
spring1.frequency = 3
scene.physicsWorld.addJoint(spring1)
spriteKitView.presentScene(scene)
注意:场景编辑器中的重力设置为(0,0)。
当我移动较大的节点时(例如,通过使用touchesBegan()捕获触摸并将节点的位置设置为触摸位置),另一个根据弹簧参数进行跟踪。
但是,如果我足够快地移动节点,使弹簧力变得极端,则弹簧收缩时两个节点都会重叠。我期待它们彼此冲突,因为它们的collisionBitMask
默认设置为-1(所有位都设置)。我已启用usesPreciseCollisionDetection
,但效果仍然可见。
当我在场景周围添加边缘循环时,它们会按预期与该边缘碰撞。与具有物理主体(但没有连接关节)的其他节点相同。
我的印象是,弹簧的存在会以某种方式使引擎忽略与关节连接的节点之间的碰撞。 还有其他人观察过这个吗? 我忘了什么吗?或者这是否按预期工作?