Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a spike and a ball is falling down, when the ball touches the top point of the spike, I want to make the ball invisible, and when it touches on the other part i.e on the sides I don't want to make it invisible.

enter image description here

I have two sprite nodes - Ball and spike.

How can I make the collision detect only on specific part i.e only on top of the spike in spritekit?

share|improve this question
2  
I'm not familiar with spritekit, but can't you just test collision with a point at the top of the spike, instead of the full spike? –  Sergio Jul 16 at 16:47
    
@Sergio. That is what i want to know, how to detect collision only on the point of the spike node. The spike is the complete node, so when i detect collision it happens with the complete spike. –  user654987 Jul 16 at 17:09

2 Answers 2

up vote 1 down vote accepted

Remember that SpriteKit's physics system is based on "SKPhysicsBody"s, which are added to "SKSpriteNode"s. Those physics bodies, however, needn't be attached to visible nodes.

The simplest method is to create a SpriteNode with no actual sprite or visible body, add it as a child to the area you want on the visible shape, and categorize it differently from the visible body.

In your specific case, create a tiny node, add it to the "Triangle", position it at the top, and treat it as a separate category with a different contact behavior for the "Circle". In that contact method, you define the unique contact behavior you desire (whether you want to lower the alpha, set the alpha to 0, remove the "Circle" from the scene, or any other form of "invisibility" appropriate to your situation).

share|improve this answer

I'm just learning SpriteKit myself, however I think the following would be faster:

- (BOOL)checkCollisionForSpikePoint:(GCPoint)spikePoint
                         ballCenter:(CGPoint)ballCenter
                         ballRadius:(CGFloat)ballRadius
{
    CGFloat hypotenuse = sqrt(exp(spikePoint.x-ballCenter.x,2) + exp(spikePoint.y-ballCenter.y,2));
    return hypotenuse < ballRadius;
}

Call this in your collision handler and pass it the relevant info (the point representing the tip of the spike, the point representing the center of the ball, and the radius of the ball). It will return a BOOL indicating if the tip of the spike rests inside the ball.

This is extremely fast (and admittedly untested) code and doesn't add any new nodes to the scene.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.