Are these useful to anyone?
Just thought I'd share, since it seems to be two really common tasks that are probably extensively used.
https://github.com/TTStu/SparrowTinkering/tree/master/SPDisplayObject%2BCentralize
The first, SPDisplayObject+Centralize, allows you to call a single method replacing:
mySprite.x = 100.0f;
mySprite.y = 100.0f;
mySprite.pivotX = mySprite.width / 2.0f;
mySprite.pivotY = mySprite.height / 2.0f;
with just:
[mySprite centralizeWithX:100.0f andY:100.0f];
Of course, to leave the x/y coordinates at the "default" (0.0f) value, just use:
[mySprite centralizeWithX:mySprite.x andY:mySprite.y];
And to centralize an object within another container (eg SPSprite) use:
[mySprite centralizeWithX:self.width/2.0f andY:self.height/2.0f];
https://github.com/TTStu/SparrowTinkering/tree/master/SPSprite%2BHitBounds
The second SPSprite+HitBounds addresses the issue where rectangular collision detection is needed, but the "corners" of the bounding box make the collision area too large, you want a smaller collision rectangle to allow images to get really close but "not quite touch" or to reduce white space getting caught up.
So you just make the actual collision rectangle smaller than the object itself to get much more realistic collisions.
To set it up just use:
[mySprite setHitQuadXScale:0.8f YScale:0.5f];
(That makes the collision rectangle 80% narrower and 50% shorter for example).
(edit: it makes it 20% narrower lol got my maths back to front 🙂 )
Then, when doing collision detection:
if ([[object1 bounds] intersectsRectangle:[mySprite hitBounds]]) {
// Kerpow!
}
Well, I hope someone finds it useful 🙂