Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit Physics Joints corrupt for moving scene

I've created a SpriteKit Scene with physics bodys. This is working quite well. I'm also scrolling the scene around, using the Apple best practice with a "camera node".

My scenes node tree is like that:

Scene
|- World
   |- Camera node
   |- Game nodes with physics bodys

My Problem is, during the game I create new nodes with new joints (mostly fixed joints) dynamically. At that point, the world node has a different position than in the beginning (eg.: at start (0,0), during the game (1000,500)..) Now, the anchorpoint for the fixed joints is not at the position I want it to, it's somewhere in the scene.

I assume, the coordinate system of the scene is different to the coordinate system of the physics engine.

For the anchor Point, I use the following conversion:

[world.scene convertPoint:node.position fromNode:node.parent];

I want to set the anchorpoint at the position of "node". After moving the world node, the calculation of the anchorpoint is not working anymore.

Thanks for your help!

Greetz - Nekro

Update:

Code to center scene on the Player

- (void)centerOnNode:(SKNode*)node {
    CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
    node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x, node.parent.position.y - cameraPositionInScene.y);
}
like image 241
Nuker Avatar asked Jan 27 '16 08:01

Nuker


1 Answers

There are two requirements to using joints with SpriteKit physics:

  1. Define the joints in world (scene) space.
  2. Don't move the world.

From your question and comments, it sounds like you've got #1 right: When the joint is between nodes that aren't direct children of the scene, you need to convert coordinates to scene space before using them to define a joint.

However, SpriteKit physics doesn't handle it well when you try to move the root coordinate system. So, implementing a "camera" by moving the world causes trouble for the joints. Instead:

  • If you can afford to target only iOS 9.0 / OS X 10.11 / tvOS 9.0 and up, use SKCameraNode instead of moving the world. It's easier, too!

    (If you're wondering about whether you can support only iOS 9, check the adoption rate statistics, and compare to your development schedule... if you won't be shipping for awhile, iOS 9 might be the "-1" OS version by the time you're done. Remember also there's a high correlation between enthusiastic customers and early adopters.)

  • If you're supporting iOS 8 / OS X 10.10 still, try adding an extra layer to your scene: fake a camera by moving a node that is a child of your scene and the parent of all your game content.

like image 143
rickster Avatar answered Sep 24 '22 12:09

rickster