3

I'm trying to create a single physics body from several other bodies using the SKPhysicsBody(bodies:) init.

I have nodes throughout my scene that have physics bodies attached to them, and I want to consolidate them into a single physics body. I'm enumerating through the nodes of the scene and picking out the nodes I want to target. I'm creating the physics body at the creation of those nodes like this:

node.physicsBody = SKPhysicsBody(rectangleOf: node.size)
node.physicsBody?.isDynamic = false

Then I add the physics body to an array of SKPhysicsBodies like this:

self.allNodePhysicsBodies.append(node.physicsBody)

I then create the conglomerate physics body like this:

let conglomeratePhysicsBody = SKPhysicsBody(bodies: self.allNodePhysicsBodies)

I've tried several different ways of adding the physics bodies to the scene: By creating an SKNode or SKSpriteNode and adding the physics bodies to it and the adding it to the scene, or setting the scene's physics body conglomeratePhysicsBody. All of these methods end up with the physics bodies all aligned at the bottom of the scene (not positioned around the scene like they should be). When I print out the conglomeratePhysicsBody to the console, I get this message: <SKPhysicsBody> type:<Compound> representedObject:[(null)]. Is there something I'm missing that is keeping the physics bodies from appearing where the nodes are?

I should add that I can see the physics bodies by using skView.showsPhysics = true.

claassenApps
  • 1,137
  • 7
  • 14
  • @Knight0fDragon, I started working with this from a comment you made in a different post. Have you worked with these before? – claassenApps Oct 27 '16 at 03:09
  • was that the comment he made about something getting stuck on the gap between bodies, and that making an amalgamated body would prevent this stickiness in the gaps? If so, I think the problem might be that bodies need to be contiguous for the merger of bodies to work. – Confused Oct 27 '16 at 12:38
  • @Confused, thanks for the comment, but according to the Apple documentation, "these areas do not need to be contiguous. If there is space between two parts, other bodies may be able to pass between these parts." -https://developer.apple.com/reference/spritekit/skphysicsbody/1519736-init – claassenApps Oct 27 '16 at 12:41
  • I think it's often telling when Apple doesn't provide an example, in their documents, of how something might work. When they don't, I assume they don't have tests or examples of use they've tested against. In these cases there are often problems. – Confused Oct 27 '16 at 12:45
  • 1
    And it is HIGHLY annoying that the company with the most amazing amount of profit sitting in offshore companies (largest in the history of business) can't hire a few programmers to make demos of everything they create, and documentation that's up to date. Most of the SpriteKit demos that do exist in their docs are still only in Objective-C... – Confused Oct 27 '16 at 12:47
  • I agree, it is frustrating. Makes me thankful for a community like this to post questions to. – claassenApps Oct 27 '16 at 12:48
  • yeah, all 10 of us... – Confused Oct 27 '16 at 12:49
  • Quality not quantity I guess :) – claassenApps Oct 27 '16 at 12:50
  • I'm definitely not of any quality, unfortunately. I'm just vacuuming up as much knowledge as I can. There's more holes in my knowledge than a pasta colander or fishing net. – Confused Oct 27 '16 at 12:55
  • 1
    @claassenApps, good thing I looked at this. Are your bodies all relative to each node? you need to make sure you make the bodies relative to the parent, not the individual nodes (So if a node is at 10,10. the body of the node is 0,0. You need to move the body to 10,10) – Knight0fDragon Oct 27 '16 at 17:19
  • @KnightOfDragon, thanks for the post. I'll definitely take a look at this tonight. This sure seems like the problem. I'll post back tonight with an update. – claassenApps Oct 27 '16 at 17:35
  • 1
    @KnightOfDragon, that worked for the positioning of the nodes! Now I'm stuck with the rotation of the nodes. They're all stuck horizontally. Some should be at a 45 degree angle or a 90 degree angle. I can't assign a zPosition to the physics body and the node the physics body represents is rotated at the time of the creation of the physics body (also when I add it to the array). Do you have any suggestions for why that might be happening? – claassenApps Oct 27 '16 at 23:26
  • I have the exact same problem, have you figured out the problem for the rotations because I would really like to know. Thanks! – J.Treutlein Jan 09 '17 at 09:00
  • @J. Treutlein, unfortunately I never figured anything out. I was wanting to combine all my physics bodies into one to try and reduce the CPU usage of keeping track of all the physics bodies, but I ran some tests and I found that it didn't actually help the CPU any. I don't think this actually returns a single physics body like the description apple gives, but just keeps track of all of the separate ones and treats it as a single body. You may be able to store all the physics bodies in an array and do something similar. – claassenApps Jan 09 '17 at 20:18
  • Thanks so much for that! – J.Treutlein Jan 11 '17 at 12:00

0 Answers0