15,882,209 members
See more:
I am a beginner in cocos2dx ios game development.I have 12 sprites in a CCArray and they are falling from top to bottom of the screen randomly. Any sprite can appear any time. I have done layering that the sprite at array index 0 will come at layer 12 and at index 1 will come at layer 11 and so on.So that the sprite coming first appear ahead of the sprite coming second. But the point is this situation is not always true.The sprites can come like index 5 first and then index 4 and so on. So how to detect collision of the sprites present in the same array such that if they touch each others bounding box then I can make them slow for a second or something else so that they don't overlap with each other.I am not using Box2d and don't want to use it. I just want answer without using box2d and only in cocos2dx using c++.Anyone who can help me Thanks
Posted
enhzflep 23-Jan-14 2:58am
As long as rotation isn't involved and the boxes are the same size, the simple (but nasty?) solution would be to test the corner of each bounding box to see if it lies within any of the other bounding boxes.
Things would get unpleasant fairly quickly if you had rotation or different sized boxes, since you could then have an intersection between two bounding boxes, yet have zero corner vertices contained within the intersecting box.
E.g
(a) 45° rotated box could intersect just a corner of a box
(a) a 45° rotated box could have the same centre point as a non-rotated one, yet none of the vertices of either would be within the bounding box of the other.
(b) a rectangle could intersect with a narrower box.

A rather more complex strategy would involve seeing if any of the vectors formed between two bounding-box vertices intersected any of the other vectors between two vertices. Still, relatively straight-forward, I think - but an awful lot more computationally intense. It would also slow-down with a smaller number of bounding boxes to test, since the big O complexity is that much higher. In the case of just 2 boxes, you'd go from 4 points to test for same-size, no rotated boxes - right up to 16 vector intersections to look for - 4 sides of each box, needing to be tested again 4 sides of the other box.

## Solution 1

All you need to do is to define a rectangle around each sprite and then check if any of them intersect. look into CCRect class and for an example using this see here.