简体   繁体   中英

Cocos2d-android detecting bounding box of sprite

Is it possible to detect collisions with certain parts of a sprite's bounding box? For example if a sprite jumps and landed into a platform, the bottom part of the sprite bounding box's and the top part of the platform bounding box's collision will be detected.

In my projects I have a helper library that has lots of little functions including something like:

CCRect CHelperMethods::InsetRect(const CCRect rectToInset, const float insetValueX, const float insetValueY) {
return CCRectMake(rectToInset.origin.x + insetValueY, rectToInset.origin.y + insetValueY, rectToInset.size.width - insetValueX * 2, rectToInset.size.height - insetValueY * 2);
}

Then, for example with a sprite that has landed, it would have a State variable, and I would say (psuedo code)

if(sprite.getState() == constStateLanded) {
    CheckCollisionForBox( CHelperMethods::InsetRect(sprite->boundingBox(), yourXOffset, yourYOffset));
}

HTH, as you tagged Android and ipHone I didn't know whether you wanted Obj-C or C++ :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM