我正在评估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,但效果仍然可见。
当我在场景周围添加一个边缘循环时,它们与该边缘发生了预期的碰撞。对于具有物理体而没有连接关节的其他节点也是如此。
我有一种感觉,弹簧的存在使引擎忽略了通过关节连接的节点之间的碰撞。是否还有其他人观察到这一点?我有遗漏吗?还是这是预期的行为?