如何防止碰撞时两个Sprite节点倾斜。假设我有一个带有PhysicsBody 1的长矩形和一个带有PhysicsBody 2的短矩形。我希望这两个矩形发生碰撞,但不希望长矩形在发生碰撞后倾斜但仍保持笔直。我该如何实现?这是长矩形的代码:
thePlayer.position = CGPoint(x: 100 - self.frame.size.width/2, y: 100 - self.frame.size.height/2)
thePlayer.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 40, height: 40))
thePlayer.physicsBody?.categoryBitMask = gamePhysics.Player
thePlayer.physicsBody?.collisionBitMask = gamePhysics.Wall
thePlayer.physicsBody?.contactTestBitMask = gamePhysics.Wall
thePlayer.physicsBody?.isDynamic = true
thePlayer.physicsBody?.affectedByGravity = false
这是短矩形的代码:
tileNode.position = CGPoint(x: x, y: y)
tileNode.physicsBody = SKPhysicsBody.init(rectangleOf: tileSize, center: CGPoint(x: tileSize.width / 2.0, y: tileSize.height / 2.0))
tileNode.physicsBody?.categoryBitMask = gamePhysics.Wall
tileNode.physicsBody?.collisionBitMask = gamePhysics.Player
tileNode.physicsBody?.contactTestBitMask = gamePhysics.Player
tileNode.physicsBody?.isDynamic = false
正如New Dev所说,解决方案很简单。我添加了node.physicsBody?.allowsRotation = false,它可以工作。