0

I add a couple of nodes, like this:

SKShapeNode* node= [SKShapeNode shapeNodeWithRectOfSize:rect.size];  // always 32x32
node.fillColor= [UIColor darkGrayColor];
node.strokeColor= [UIColor lightGrayColor];
node.position= position;
node.physicsBody= [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(rect.size.width - 2.0, rect.size.height - 2.0)];
node.physicsBody.categoryBitMask= catSomeNode;
node.physicsBody.contactTestBitMask= catSomeOtherNode;
node.physicsBody.dynamic= YES;
node.physicsBody.usesPreciseCollisionDetection= YES;
node.physicsBody.affectedByGravity= NO;
node.physicsBody.collisionBitMask= 0;

[self addChild:node];

NSLog(@"Adding node at position %@", NSStringFromCGPoint(node.position));

e.g. for the first shape:

2015-11-14 16:17:25.162 MyApp[13479:632495] Adding node at position {16, 16}

Later, I will try to animate the shape when it is touched. To determine the direction of the animation, I need to find out the position of the node.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch* touch = [touches anyObject];
   CGPoint location = [touch locationInNode:self];

   SKNode* node = [self nodeAtPoint:location];

   if (!(node.physicsBody.categoryBitMask & catSomeNode))
      return;

   if ([node isKindOfClass:[SKShapeNode class]])
   {
      // Here, the position is different
   }
}

The changed position according to the debugger is:

(lldb) p position
(CGPoint) $0 = (x = 15.999996185302734, y = 15.999996185302734)

This will mess up my logic, because i cannot easily compare positions.

2 questions:

1) Positioning

Is it correct, that positioning is always based on the node center, unlike e.g. UIView? This means essentially when I want to place a node at 0/0, I will have to set the position to width/2 / height/2, right? This feels a bit cumbersome, but well, I can work with that.

2) Touch detection

As you can see in my example, the position of the brick was changed. Why is that? After [self addChild:node]; the position will remain at 16/16, but after the touch detection it is changed to 15.999996185302734 / 15.999996185302734.

Whats wrong? I even saved the pointer of the node as class member, to check the position (in case nodeAtPoint returns a different object). But still the position is messed up.

[edit] Did some more tests. I saved the pointer to the SKShapeNode added above in my class, and I put a debug message in the scene's update: method, which prints the position of the node. This is what happens:

2015-11-14 18:39:07.560 MyApp[14414:696141] Node position {16, 16}
2015-11-14 18:39:07.701 MyApp[14414:696141] Node position {15.999999046325684, 15.999999046325684}
2015-11-14 18:39:07.866 MyApp[14414:696141] Node position {15.999998092651367, 15.999998092651367}
2015-11-14 18:39:07.951 MyApp[14414:696141] Node position {15.999997138977051, 15.999997138977051}
2015-11-14 18:39:08.019 MyApp[14414:696141] Node position {15.999996185302734, 15.999996185302734}

There is no code executed between the first update: and the second one. Also I did not tap, so no touch detection was done.

This leads me to believe I have some sort of initialization issue, so the scene is somewhat changed as soon as the app is loaded, or the scene becomes visible. I thought I already have this covered, because the node is added in the - (void)didMoveToView:(SKView *)view method.

user826955
  • 3,137
  • 2
  • 30
  • 71
  • For the issue with comparing the points, look [here](https://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison). – Chris Loonam Nov 14 '15 at 15:36
  • I'm not sure how this helps, as I'm not doing any math nor comparison here. Instead, the position value of one and the same object changes without my notice. I expect that when it is set to 16/16, it **remains** 16/16. – user826955 Nov 14 '15 at 16:08
  • @user826955 With your code I can't reproduce what you are saying. Also I can't say I haven't seen this before, but I just can't recall correctly what was the cause. Are you doing something else or just adding a SKShapeNode to the scene and grabbing it by touch location (is there any physics related code)? – Whirlwind Nov 14 '15 at 17:45
  • While trying to assemble a minimal example, I found out that exactly this line is causing it: `node.physicsBody= [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(rect.size.width - 2.0, rect.size.height - 2.0)];` (it is part of the code above) As soon as I remove the physics body, the position will remain at 16/16. This is especially weird because the physicsbody is added during shape creation, and not some time after. – user826955 Nov 14 '15 at 18:13

0 Answers0