3

This seems to be a major oversight by Apple, but since SKPhysicsJoints have their anchor points set in Scene coordinates, this makes doing any kind of scrolling game impossible.

To simulate a camera in SpriteKit you create a WorldNode which contains all of the gameplay elements, and then pan that around the scene. Unfortunately, doing this causes the Scene coordinates of every object in the game to change on every frame as you pan the world around. In turn, this breaks the joint anchor points, and things go berserk.

There isn't even a way to change the joint's anchor point, so I don't even have a way of just updating the coordinate every frame. It would seem that using SKPhysicsJoint in a scrolling game is not an option.

Does anyone know of a way around this?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
BGreenstone
  • 260
  • 1
  • 11
  • This should work, I can only assume your scrolling setup is not correct. Or maybe you're forgetting to convert coordinates to world space somewhere. Have you read the part about emulating a camera in the Sprite Kit programming guide? – CodeSmile Jul 09 '14 at 19:50
  • Yes, I'm doing my WorldNode just like they say in the docs. The problem is that as you move the WorldNode around to scroll the game, that changes the scene coordinates of *everything*, and therefore, breaks all SKPhysicsJoints since those are represented in scene coordinates. Scene coordinates mean nothing to a game using a World Node to scroll everything around. – BGreenstone Jul 09 '14 at 20:51
  • Pretty sure you aren't considering the conversion methods. I've certainly built physics enabled scroller using this scrolling technique. – CodeSmile Jul 09 '14 at 21:42

1 Answers1

3

Ok, I think I figured out what was going on, and I was totally incorrect in my original assumption. The reason my anchor points looked incorrect is because the [convertPoint toNode] call was returning me Scene coordinates that were incorrect. After several hours I realized it was off by exactly half the screen dimensions. My Scene has an anchorPoint of (0.5, 0.5), but this screws up the conversion values. So, if I simply offset the point by width/2, height/2 it's correct:

GPoint pt = CGPointMake(anchorWorldX, anchorWorldY);
pt = [gGameScene convertPoint:pt fromNode:gGameWorld]; // convert to scene coords, but it's WRONG

pt.x += scene.size.width * scene.anchorPoint.x;   // this properly adjusts the value to be correct
pt.y += scene.size.height * scene.anchorPoint.y;

SKPhysicsJointPin* pin =[SKPhysicsJointPin jointWithBodyA:hinge.physicsBody bodyB:door.physicsBody anchor:pt];
BGreenstone
  • 260
  • 1
  • 11
  • I need to spend a little more time to wrap my head around this solution and why it works, but thank you so much for posting this!! I spent a bunch of time trying to figure out why adding the scrolling solution from Apple broke the joints. – Scooter Aug 14 '14 at 02:07
  • I had exactly the same problem and figured out the same janky solution. My code didn't work when I tried on a different device, and then stopped working on the original device too. Makes no sense, but hopefully Apple fixes the convertFrom and convertTo methods soon. – James Paul Mason Jan 03 '15 at 04:00
  • This is a very big one indeed! The convertPoint:fromNode method is obviously faulty. An oversight from apple? – Tokuriku Jan 22 '15 at 04:27