本文介绍了如何为带圆角的精灵设置物理体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我按照以下方式创建了一个SKShapeNode
,
let sprite = SKShapeNode(rect: CGRect(x: -20, y: -10, width: 40, height: 20), cornerRadius: 10)
我也像这样设置了一个物理体,
sprite.physicsBody = SKPhysicsBody(rectangleOf: sprite.frame.size)
但我意识到碰撞和接触并不精确,所以我将 showsPhysics
更改为 true
并得到以下结果.我怎样才能使物理体精确,即使是圆角?
解决方案
来自
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))
这篇关于如何为带圆角的精灵设置物理体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!