2

I have been fiddling with this topic for about three months now. I thought it was just a temporary bug Apple would fix soon and postponed it to the end of development. Now my game is almost finished and I still could not find a good workaround:

If I have a SKShapeNode and add a physicsbody to it, I cannot use the setPosition:(CGPoint) method to change the position anymore. My object gets always placed at (0,0) of its parent view.

I have tried setting the position inside the didSimulatePhysics method, to make sure all physic calculation were done.

I tried using SKAction to move the node and other suggestions from similar questions here. None worked as desired.

The only (kind of) working solution for me is to use this workaround method:

- (void)setPositionWorkaround:(CGPoint)p {
    CGVector vel = self.physicsBody.velocity;
    CGFloat angVel = self.physicsBody.angularVelocity;
    SKPhysicsBody *temp = self.physicsBody;
    self.physicsBody = nil;

    [self setPosition:p];
    [self setPhysicsBody:temp];
    [self.physicsBody setVelocity:vel];
    [self.physicsBody setAngularVelocity:angVel];
}

Strangely this method works only once. Every additional time I call this method the position is set to the first call parameter even though there is no data left from the initial call. I checked the parameters and the coordinate systems countless times.

Did someone run into the same problem and found a working solution?

Community
  • 1
  • 1
Git.Coach
  • 3,032
  • 2
  • 37
  • 54
  • This is not any apple bug that I am aware of, are you sure you have dynamics turned off or turn off affected by gravity? I wrote a quick sample app and it works fine – Knight0fDragon Feb 18 '16 at 16:03
  • 1
    Affected by gravity is off and I tried changing the dynamics flag on / off. Everything worked fine and after an iOS update this behaviour changed on iPhone 6,6+ and iPad. All after the same update. The very same update that messed up SKEmitterNode behaviour for many devs. – Git.Coach Feb 18 '16 at 16:11
  • I may need more info then, because I cannot reproduce your bug. I have a `SKShapeNode` that is a circle, I slap on a `SKPhysicsBody` that is a circle with the same radius, then I call set position on touch. Works fine – Knight0fDragon Feb 18 '16 at 16:13
  • I have a rather complicated setup going on inside of the node. I will dissect it and post my results. – Git.Coach Feb 18 '16 at 16:22
  • How big is your physics body? Maybe it is much bigger then the ShapeNode. This could cause a collision which prevents your movement. Maybe you can just add a breakpoint in didBeginContact to check – Stefan Feb 18 '16 at 20:02
  • The physicsbody has half the radius of the ball’s diameter. I removed everything from the ShapeNode and still receive the same behaviour. I will create an empty project and see if I can reproduce it as well. – Git.Coach Feb 18 '16 at 21:54

1 Answers1

1

After a long time fiddling with this problem I finally found the solution for it.

So, since a relatively recent update on SpriteKit (3-4 months back) I cannot move any SKNode subclass that has a SKPhysicsBody attached to it. setPosition:(CGPoint)position and [SKAction move…] both do not work.

In my case removing the SKPhysicsBody and setting the position did not work, because I was using a pool that recycled my SKShapeNodes for performance reasons. The pool did not remove the attached SKPhysicBodies though.

So: If you have this problem make sure ALL SKPhysicBody objects are removed from the node and childnodes.

Git.Coach
  • 3,032
  • 2
  • 37
  • 54
  • 1
    This worked, thanks! So frustrating they introduce bugs like this, though. – Crashalot Mar 28 '17 at 06:13
  • @Crashalot Yes, working with Spritekit has been pure pain... I published a blog post about it recently. Hope you are able to finish your project :) – Git.Coach Mar 28 '17 at 06:22
  • Indeed, such an unnecessary pain. Forcing a Unity app onto iOS, though, presents its own problems, though. – Crashalot Mar 28 '17 at 06:29
  • Since I'm currently experimenting with Unreal Engine and Unity I would be interested in hearing about the trouble you experience... Any blog you share it on? – Git.Coach Mar 28 '17 at 06:32
  • Sorry didn't mean to suggest we're using Unity ... just sharing experiences from other developers who used Unity and tried to force the game into an Apple app. – Crashalot Mar 28 '17 at 06:59