4

Is it possible to position a physics body on a sprite? I only want a certain part of my sprite node to have collision detection, not the whole image.

Heres my physics body

physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: CGFloat(54.0), height: CGFloat(100.0)))

But i want to position the physics body at the top of the node, where it usually gets placed in the middle of the node.

rakeshbs
  • 24,392
  • 7
  • 73
  • 63
Rachel Evans
  • 120
  • 5

2 Answers2

5

You can try creating a smaller SKSpriteNode of the same size as the SKPhysicsBody and adding the larger SKSpriteNode as a child to the smaller one. Changing the position of the larger one as you want. For example

override func didMoveToView(view: SKView) {

    let smallerSprite = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(30, 30))
    smallerSprite.physicsBody = SKPhysicsBody(rectangleOfSize: smallerSprite.size)
    smallerSprite.position = CGPointMake(100, 400)
    self.addChild(smallerSprite)

    let largerSprite = SKSpriteNode(color: UIColor(white: 0.5, alpha: 0.5), size: CGSizeMake(100, 100))
    largerSprite.position = CGPointMake(-10, -10)
    smallerSprite.addChild(largerSprite)

    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)

}
rakeshbs
  • 24,392
  • 7
  • 73
  • 63
  • 1
    This actually worked perfectly for me :) as my images physics body changes depending on the core motions acceleration.x. so based on the position i can change the larger sprites position relative to where i need the physics body. I want to up vote this answer but currently not high enough to do this :( But thank you so much for the fast response rakeshbs and making my day a lot less stressful. Hope you have a good rest of the day! Rachel – Rachel Evans Oct 15 '15 at 12:45
3

As an addition to rakesh's answer...A different approach to get the same result would be to use + bodyWithRectangleOfSize:center: method. Like this:

override func didMoveToView(view: SKView) {

let sprite = SKSpriteNode(color: SKColor.whiteColor(), size: CGSize(width: 100.0, height: 100.0))

//I assume that you have initialized view and scene properly. If  so, this will position a sprite in the middle of the screen.
sprite.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))
var physicsBodySize:CGSize = CGSize(width: sprite.size.width, height: 30.0) //Create a size here. You can play with height parameter.

sprite.physicsBody =
    SKPhysicsBody(rectangleOfSize: physicsBodySize, center: CGPoint(x: 0.0, y: sprite.size.height / 2.0 - physicsBodySize.height / 2.0))

//Not needed, just set to restrict that sprite can't off screen
sprite.physicsBody?.affectedByGravity = false
self .addChild(sprite)

}

The result:

enter image description here

If you try to change the height of physics body to 10.0, you will get something like this:

enter image description here

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • Thank you for the response Whirlwind! rakeshbs answer fits best for me only reason being i need to change the x and y position of the physics body depending on the core motion acceleration.x position. But this I know I will most definitely need in another project of mine. Hope you have a great rest of your day, and thanks again for taking the time to answer my question! Rachel – Rachel Evans Oct 15 '15 at 12:49