且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为带圆角的精灵设置物理体

更新时间:2022-10-17 17:54:50

来自

let spaceShipTexture = SKTexture(imageNamed: "spaceShip.png")

宇宙飞船 1:圆形物理体

let circleSpaceShip = SKSpriteNode(texture: spaceShipTexture)circleSpaceShip.physicsBody = SKPhysicsBody(circleOfRadius: max(circularSpaceShip.size.width/2,circleSpaceShip.size.height/2))

宇宙飞船 2:矩形物理体

let rectangleSpaceShip = SKSpriteNode(texture: spaceShipTexture)rectangleSpaceShip.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: circleSpaceShip.size.width,高度:circularSpaceShip.size.height))

宇宙飞船 3:多边形物理体

let polygonalSpaceShip = SKSpriteNode(texture: spaceShipTexture)让路径 = CGMutablePath()path.addLines(之间:[CGPoint(x:-5,y:37),CGPoint(x:5,y:37),CGPoint(x:10,y:20),CGPoint(x: 56, y: -5), CGPoint(x: 37, y: -35), CGPoint(x: 15, y: -30),CGPoint(x: 12, y: -37), CGPoint(x: -12, y: -37), CGPoint(x: -15, y: -30),CGPoint(x: -37, y: -35), CGPoint(x: -56, y: -5), CGPoint(x: -10, y: 20),CGPoint(x: -5, y: 37)])path.closeSubpath()多边形SpaceShip.physicsBody = SKPhysicsBody(polygonFrom: path)

Spaceship 4:使用纹理的 alpha 通道的物理体

let texturedSpaceShip = SKSpriteNode(texture: spaceShipTexture)texturedSpaceShip.physicsBody = SKPhysicsBody(texture: spaceShipTexture,尺寸:CGSize(宽度:circularSpaceShip.size.width,高度:circularSpaceShip.size.height))

I have created a SKShapeNode in the following way,

let sprite = SKShapeNode(rect: CGRect(x: -20, y: -10, width: 40, height: 20), cornerRadius: 10)

I also set a physics body like so,

sprite.physicsBody = SKPhysicsBody(rectangleOf: sprite.frame.size)

but I realized that the collisions and contacts weren't precise, so I changed showsPhysics to true and got the following. How can I make the physics body precise, even for the rounded corners?

From the documentation:

Maybe the #4 is your case

let spaceShipTexture = SKTexture(imageNamed: "spaceShip.png")

Spaceship 1: circular physics body

let circularSpaceShip = SKSpriteNode(texture: spaceShipTexture)
circularSpaceShip.physicsBody = SKPhysicsBody(circleOfRadius: max(circularSpaceShip.size.width / 2,
                                                                  circularSpaceShip.size.height / 2))  

Spaceship 2: rectangular physics body

let rectangularSpaceShip = SKSpriteNode(texture: spaceShipTexture)
rectangularSpaceShip.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: circularSpaceShip.size.width,
                                                                     height: circularSpaceShip.size.height))

Spaceship 3: polygonal physics body

let polygonalSpaceShip = SKSpriteNode(texture: spaceShipTexture)
let path = CGMutablePath()
path.addLines(between: [CGPoint(x: -5, y: 37), CGPoint(x: 5, y: 37), CGPoint(x: 10, y: 20),
                        CGPoint(x: 56, y: -5), CGPoint(x: 37, y: -35), CGPoint(x: 15, y: -30),
                        CGPoint(x: 12, y: -37), CGPoint(x: -12, y: -37), CGPoint(x: -15, y: -30),
                        CGPoint(x: -37, y: -35), CGPoint(x: -56, y: -5), CGPoint(x: -10, y: 20),
                        CGPoint(x: -5, y: 37)])
path.closeSubpath()
polygonalSpaceShip.physicsBody = SKPhysicsBody(polygonFrom: path)

Spaceship 4: physics body using texture’s alpha channel

let texturedSpaceShip = SKSpriteNode(texture: spaceShipTexture)
texturedSpaceShip.physicsBody = SKPhysicsBody(texture: spaceShipTexture,
                                              size: CGSize(width: circularSpaceShip.size.width,
                                                           height: circularSpaceShip.size.height))