Collision Detection

I am making a game in which multiple character fire towards each other and on hitting the fire they will act with different animations like: blast, bounce, blur and so on. For now the thing where i am stuck is that while fire is traveling how will we check that its intersecting with other objects of not?

There are two immediate approaches to this.

  1. Use a Physics library.
    Box2d and Chipmunk come bundled with cocos2d-x and can be used to perform collision detection and collision response if required.
    http://www.cocos2d-x.org/programmersguide/12/index.html

  2. Write your own
    AABB and Circle collision is pretty simple to implement yourself.
    https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection

Further reading:

1 Like

Thank you firstly but actually i am somewhere lacking in implementing the bodies as i have very occupied screen not the one like you send in the ref. Have you played gemoetric wars ios Game ?

It doesn’t matter whether you are looking at one or one thousand - you still have to check for collisions in the same way!

Yeah, thats same i am saying but when a player aim it can be hit the different objects in its path so how to check those elements its under water sea game.

I’m sorry I do not understand what you are asking here.
If you want to check if your player has collided with some other thing, then you need to do one of the things aimax27 has suggested.

If writing your own then you need to iterate over all the entities, and check if they are colliding with any of the entities they can collide with.

Pseudo code

for each entity {
  if (entity.left > me.right) break;  
  if (entity.right < me.left) break;
  if (entity.bottom > me.top) break;
  if (entity.top < me.bottom) break;
  // the entity rectangle overlaps my rectangle
}

If your entities can collide with one another, then you need to do the same for every entity

At the point in the comment you can do ‘better’ collision detection if necessary or just accept they have collided.

It is easier to use a physics library, as that will do the ahrd work for you, at the possible expense of performance / excutable size.

1 Like

It’s also worth noting that physics engines implement various broadphase, sorting and partitioning mechanisms to minimise the amount of tests actually done. So if you’re needing to test a large amount of objects I strongly recommend taking the physics library approach and not reinventing the wheel.