import SpriteKit
import GameplayKit
extension SKPhysicsBody {
func ideal() -> SKPhysicsBody {
//摩擦係数の値で 1 に近いほど摩擦が多くなります0.0から1.0の間
self.friction = 0
self.linearDamping = 0 //粘性//体にかかる流体または空気の摩擦力
self.angularDamping = 0 //体の回転速度を遅くする特性 0.0から1.0の間の値
self.restitution = 0.98 //反発率
self.isDynamic = true //動かせる性質
self.allowsRotation = false //角力とインパルスの影響デフォルト値はtrue
// 重力を無視する 重力の影響を受けない
//circleA.physicsBody?.affectedByGravity = false
self.affectedByGravity = true
return self
}
}
class GameScene: SKScene, SKPhysicsContactDelegate {
let circleACategory: UInt32 = 0x1 << 0
let squareACategory: UInt32 = 0x1 << 1
let squareBCategory: UInt32 = 0x1 << 2
let bottomCategory:UInt32 = 0x1 << 3
var n = 0
let label = SKLabelNode(text: "kaisu: 0kai")
override func didMove(to view: SKView) {
backgroundColor = .blue
self.physicsWorld.contactDelegate = self
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
physicsWorld.gravity = CGVector(dx: 0, dy: -1.5)
let labelSize: CGFloat = 30.0
label.fontSize = labelSize
label.position = CGPoint(x:self.frame.midX, y:self.frame.height/1.12)
label.fontColor = SKColor.white
self.addChild(label)
let circleA = SKShapeNode(circleOfRadius: 40)//circleOfRadiusで円の半径
circleA.position = CGPoint(x:self.frame.midX, y:self.frame.midY + 140)
circleA.fillColor = UIColor.red
circleA.lineWidth = 4.0
circleA.strokeColor = UIColor.lightGray
self.addChild(circleA)
circleA.physicsBody = SKPhysicsBody(circleOfRadius: circleA.frame.width/2).ideal()
let squareA = SKShapeNode(rectOf: CGSize(width:50, height:50),cornerRadius: 3)
squareA.position = CGPoint(x:self.frame.midX, y:self.frame.midY + 10)
squareA.fillColor = UIColor.yellow
squareA.lineWidth = 2.0
squareA.strokeColor = UIColor.lightGray
self.addChild(squareA)
squareA.physicsBody = SKPhysicsBody(rectangleOf: squareA.frame.size).ideal()
squareA.physicsBody?.affectedByGravity = false
let squareB = SKShapeNode(rectOf: CGSize(width:100, height:100),cornerRadius: 5)
squareB.position = CGPoint(x:self.frame.midX, y:self.frame.midY - 200)
squareB.fillColor = .green
squareB.lineWidth = 0.0
self.addChild(squareB)
squareB.physicsBody = SKPhysicsBody(rectangleOf: squareB.frame.size).ideal()
let bottomRect = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: 1)
let bottom = SKNode()
bottom.physicsBody = SKPhysicsBody(edgeLoopFrom: bottomRect)
self.addChild(bottom)
//Aカテゴリ
circleA.physicsBody?.categoryBitMask = circleACategory
//Bカテゴリ
squareA.physicsBody?.categoryBitMask = squareACategory
squareB.physicsBody?.categoryBitMask = squareBCategory
//bottomカテゴリ
bottom.physicsBody?.categoryBitMask = bottomCategory
circleA.physicsBody?.contactTestBitMask = squareACategory | squareBCategory | bottomCategory
}
//衝突判定処理
func didBegin(_ contact: SKPhysicsContact) {
n += 1
label.text = "kaisu: \(n)kai"
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == circleACategory && secondBody.categoryBitMask == squareACategory{
secondBody.node?.removeFromParent()
}
if firstBody.categoryBitMask == circleACategory && secondBody.categoryBitMask == squareBCategory{
secondBody.node?.removeFromParent()
}
if firstBody.categoryBitMask == circleACategory && secondBody.categoryBitMask == bottomCategory{
firstBody.node?.removeFromParent()
}
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
}
}