• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Spritekit

My WordPress site

  • Top
  • SpriteKitBase
    • Collision
    • Enumerated
    • Shooting
    • Breakout
    • Physics1
      • physics4
      • physics3
      • Physics2
  • SwiftUI&SpriteKit
    • TabScroll
    • LocalWebView
      • Counter
      • StateObject
      • Button
      • SceneMove
    • Swift Learning
  • Photo Gallery
    • Photo Galler 2
    • Photo Gallery 3
    • リンク
      • プロフィール
      • p5.play test

physics3

04/24/2022 by kyougif


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 {

override func didMove(to view: SKView) {
  backgroundColor = .blue
  self.physicsWorld.contactDelegate = self
  physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
  physicsWorld.gravity = CGVector(dx: 0, dy: -2)

let circleA = SKShapeNode(circleOfRadius: 50)//circleOfRadiusで円の半径
  circleA.position = CGPoint(x:self.frame.midX, y:self.frame.midY + 230)
  circleA.fillColor = UIColor.red
  circleA.lineWidth = 4.0
  circleA.strokeColor = UIColor.lightGray
  self.addChild(circleA)
  circleA.physicsBody = SKPhysicsBody(circleOfRadius: circleA.frame.width/2).ideal()

  //物体Aのカテゴリ0と物体Bの衝突が等しくなければAはBを通過する。それ以外は衝突する
   //Aカテゴリ
  circleA.physicsBody?.categoryBitMask = 0x1 << 0
   //通過する(衝突される側のcategoryBitMaskとcollisionBitMaskを0にする)
   circleA.physicsBody?.collisionBitMask = 0x1 << 0
    
  let circleB = SKShapeNode(circleOfRadius: 25)//circleOfRadiusで円の半径
   circleB.position = CGPoint(x:self.frame.midX, y:self.frame.midY + 110)
   circleB.fillColor = UIColor.yellow
   circleB.lineWidth = 4.0
   circleB.strokeColor = UIColor.lightGray
   self.addChild(circleB)
   circleB.physicsBody = SKPhysicsBody(circleOfRadius: circleB.frame.width/2).ideal()   
 circleB.physicsBody?.categoryBitMask = 0x1 << 1
 circleB.physicsBody?.collisionBitMask = 0x1 << 1

// 四角形を作成
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()
//Bカテゴリ
  squareB.physicsBody?.categoryBitMask = 0x1 << 0 + 0x1 << 1
  //通過する(衝突される側のcategoryBitMaskとcollisionBitMaskを1にする)
  squareB.physicsBody?.collisionBitMask = 0x1 << 0 + 0x1 << 1
//後から指定したsquareBが上に表示される
}
   override func touchesBegan(_ touches: Set, with event: UIEvent?) {
}

}

Filed Under: Programming, spritekit

Primary Sidebar

最近の投稿

  • ObservableObj
  • SideMenu
  • TabScroll
  • Text
  • TappedNodes
  • Collision2
  • CoreMotion
  • Collision
  • Timer
  • Enumerated
  • Random
  • SwiftUI-List
  • Shooting
  • Sound
  • Camera
  • SpriteSheet
  • SKPhysics42
  • physics4
  • physics3
  • Physics2

アーカイブ

  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2014年9月
  • 2014年5月

カテゴリー

  • Programming
  • spritekit
  • swiftui

固定ページ

  • Home
  • p5.play test
  • Photo Galler 2
  • Photo Gallery
  • Photo Gallery 3
  • Top
  • プロフィール
  • リンク

Copyright © 2026 · Genesis-child on Genesis Framework · WordPress · Log in