Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
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.
(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.

1 solution

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.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900