How to use collisions and contacts?

Hi, everybody!

What difference between collisions and contacts?
And if I have a lot of monsters, which shouldn’t interact and a hero, which should interact with monsters - what the best way to do it?

I use cocos2d-x v3.2 and build-in physical engine.

Thanks in advance

Collision are happening, when two bodies/shapes touch/overlap. It’s the event itself.
A collision can have multiple contact points. The points, where bodies/shapes are touching/overlapping each other.

You would need collision masks to filter different collisions: monster/hero, hero/monster, monster/monster.

Are the monsters dynamic bodies or static bodes?
The monsters should not interact with what? The hero? Monsters? Environment?

If you set your Hero’s category to 1
And you set your monsters’ category to 2

This tells the physics engine the two types are different.

now set the mask of the Hero to 3 - (in binary that’s 11) which tells it to collide with anything that is a 1 or a 2 (you might want to make it 2 so he only collides with monsters)

Set the mask of the monsters to 1 - which mean they only collide with the Hero.

now heros collide with monsters and monsters collide with hereos

I normally handle the collisions from the Monster point of view - that is I handle the collision events in the Monster object - and ‘tell’ the player he’s dead, rather than the other way around - but that’s just personal preference.

Setting to 3 would make no sense, if there is only one hero, as the hero can never collide with itself.

The monsters are dynamic bodies.

The monsters should not interact with each other.

I thought that I do something wrong, but I read your comments and understood that did as you wrote.
I had a problem with FPS. When a lot of monsters (more than 50) pursued the hero and overlap each other FPS was falling (see attached image). I solved a problem - I changed phisics body from Box to Circle, and now FPS keeps steadily with more than 150 monsters.
But this solution may be not the best…

Just use the filtering method based on a bit mask described by @Maxxx above.
Chipmunk supports filtering by groups and layers.
http://chipmunk-physics.net/release/ChipmunkLatest-Docs/#cpShape-Filtering

Circles collisions are a little faster to calculate than boxes, but that depends on the algorithms used. Chipmunk implements different algorithms than box2d. It also does not support sleeping like box2d does. Chipmunk only supports sleeping thresholds, wile box2d supports explicit sleeping of bodies.

The performance of Chipmunk also depends on tuning the engine for your purpose.
You may switch from the bounding box tree to the spatial hash and see, if you get more performance:
http://chipmunk-physics.net/release/ChipmunkLatest-Docs/#cpSpace-SpatialHash

@iQD, thanks for your advice) I will try to modify all masks correctly and tune engine.

Sure - but is there only one hero? http://en.wikipedia.org/wiki/List_of_Fantastic_Four_members

:smile:

You should read more “what if?” stories :wink:

Can I see your code to study?

The concept here isn’t hard. The art takes care of the perspective, physics bodies, raycast queries, collisions, etc.

Though quite outdated, see the youtube sonar systems cocos2d-x physics video on collisions, just modify it for V3, I did and it went fine


But I’m not proud of this code :joy: