通过didBeginContact提供的接触点并不总是在休息界定参与碰撞的物理机构的路径。 其中,机构从来没有接触到这种行为的情况下最容易出问题的(如果圆节点是1px的进一步正确,接触仍然发生,虽然它是数学上是不可能。)
在图像和以下程序,该矩形具有的100像素的宽度,但接触点(在输出示出)是在101.4550 ...
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var node1:SKShapeNode
var node2:SKShapeNode
var node2PhysicsPath:CGPath
var contactPoint:SKShapeNode
required init?(coder aDecoder: NSCoder) {
contactPoint = SKShapeNode(circleOfRadius: 10)
contactPoint.strokeColor = SKColor(calibratedRed: 1, green: 0, blue: 0, alpha: 1)
node1 = SKShapeNode()
node1.position.x = 150
node1.position.y = 200
node1.physicsBody = SKPhysicsBody(circleOfRadius: 50)
node1.physicsBody?.contactTestBitMask = 0xffffffff
node1.physicsBody?.categoryBitMask = 0xffffffff
node1.physicsBody?.usesPreciseCollisionDetection = true
node2PhysicsPath = CGPathCreateWithRect(CGRect(x: 0, y: 0, width: 100, height: 100), nil)
node2 = SKShapeNode()
node2.physicsBody = SKPhysicsBody(polygonFromPath: node2PhysicsPath)
node2.physicsBody?.dynamic = false
node2.physicsBody?.contactTestBitMask = 0xffffffff
node2.physicsBody?.categoryBitMask = 0xffffffff
node2.physicsBody?.usesPreciseCollisionDetection = true
super.init(coder: aDecoder)
physicsWorld.contactDelegate = self
}
func didBeginContact(contact:SKPhysicsContact){
println(contact.contactPoint);
contactPoint.position = contact.contactPoint
self.physicsWorld.speed = 0
println(CGPathContainsPoint(node2PhysicsPath, nil, contact.contactPoint, true))
}
override func didMoveToView(view: SKView) {
self.addChild(node1)
self.addChild(node2)
self.addChild(contactPoint)
self.physicsWorld.gravity = CGVector(dx: 0, dy: -1.0)
}
}