2

In my game, I'm trying to determine what points to dole out depending on where an arrow hits a target. I've got the physics and collisions worked out and I've decided to draw several nested circular SKShapeNodes to represent the different rings of the target.

I'm just having issues working out the logic involved in checking if the contact point coordinates are in one of the circle nodes...

Is it even possible?

Astirian
  • 63
  • 1
  • 10
  • possible duplicate of http://stackoverflow.com/questions/481144/equation-for-testing-if-a-point-is-inside-a-circle (it's a very common collision detection algorithm so yes, it's certainly possible) – CodeSmile Jun 03 '14 at 11:44

2 Answers2

3

The easiest solution specific to Sprite Kit is to use the SKPhysicsWorld method bodyAtPoint:, assuming all of the SKShapeNode also have an appropriate SKPhysicsBody.

For example:

SKPhysicsBody* body = [self.scene.physicsWorld bodyAtPoint:CGPointMake(100, 200)];
if (body != nil)
{
    // your cat content here ...
}

If there could be overlapping bodies at the same point you can enumerate them with enumerateBodiesAtPoint:usingBlock:

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Thanks for the illuminating answer :). However after a lot of experimentation, I think I need to figure out distance along radius from circle center as SprikeKit's contact detection seems a bit off. Almost as if the SKShapeNode or SKPhysicsBody radius ends up bigger than what's defined in the code... – Astirian Jun 06 '14 at 07:47
  • Yes, shape threshold increases the actual size of a body, you can simple reduce the radius a bit to combat this effect. Either way, radius to radius (circle to circle or simply distance check) is really easy to implement and examples are aplenty. – CodeSmile Jun 06 '14 at 08:04
  • Oh dear, it appears my problem is worse than I originally thought... I've done some testing and the problem lies with SKPhysicsJointFixed. I'm using the contact coordinates between the arrow and the target nodes to create the joint anchor but the joints are getting created before they hit the target. I wonder if it's because I'm using a 640x400 resolution and scaling to fit? – Astirian Jun 06 '14 at 08:56
3

You can also compare the SKShapeNode's path with your CGPoint.

SKShapeNode node; // let there be your node
CGPoint point;   // let there be your point

if (CGPathContainsPoint(node.path, NULL, point, NO)) {
   // yepp, that point is inside of that shape
}
Laszlo
  • 2,803
  • 2
  • 28
  • 33