1
votes

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
3

3 Answers

1
votes

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!

1
votes

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.

1
votes

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.