How to check collision if only bottom half of the sprite matters (think of a cherry with a stem)

109 views Asked by At

I am trying to put together a game using SpriteKit, in Swift.

I have a moving sprite which is a rectangle of width (sprite.frame.size.width) and height 2*(sprite.frame.size.width)

I only want to check collision of the bottom half which is a square of width (sprite.frame.size.width) and height (sprite.frame.size.width)

I set sprite.anchorPoint = CGPointMake(0.5, 0.25) and use method CGRectIntersectsRect to check for collision with another sprite. But this does not work. The collision area remains as the first rectangle.

I do not want to use methods that call for physicsBody because there is no other physics in the game.

What am I missing here?

1

There are 1 answers

0
abacaba On BEST ANSWER

I actually found this UIEdgeInsets function which can be used to shrink a frame. If spriteB is the sprite in question, then instead of testing like this:

if CGRectIntersectsRect(spriteA.frame, spriteB.frame) { // something happens }

just inset the edges to the bottom half of spriteB, like this:

if CGRectIntersectsRect(spriteA.frame, UIEdgeInsetsInsetRect(spriteB.frame, UIEdgeInsetsMake(spriteB.frame.height/2 , 0 , 0 , 0))) { // something happens }