我目前有两个SKSpriteNodes,我已经添加了SKPhysicsBodies。当他们没有连接SKJoint时,他们会按预期发生碰撞。一旦我添加了SKPhysicsJoint,它们就会直接通过彼此。我正确地添加任何关节功能,但SKPhysicsJointLimit仅限制节点彼此分开的程度,而不是它们接近的程度。我该如何解决这个问题?
以下是我用于联合的代码:
let joint = SKPhysicsJointLimit.joint(withBodyA: object1.physicsBody!, bodyB: object2.physicsBody!, anchorA: CGPoint(x: object1.position.x + iconController.position.x, y: object1.position.y + iconController.position.y), anchorB: CGPoint(x: object2.position.x + iconController.position.x, y: object2.position.y + iconController.position.y))
joint.maxLength = screen.height * 0.4
physicsWorld.add(joint)
物理两个节点的体现:
self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 2)
self.physicsBody?.allowsRotation = false
self.physicsBody?.friction = 0
self.physicsBody?.mass = 0.1
我已经使用不同的值对SKPhysicsBody的上述修改进行了测试,并且它也执行相同的操作。
答案 0 :(得分:0)
SKPhysicsJoint
对象连接两个物理实体,以便它们由物理世界一起模拟。
您也可以使用SKPhysicJointPin
:
销钉接头允许两个机构围绕机身独立旋转 锚点好像固定在一起。
如果您的物体在物理引擎添加SKPhysicsJoint
之前运作良好,那么他们按照您的意愿解雇了didBeginContact
并且您已经设置了,我认为您的问题只是一个错误的锚点。尝试添加:
let skView = self.view as! SKView
skView.showsPhysics = true
到您的场景初始化代码:您将看到物理机构的轮廓,也许您会立即看到问题。
为了帮助您,我将尝试举例说明相互碰撞的元素:
enum CollisionTypes: UInt32 {
case Boundaries = 1
case Element = 2
}
class GameScene: SKScene,SKPhysicsContactDelegate {
private var elements = [SKNode]()
override func didMoveToView(view: SKView) {
physicsWorld.gravity = CGVector(dx: 0, dy: 0)
self.physicsWorld.contactDelegate = self
let boundariesFrame = CGRectMake(20, 20, 200, 400)
let boundaries = SKShapeNode.init(rect: boundariesFrame)
boundaries.position = CGPointMake(350,150)
let boundariesBody = SKPhysicsBody.init(edgeLoopFromRect: boundariesFrame)
boundariesBody.dynamic = false
boundariesBody.categoryBitMask = CollisionTypes.Boundaries.rawValue
boundariesBody.contactTestBitMask = CollisionTypes.Element.rawValue
boundaries.physicsBody = boundariesBody
addChild(boundaries)
for index in 0..<5 {
let element = SKShapeNode(circleOfRadius: 10)
let body = SKPhysicsBody(circleOfRadius: 10)
body.linearDamping = 0
// body.mass = 0
body.dynamic = true
body.categoryBitMask = CollisionTypes.Element.rawValue
body.contactTestBitMask = CollisionTypes.Boundaries.rawValue | CollisionTypes.Element.rawValue
body.collisionBitMask = CollisionTypes.Boundaries.rawValue | CollisionTypes.Element.rawValue
element.physicsBody = body
element.position = CGPoint(x: size.width / 2, y: size.height / 2 - 30 * CGFloat(index))
elements.append(element)
addChild(element)
}
}
}
希望它可以帮助您找到问题。