8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

3つ以上のオブジェクトの衝突

Last updated at Posted at 2014-10-14

タイトル通りでございますが
3つのオブジェクトの衝突について
当方はかなり手こずりましたので、投稿させていただきます。

contact1 (1).gif

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

let whiteCategory:  UInt32 = 0x1 << 0
let blackCategory:    UInt32 = 0x1 << 1
let blueCategory:   UInt32 = 0x1 << 2


override func didMoveToView(view: SKView) {
    

    self.physicsWorld.contactDelegate = self
    
    self.size = view.bounds.size
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsWorld.gravity = CGVectorMake(0.0, -3.0)
    

    let blackSquare = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
    blackSquare.position = CGPoint(
        x: CGRectGetMidX(self.frame),
        y: CGRectGetMidY(self.frame)
    )
    blackSquare.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
    blackSquare.physicsBody?.affectedByGravity = false
    blackSquare.physicsBody?.dynamic = false
    

    blackSquare.physicsBody?.categoryBitMask = blackCategory
    blackSquare.physicsBody?.contactTestBitMask = whiteCategory
    

    let blueSquare = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(50, 50))
    blueSquare.position = CGPoint(
        x: CGRectGetMidX(self.frame) - 100,
        y: CGRectGetMidY(self.frame) - 100
    )
    blueSquare.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
    blueSquare.physicsBody?.affectedByGravity = false
    blueSquare.physicsBody?.dynamic = false

//(2)
// blackSquare.physicsBody?.categoryBitMask = blueCategory
// blackSquare.physicsBody?.contactTestBitMask = whiteCategory

    blueSquare.physicsBody?.categoryBitMask = blueCategory
    blueSquare.physicsBody?.contactTestBitMask = whiteCategory
    

    self.addChild(blackSquare)
    self.addChild(blueSquare)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    

    for touch in touches {
        
        let location = touch.locationInNode(self)
        let whiteRectangle = SKSpriteNode(color: UIColor.whiteColor(), size: CGSizeMake(50, 50))
        whiteRectangle.position = location
        whiteRectangle.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
        

        whiteRectangle.physicsBody?.categoryBitMask = whiteCategory

// whiteRectangle.physicsBody?.contactTestBitMask = blackCategory
// whiteRectangle.physicsBody?.contactTestBitMask = blueCategory

        self.addChild(whiteRectangle)
    }
    
}

override func update(currentTime: CFTimeInterval) {
}


func didBeginContact(contact: SKPhysicsContact!) {
    
    var firstBody, secondBody: SKPhysicsBody
    

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
        //thirdBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
       // thirdBody = contact.bodyA
    }
    

    if firstBody.categoryBitMask & whiteCategory != 0 &&
        secondBody.categoryBitMask & blackCategory != 0 {
            //secondBody.node?.removeFromParent()
            println("black")
    }
    

    if firstBody.categoryBitMask & whiteCategory != 0 &&
        secondBody.categoryBitMask & blueCategory != 0 {
            //secondBody.node?.removeFromParent()
            println("blue")
    }
}

}

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?