cocos2d FAQ addition: Sprite Bounding Rectangle

On May 22, 2010, in cocos2d, by Steffen Itterheim

I’ve added two short lessons to the cocos2d for iPhone FAQ about getting a sprite’s bounding box (with code) and testing rotated rectangles for intersection (collision).

cocos2d doesn’t have a method to get the bounding box, and using the [sprite contentSize] to create a rectangle is flawed because it will stop working as soon as the sprite is either scaled or its anchorPoint is not at the center (0.5f, 0.5f). It’s easy to fix that and it’s even easier to write an Objective-C category so that you can call [sprite getBoundingRect] as if it were a regular CCSprite method provided by cocos2d. Of course i know that as soon as cocos2d developers read this the question about testing for rotated rectangle intersection will come up. I linked to the “proper” solution but honestly, i believe the more pragmatic approaches that i’ve added will actually work better in most cases.