Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprite Kit - Adding two physics bodies to one SKNode

Is it possible to add two (ore more) SKPhysicsBodys to one SKNode? Something similar to this: Example from PhysicsEditor Because the head of the character should collide with a ball, the top should be round. Furthermore the ball mustn't go through the player. Do you have an idea how to accomplish this?

like image 661
dennis-tra Avatar asked Sep 24 '13 22:09

dennis-tra


1 Answers

As the physicsBody property on SKNode suggests, there's a one-to-one relationship between nodes and physics bodies.

However, that doesn't mean you have to have one basic shape for every visible sprite. There are a few approaches you can take to accomplish what you're looking for:

  • Does the top really need to be rounded? You can cover most of the monkey art with a rectangle. (I presume you want a rounded top so collisions bounce in different directions, though.)
  • Create the "round-ended-rectangle" shape you're after using a polygon. You'll have to pick a number of sides for approximating the curve that fits your app: too many and it'll slow down the physics simulation, too few and it won't behave like a circle when other bodies bounce off.
  • Every body needs a node, but not every node needs a visible sprite. You can make your monkey out of two nodes: one that holds the art and has the square or round physics body attached, and another node that has the other physics body, attached through a fixed joint, but with no art.
like image 72
rickster Avatar answered Sep 24 '22 09:09

rickster