如何防止两个sprite节点碰撞时发生倾斜。比方说,我有一个长矩形的PhysicsBody 1和一个短矩形的PhysicsBody 2。我想让这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 1和physicsBody 2。我添加了node.physicsBody?.allowRotation = false,就可以了。