gpt4 book ai didi

ios - 添加 SKPhysicsJointLimit 后 SKPhysicsBodies 不会发生碰撞

转载 作者:行者123 更新时间:2023-11-30 13:14:15 25 4
gpt4 key购买 nike

我目前有两个 SKSpriteNode,我已将 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)

两个节点的PhysicsBody:

self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 2)
self.physicsBody?.allowsRotation = false
self.physicsBody?.friction = 0
self.physicsBody?.mass = 0.1

我已经使用不同的值对 SKPhysicsBody 的上述修改进行了测试,结果是相同的。

最佳答案

SKPhysicsJoint 对象连接两个物理体,以便物理世界将它们模拟在一起。您还可以使用SKPhysicJointPin:

A pin joint allows the two bodies to independently rotate around the anchor point as if pinned together.

如果您的对象在使用物理引擎添加 SKPhysicsJoint 之前工作良好,因此它们按照您的意愿和设置触发了 didBeginContact,我认为您的问题很简单一个错误的 anchor 。尝试添加:

让 skView = self.view as! SK View
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)
}
}
}

希望它可以帮助您找到问题。

关于ios - 添加 SKPhysicsJointLimit 后 SKPhysicsBodies 不会发生碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38417683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com