0

Hi I have some triangular shapes in my game that need a collisionBitMask to handle contact but I cant figure out how to model the physicsBody other than circleOfRadius or RectangleOfSize. How do I make a custom physics body so that the contacts are accurate. This is an issue for many different characters/obstacles in my game.

var randomNumber = arc4random() % 3
    var randomMountainImage:String!
    if randomNumber == 0 {
        randomMountainImage = "RedMountain.png"
    } else if randomNumber == 1 {
        randomMountainImage = "OrangeMountain.png"
    } else {
        randomMountainImage = "BeigeMountain.png"
    }
    var mountainTexture = SKTexture(imageNamed: randomMountainImage)
    var randomMountain = SKSpriteNode(texture: mountainTexture)
    var maxPositionOffset = min(100, movementAmount)
    randomMountain.position = CGPointMake( CGRectGetMidX(self.frame) + self.frame.width, 200 - gapHeight + CGFloat(movementAmount))
    randomMountain.zPosition = 8
//here is the problem
    randomMountain.physicsBody = SKPhysicsBody(rectangleOfSize: randomMountain.size)
    randomMountain.physicsBody?.dynamic = false
    self.addChild(randomMountain)
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Jeremy Sh
  • 609
  • 7
  • 24

1 Answers1

1

You can set the SKTexture as your SKPhysicsbody parameter:

randomMountain.physicsBody = SKPhysicsBody(texture: mountainTexture, size: mountainTexture.size)

The

Christian
  • 22,585
  • 9
  • 80
  • 106
  • Note that there is a known "upside down physicsbody" bug in iOS 8.0 and 8.1 using this approach which happens when you reuse the texture on subsequent sprites. http://stackoverflow.com/questions/27748034/spritekit-skphysicsbody-bodywithtexture-is-upside-down/ hopefully it doesn't happen to you, but if you notice an upside down physics body, you'll know what happened. – Patrick Collins Feb 06 '15 at 18:28