1

I've spent too long on this. Someone has solved this already, please help.

Enemy fires on my ship. SKPhysicsContact takes over and I get contactPoint. I explode the fired missile at the contactPoint, in world coordinates. All good. Now I would like to use the point, not in the scene coordinates returned by contactPoint, but in my ship coordinates to start emitting smoke from the ship, where I've been hit. I've used convertPoint function before, with success, but I can't seem to get it right here...Docs say the contactPoint is expressed in Scene coordinates, while myShip has its coordinates, living as a child of a world node, which is a child of the scene node. They are in the same Node hierarchy. I think...I have a Scene->World->Camera, where myShip is a child of World. My code, I think, is saying convert contactPoint from Scene coordinates to coordinates in myShip. But this does not work. Nor does any other combination. What am I missing? I suspect the Camera hierarchy, but unsure. The numbers being returned into smoke.position are way out of whack...

- (SKEmitterNode *) newSmokeEmitter: (SKPhysicsContact *) contact
{
    NSString *smokePath = [[NSBundle mainBundle] pathForResource:@"ShipSmoke" ofType:@"sks"];
    SKEmitterNode *smoke = [NSKeyedUnarchiver unarchiveObjectWithFile:smokePath];

    smoke.targetNode = myShip;
    smoke.name = @"shipSmoke";
    smoke.position = [self convertPoint:contact.contactPoint toNode:myShip];

    //my temporary kludge that places the smoke on the ship, randomly

    //smoke.position = CGPointMake(skRand(-25,25), skRand(-25,+25));

    NSLog(@"Ship at world pos: %f,%f", myShip.position.x, myShip.position.y);
    NSLog(@"Contact at scene pos: %f,%f", contact.contactPoint.x, contact.contactPoint.y);
    NSLog(@"Smoke position at ship pos: %f,%f", smoke.position.x, smoke.position.y);
    [myShip addChild:smoke];
    return smoke;
}
julius patta
  • 78
  • 1
  • 8

2 Answers2

2

If you haven't solved it already, the answer is to convert from the node's scene:

smoke.position = [self convertPoint:contact.contactPoint fromNode:self.scene];

Or, in Swift:

smoke.position = convertPoint(contact.contactPoint, fromNode: scene)
SwampThingTom
  • 891
  • 1
  • 6
  • 15
1

If I understand your node hierarchy correctly this could fix the issue.

CGPoint contact = [self convertPoint:contact.contactPoint toNode:self.world];
smoke.position = [self.world convertPoint:contact toNode:myShip];

I don't think you can convert the point directly to ship. You have to convert it down the hierarchy until you get to the node you want.

Hopefully that helps.

Edit

To verify that the actual contact point is on the scene where I would expect you could try this.

SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(50, 50)];
sprite.position = contact.contactPoint;
[self addChild:sprite];

This should add a sprite exactly where the contact point is if the point returned is in the scene coordinate system. If it it does show up at the right spot than it truly does come down to converting points correctly.

Skyler Lauren
  • 3,792
  • 3
  • 18
  • 30
  • I get where you were going, thank you. No luck. With small tweek to code (my 'self' is the scene), I tried: CGPoint contactW = [self convertPoint:contact.contactPoint toNode:world]; smoke.position = [world convertPoint:contactW toNode:myShip]; – julius patta Jun 11 '15 at 19:16
  • @juliuspatta I updated my answer with a debugging tip. If you get a chance try that out and see if a green square shows up in the right position. – Skyler Lauren Jun 11 '15 at 19:27
  • Hi: thanks again for the tip. It indeed is just a matter of the right conversion. I use the contactPoint for other needs, and all is working as it should. – julius patta Jun 12 '15 at 19:10
  • My problem with conversion to myShip coordinates started after I introduced (re-factored my code into) the scene->world->camera hierarchy of nodes. Anything I perform with the world hierarchy is fine. Whenever I use system generated coordinates (such as contactPoint from SKphysics) I can no longer find the right conversion path to get to myShip coordinates (a child of world). My game is fully functional with a lot of activity, interaction and sprites... – julius patta Jun 12 '15 at 19:10
  • I just can't make the smoke come out based on the actual system recognized physical contact -- so for now I kludge it :) (see my comment) while it's close enough, it ain't elegant nor proper... – julius patta Jun 12 '15 at 19:10