1

I currently have two SKSpriteNodes on top of each other like so (The white part is one and the brown round circle part is the other one):

enter image description here

The white part is positioned 1/3 the way down on top of the round brown sprite node. In the picture, the brown round part has a SKPhysicsBody applied to it already as seen by the light blue outline around it. When I add a SKPhysicsBody around the top ovalish white part it pushes it up and not in the position I wanted it.

enter image description here

How can I have a SKPhysics body coving both bodies of sprites but not have the physics bodies push on one another which makes the white part move upwards? I would like the white part to stay in the position it was in the first image.

Thanks for anyone help!

Here's the code I used for the SKPhysicsBody's:

//  create, position, scale & add the round body
roundBody = SKSpriteNode( imageNamed: "roundBody" )
roundBody.position     = CGPoint( x: 207, y: 70 )
roundBody.zPosition    = 1
roundBody.xScale       = 0.3
roundBody.yScale       = 0.3
//  add sprite node to view
self.addChild( roundBody )

// create, position, scale & add the head
theHead!.position    = CGPoint( x: 207, y: roundBody.frame.maxY / 1.15 )
theHead!.zPosition   = 2
theHead!.xScale      = 0.3
theHead!.yScale      = 0.3

//  setting up a SKPhysicsBody for the round body
roundBody.physicsBody = SKPhysicsBody( circleOfRadius: roundBody.size.width / 4 )
roundBody.physicsBody!.dynamic = true
roundBody.physicsBody!.affectedByGravity  = true
roundBody.physicsBody!.allowsRotation = false
roundBody.physicsBody!.pinned = false

//  setting up a SKPhysicsBody for the head
theHead!.physicsBody = SKPhysicsBody(circleOfRadius: theHead!.size.width / 2 )
theHead!.physicsBody!.dynamic = true
theHead!.physicsBody!.affectedByGravity = false
theHead!.physicsBody!.allowsRotation = false
theHead!.physicsBody!.pinned  = false
Alan Guilfoyle
  • 381
  • 4
  • 13

3 Answers3

1

If you never want it to move, set it's .dynamic property to false. Then other objects may or may not bounce/collide with it (depending upon their collisionBitMask) but it won't move in response to those collisions.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • I tried setting the physicsBody's dynamic property to false and yes that did fix my issue with it being positioned correctly, however now I can't apply velocity to those SKSpriteNodes. Any thoughts on how I can keep the positioning but be able to move the Sprites via velocity, impulse, etc? – Alan Guilfoyle Mar 28 '16 at 16:57
  • @AlanGuilfoyle - would it make sense to make theHead a child of roundBody, so that they move together? Are they always together to form a larger sprite? – Steve Ives Mar 29 '16 at 13:51
  • I'm not sure how to do inheritance within spritekit. The 'theHead' would be a child of the 'roundBody' since they would be always together. However, I was able to fix my problem I with a SKPhysicsJointPin – Alan Guilfoyle Mar 29 '16 at 13:58
  • 1
    Alan - Glad it's working and you're right if you want the two to move independently but attached. for a simpler technique, it's not inheritance, but add the head as a child node. So having created the body and the head as SKSpriteNodes and added the body with addChild(Body), you set the head's position to (x: 0, y: -50) and then do body.addChild(head), which will add the head 50 pixels above the body. then just move the body around and the head will move with it. If you se the head's anchor to middle bottom of it's frame, you can rotate the head about that point too for some movement. – Steve Ives Mar 29 '16 at 14:03
  • -Steve: Awesome, I'm going to try that. I appreciate tips and help! I'll let you know how it goes! – Alan Guilfoyle Mar 29 '16 at 14:10
  • @AlanGuilfoyle - no problem. Remember that when adding children to existing SKSpritnodesto build up a more complex sprite, the anchor point of the child is very important and the child's position is relative to the parent, not the scene. – Steve Ives Mar 29 '16 at 14:20
1

I was able to figure out that if you use SKPhysicsJointPin it does the exact thing I needed! (Which was to basically pin a sprite head on it's body and share a physics body)

let joinTogether = SKPhysicsJointPin.jointWithBodyA(
                    roundBody.physicsBody!,
                    bodyB:theHead!.physicsBody!,
                    anchor: GPointMake(CGRectGetMidX(roundBody.frame), 
                    CGRectGetMinY(theHead!.frame)))

scene!.physicsWorld.addJoint(joint)

Hope this helps someone in the future!

Alan Guilfoyle
  • 381
  • 4
  • 13
1

Your own answer is correct and a better solution but just to explain further.

The reason of the bodies colliding is that by default a physics body's collision bit mask is set to all categories which means it will collide with everything. In your code you are not calling

roundBody.physicsBody?.collisionBitMask = ...

which is why its using the default values.

To change that you could give your body and head a different collisionBitMask. Im sure you will deal with this sooner or later when you handle collisions

Also as a tip it's a better idea to not force unwrap the physics bodies unless you have too, even though you know they exist. So you should replace your ! with ? whenever possible.

crashoverride777
  • 10,581
  • 2
  • 32
  • 56