0

I am making one game where a character has some animations like run, jump etc.,

Is there any way to detect collision while it is animating? Because it is changing its shape while in idle state, running, and jumping state.

While I searched for solution I founded these two Sprite Helper

Physics Editor

Kumar C
  • 525
  • 6
  • 21

1 Answers1

0

Best way is to define your own bounding box for the sprite and decide a specific size to test it against other shapes in your level.

Otherwise you can use the property sprite.boundingBox which will return the CGRect of the actual sprite but I think this is related to the current transformation stack according to the CCNode tree. It works in many situations but not if sizes according to animation phases change so much.

So, choose a specific bounding box:

CGSize playerBounds = CGSizeMake(20,20);
CGRect bound = CGRectMake(player.position.x, player.position.y, playerBounds.width, playerBounds.height);
// or CGRect bound = player.boundingBox

test it against your level:

for (CCSprite *levelPiece in pieces.children) {
  if (CGRectIntersectsRect(bound, levelPiece.boundingBox)) {
    // they're colliding
  }
}
Jack
  • 131,802
  • 30
  • 241
  • 343
  • If I use bounding boxes I can detect any collision. But I want to detect collision in box2d shapes. I have 10 frames for running animations. Each frame has different shape with legs body angle, hands at different angles. I have defined bodies for all frames and changing accordingly. But this not an efficient idea. So I need an optimized way of determining collision in box2d accurately or apprpximately – Kumar C Oct 19 '13 at 05:32
  • Just use a coarse collision phase in which you test big bounding boxes to see if there are possible collision and then check them with a fine collision detection phase which will be more expensive but will be done on less data. – Jack Oct 19 '13 at 17:27