1-star challenge – accurate collision detection
The collision detection we implemented is a little inaccurate due to the yeti's texture. The CGRectIntersectsRect
class is working properly but the collision is being triggered as soon as the snowball enters into contact with the snow under the sledge. Now that you know how to calculate the distance between two points, try to detect the collisions when the distance between the snowball and the anchor point of the yeti is small enough.
The solution
To achieve this new collision detection, we just need to modify one line. Go to the update
method and modify the condition:
if (CGRectIntersectsRect(snowBall.boundingBox, _yeti.boundingBox) && !_collisionDetected) {
By the new one using ccpDistance
:
if(ccpDistance(snowBall.position, _yeti.position) <= _yeti.contentSize.width/2) {
As we want to know the distance between the snowball and the yeti, we check it in the for
loop and we just calculate the distance between their positions. I decided that if this distance is lower than half the yeti, it's close enough to be a collision.