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 1 year, 10 months ago.
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])
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.
You must be logged in to reply to this topic.
Social Widgets powered by AB-WebLog.com.