Sprite Kit - Adding two physics bodies to one SKNode
Asked Answered
E

3

8

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?

Egalitarian answered 24/9, 2013 at 22:29 Comment(1)
Use + (SKPhysicsBody *)bodyWithBodies: methodPskov
I
6

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.
Improve answered 24/9, 2013 at 23:15 Comment(2)
Just a note, iOS 8 now lets you add two physics bodies to one SKNode using bodyWithBodies:, as in SKPhysicsBody.bodyWithBodies(bodies: AnyObject[]?). Note that properties on the child bodies, such as mass or friction, are ignored. Only the shapes of the child bodies are used. If you need a node with multiple hit targets (e.g. body vs head) then you need to do what rickter suggests in bullet point #3.Lazar
This is not true, you can set multiple bodies with + (SKPhysicsBody *)bodyWithBodies: methodPskov
R
3

This is the top hit on Google for this question so i thought I'd update the answers. This is possible (Although may not have been in 2013), as per the answer from Mike S in this post.

You need to use

// Swift
SKPhysicsBody(bodies: <#[AnyObject]#>) 

// Obj-c
[SKPhysicsBody bodyWithBodies:(NSArray *)bodies]

So you create a body with other bodies and add that one body to your SKnode.

Radicand answered 27/4, 2015 at 10:48 Comment(0)
C
0

No, every SKNode can only have one SKPhysicsBody since physicsBody is a single property of SKNode. Like rickster said, your first option is to make the monkey one silo-shaped physics body using the bodyWithEdgeLoopFromRect: class method of SKPhysicsBody.

Your other option is to create two separate SKNodes and attach them with an SKPhysicsJointFixed. rickster's answer is great, but I just wanted to give the class names in case you want to do a little Googling around.

Cantonese answered 25/9, 2013 at 5:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.