0

Question

How to achieve an intersection of two sprites, when one is a child of self and the other is a child of a sprite?

As their positions are completely different relevant to the comparisons between each other?

Example; this is ran before each frame to determine whether they intersect

-(void)checkInFOVWithPlayer:(Player *)player andEnemy:(Player *)enemy {
    SKNode *node = [player childNodeWithName:player.playersFOVName];

    if (CGRectIntersectsRect(node.frame, enemy.frame)) {
//        [self playerAimAtEnemy:enemy withPlayer:player];

        NSLog(@"inframe");
    } else {
        NSLog(@" ");
    }
}

However, node is a child of player and enemy is a child of self. So how can you check if they intersect?

Here's where they're initialised

float radianAngle = ((fovAngle) / 180.0 * M_PI);

float fovOpposite = atanf(radianAngle) * fovDistance;

SKShapeNode *fov = [SKShapeNode node];
UIBezierPath *fovPath = [[UIBezierPath alloc] init];
[fovPath moveToPoint:CGPointMake(0, 0)];
[fovPath addLineToPoint:CGPointMake(fovOpposite *-1, fovDistance)];
[fovPath addLineToPoint:CGPointMake(fovOpposite, fovDistance)];
[fovPath addLineToPoint:CGPointMake(0, 0)];
fov.path = fovPath.CGPath;
fov.lineWidth = 1.0;
fov.strokeColor = [UIColor clearColor];
fov.antialiased = NO;
fov.fillColor = [UIColor greenColor];
fov.alpha = 0.2;
fov.name = @"playerFOV";
[_playerImage addChild:fov];

and enemy

NSString *bundle = [[NSBundle mainBundle] pathForResource:@"enemyImage" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
SKTexture *texture = [SKTexture textureWithImage:image];
_enemy = [Player spriteNodeWithTexture:texture];
_enemy.position = CGPointMake(100, 100);
_enemy.size = CGSizeMake(20, 20);
_enemy.playerAimAngle = [self returnRandomNumberBetween:0 to:360];
_enemy.anchorPoint = CGPointMake(0.5, 0.5);
_enemy.playerHealth = 100;
_enemy.playerIsDead = false;
[self addChild:_enemy];
Daniel
  • 285
  • 3
  • 12
  • 2
    Convert both frames to a common coordinate system such as the screen. – rmaddy Sep 12 '15 at 19:16
  • @rmaddy How would I approach this? – Daniel Sep 12 '15 at 20:40
  • @rmaddy `CGPoint positionInScene = [node.scene convertPoint:enemy.position toNode:node.parent];` Trying to play with this, still cannot get any results. – Daniel Sep 12 '15 at 21:03
  • You have to convert both frames to the same node. – rmaddy Sep 13 '15 at 02:37
  • @rmaddy It's not really possible for me to actually put them in the same node, as the pathfinding for the enemies relies on different nodes, and the FOV is a child of the player so it's not really possible. Is there no workaround? – Daniel Sep 14 '15 at 11:34
  • You don't need to put the nodes in a common parent. You just need to convert the frames to a common parent. – rmaddy Sep 14 '15 at 13:30
  • @rmaddy The problem I'm having is that I can create a common "position" however, I cannot create a common "frame" so for example, I created a duplicate node of the FOV and ended up having a lot of problems with rotation etc, The only way I can feel to do it, is converting the position of the enemy and making it relative to the FOV, if that doesn't work. I'm pretty stumped. is `convertPoint:toNode` the right approach to this? – Daniel Sep 14 '15 at 13:35

1 Answers1

0
-(void)checkInFOVWithPlayer:(Player *)player andEnemy:(Player *)enemy {
    SKNode *fovNode = [player childNodeWithName:player.playersFOVName];

    SKNode *node = [self childNodeWithName:@"enemy"];

    CGPoint newPosition = [self convertPoint:node.position toNode:fovNode.parent];

    if (CGRectContainsPoint(fovNode.frame, newPosition)) {
        [self playerAimAtEnemy:enemy withPlayer:player];
    }
}

This has ended up being my solution to the problem, Now however, I must find a way to change the frame of the node so that it is not rectangular.

Daniel
  • 285
  • 3
  • 12