collision detection doesnt work as expected

Forums Programming cocos2d support (graphics engine) collision detection doesnt work as expected

This topic contains 1 reply, has 2 voices, and was last updated by  ksjogo 2 years, 2 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
Author Posts
Author Posts
January 27, 2012 at 8:04 pm #238748

arbitur
Participant
@arbitur

im making a little more advanced snake game, and im making the worms body by adding CCSprites at the heads position eatch 0,02 sec.

that works fine but when i try to add collision detection between the first worms body and the second worms body it wont work.

it only works when the last “spawned” body parts are colliding.

im using CGRectIntersectsRect([body1 boundingBox], [body2 boundingBox])

January 27, 2012 at 9:01 pm #363629

ksjogo
Participant
@jogo

There are 2 reasons this does not work:

BoundingBox on the body is not updated to all the attached sprites.

If if it is this way, it would not work, because boundingBox returns a rect. Thus it would crash in “caves” where it should not.

2 simple solutions:

Loop through each sprite and check for intersects after movement, but this would be n^2, so could grow slow.

Faster is a two-dimensional BOOL array. You just store the occupied fields and when a snake moves you check if the target is already occupied.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.