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?
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.
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
.
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.
© 2022 - 2024 — McMap. All rights reserved.