What's the best way to get the bounding box of a sprite?

Forums Programming cocos2d support (graphics engine) What's the best way to get the bounding box of a sprite?

This topic contains 4 replies, has 2 voices, and was last updated by  karnak 3 years, 7 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
Author Posts
Author Posts
September 16, 2010 at 11:02 pm #224813

karnak
@karnak

Hello,

Does Cocos offer a native bounding box solution?

What I’m doing is something like this:

CGFloat x = player.position.x;
CGFloat y = player.position.y;
int w = player.contentSize.width;
int h = player.contentSize.height;
static float vel = 300.0;
float hb, wb;

if (vel > 0) {
hb = y + h/2;
} else {
hb = y - h/2;
}

Where hb and wb are the width and height of the bouding box, depending if the player is going up (to check if it will hit a top platform) or down (to hit against a bottom platform).

Is that the right approach?

Thanks

September 16, 2010 at 11:28 pm #298121

manucorporat
Participant
@manucorporat

Does Cocos offer a native bounding box solution?

yes.

CGRect boundingBox = [player boundingBox];

September 16, 2010 at 11:41 pm #298122

karnak
@karnak

@manucorporat: Amazing, thanks.

Edited: Just noticed that the bounding box will be correct only if the sprite has an anchorPoint of 0,0, otherwise it is outside the body.

September 17, 2010 at 12:24 am #298123

manucorporat
Participant
@manucorporat

I developed this method and it supports any anchor point and rotation.

You can enable in ccConfig.h

#define CC_SPRITE_DEBUG_DRAW 1
#define CC_SPRITEBATCHNODE_DEBUG_DRAW 1

to debug bounding box collisions.

September 17, 2010 at 2:45 am #298124

karnak
@karnak

@manucorporat: got it working now, thanks for the tips, the DEBUG_DRAW was essential.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.