3
\$\begingroup\$

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?

\$\endgroup\$
  • 2
    \$\begingroup\$ 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? \$\endgroup\$ – Sergio Jul 16 '14 at 16:47
  • \$\begingroup\$ @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. \$\endgroup\$ – user654987 Jul 16 '14 at 17:09
5
\$\begingroup\$

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).

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ This doesn't work when working with SpriteKit because one doesn't have direct access to collision handling. Rather, one sets up SKPhysicsBody nodes and handles contact behavior from there. As such, the tip will never be "inside" the ball from the SpriteKit developer's perspective. One could still handle the problem without the creation of an extra node, but it involves using the contact handler, determining if the tip of the spike is at the point of contact in that handler. Much simpler, especially from OP's point of view, to create a third node and handle efficiency later, if at all. \$\endgroup\$ – Attackfarm Jan 3 '16 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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