1

I have two SKNode with physics (one under another):

- (SKNode*) newNode {
    SKShapeNode *node = [SKShapeNode shapeNodeWithCircleOfRadius:60];
    node.strokeColor = [SKColor clearColor];
    node.fillColor = [SKColor greenColor];

    node.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:60];
    node.physicsBody.categoryBitMask = 1;
    node.physicsBody.collisionBitMask = 1;
    node.physicsBody.contactTestBitMask = 1;
    return node;
}

- (void)createSceneContents {
    SKNode *n1 = [self newNode];
    n1.position = CGPointMake(100,100);
    [self addChild:n1];

    SKNode *n2 = [self newNode];
    n2.position = CGPointMake(100,300);
    [self addChild:n2];
    ...
}

Their goal is to reach a predetermined position directly opposite them at some distance.

To achieve this, I use SKAction (this is important and can not be changed):

[n1 runAction:[SKAction moveTo:CGPointMake(800,100) duration:3]];
[n2 runAction:[SKAction moveTo:CGPointMake(800,300) duration:3]];

In addition, on the first node's movement path I place a third node - exactly the same.

 - (void)createSceneContents {
    ...
    SKNode *obstacle = [self newNode];
    obstacle = CGPointMake(500,300);
    [self addChild:obstacle];
 }

The third node must be shifted in the direction of first node's movement by the mechanism of physics.

Click to illustration of the Scene (the red circles marks destination position of nodes)

Then I run the app, everything goes as is conceived, except for one thing - a first node (with obstacle on the path) was not reach the target location, just a few pixels:

Click to see result

The question is simple: why? And how can I fix it? Thank you!

  • Why do you move nodes by actions if they are meant to respond to collisions? You cant mix those two. Why? Because you affect on sprite's position in two different ways and effects of skaction and a physics engine interfere, so you can end up with weird results. – Whirlwind Jan 17 '17 at 17:15
  • Of course contact detection will work, and you can mix actions that change node's position with physics, but if your nodes collide then you will likely have a mess. – Whirlwind Jan 17 '17 at 17:24
  • @Whirlwind - Yes, i need this mixture to collision detection, and then for more accurate moving the third node (obstacle). Yes i can move obstacle node by SKAction too, but this is really difficult to animate it without node overlapping. The first move i must making by action (not by physics), because global game logic require it (game itself is not physics based) – Maxim Krylov Jan 17 '17 at 19:07
  • I think you mixing terms here. When you say collision detection that probably refers to contact detection... which works without collision bit masks set. But you have set collision bit masks, so I had impression that your nodes collides with orher bodies , while you trying to move them using actions which would certainly mess up the node positioning. – Whirlwind Jan 17 '17 at 19:42
  • thank You @Whirlwind ! Yes i try to achieve two goals 1) detect contact - to run related game logic 2) use collisions mechanism to make pretty animation while tow nodes moving with out overlapping – Maxim Krylov Jan 18 '17 at 12:30
  • @Whirlwind that would describe my problem more clearly - just imagine the chess board with positioned chips. When one chip is moved to the position of the other, the second has to move over to the cell back. To make movement strictly by the cells (at the end of each move chip is occupied precisely the cell center), I prefer to do use SKActiont because I can specify the exact movement coordinates. But the synchro movement of the two chips (to looks like the one pushes the other), it is more convenient to do with physics. From this and there was confusion. – Maxim Krylov Jan 18 '17 at 12:41

0 Answers0