开发者

How to detect collision between three objects simultaneously in Box2D?

I am new to Objective-C. I am currently working on a game using Cocos2D and Box2D. My problem is that when 3 objects collide together, the game crashes. Now let me describe my game in details:

In my game I have a main character standing on top of a building. Below the building 开发者_运维技巧there's this the road. Enemies pass by the road at various random speeds entering the screen from right and exiting from the left. I have created the enemies as b2_kinematicBodies and set a random velocity for each of them using SetLinearVelocity(). The main character shoots the enemies. The projectile (the object being shot) is a b2_dynamicBody. When the projectile hits the enemies, both the projectile and the enemy are destroyed. During gameplay sometimes an enemy moving at a slow speed is crossed by one which is moving at a higher speed. If a projectile hits the two enemies just at the point when they are overlapping and one is about to pass the other one, the game crashes! Please help me with this.

I have detected collision using b2contactListener class.

One thing I didn't mention before is that I am not creating the enemies as individual distinct bodies. Instead, I am creating it once and making it move and I am calling this method (which creates the enemies and makes them move) inside init as below:

[self schedule:@selector(addRightTarget) interval:2.0];


I believe the issue is that the collisions are calculated before your handler gets any calls. Meaning that when your handler gets called, the bullet has hit 2 objects. So you get 2 call-backs as shown below.

Collision Detected: Bullet + Enemy1

  • Destroy Enemy1
  • Destroy Bullet

Collision Detected: Bullet + Enemy2

  • Destroy Enemy2
  • Destroy Bullet [CRAAAASH!!! You just tried to delete a non-existent object]

1st: You should not be removing anything except in your step function (as someone mentioned in another answer)

2nd: Pick one of these:

  • Make your list/array of objects-to-delete be a 'set' or implemented in such a way that duplicates are avoided.
  • Check for existence of your object in the world


The collision only happens between 2 objects in Box2D. So in your mentioned scenario your will get multiple collision events which could be,

Enemy-1 and Enemy-2 Enemy-1 and Bullet Enemy-2 and Bullet

So one possible reason of crash could be that you are not expecting (Enemy-1 and Enemy-2) collision and you are handling it like you have collision between (Enemy-1 and Bullet) so you might be casting it into wrong class. Make sure you are checking the kind of class "isKindOf" before casting it. Also you may want to use Contact Filtering and or assign category masks to your enemies so that they don't collide with each other and only collide with bullet.

But it will be more help full if you tell something about how and where you destroy your bodies (I hope its not inside your Collision Detection Functions) and also if you can share the exception text when your application crash, that will be helpful.


I used a rather cheap workaround. I alternately created enemy fixtures of different sizes(differing by few pixels). So now if i shoot them even when they overlap, the app doesn't crash(because only the bigger object collides and gets destroyed). This serves my purpose. Thanx a llot for your help. I really appreciate it! :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜