How to create custom collision events on Sprites without physics? v3.12

Hello!

I’m wondering if there is any good tutorials on EventCustom.

The problem:
I’m moving boxes all around the screen, and when it hit the player I want to detect the collision on both sides, and gives back name or tag of collided object, without using Physics. Beacause using physics only for detecting collisions is unnecessery in this case i guess.

What i want to do is detect overlapping sprites, and when a box overlaping the player or vice-verse i want that the box know that it had been hit by player, and the player know that it had been hit by x indexed box for instance.

So is there any best practice on making an event system for this, and if so where can i find a good tutorial, because i look around and can’t find one.

I did check: http://www.cocos2d-x.org/wiki/EventDispatcher_Mechanism and others as well, but didn’t help me too much.

Thank you for your help! :slight_smile:

1 Like

Sprite class has bounding box, which cocos2d::Rect class. Rect has methods:
bool containsPoint(const Vec2& point) const;
bool intersectsRect(const Rect& rect) const;
You can use it’s methods for simple collisions detection. Better do it in your scene’s update(dloat d) method.

Thank you for your reply!

Actually I do not want to tell which elements will collide, or check it on every frame and not on game scene. I would like to get registered on a custom event I made with all my objects and i the event triggers the callback start to run. No matter of which object it is. But when triggering happens i would like to know which is the exact object that collided on the object itself and handle the event… i hope it is clear, sometimes i can be very complicated describer :slight_smile:

You need check it on every frame, there’s no other way*. Everything else of your choice. You can use update method of each sprite, or create custom Action that will check collisions. You can trigger callback directly, or create custom EventListener.

*if we talk about simple way without using third party libs like Box2D

“You can trigger callback directly, or create custom EventListener.” - yes that was the exact question, on how to make custom eventlistener :slight_smile:

actually i write it wrong, and you’re right with the update, i’m just lost in my toughts. for a faster soloution i made it with rect intersect and let the object know they collided. and it works, but i will try to look around and maybe I’ll find an EventCustom tutorial.

thx for your help anyway! I leave this stuff opened here if someone has an idea! :slight_smile:

Custom event very simple. You just create event with some name, set callback (and userdata if you need it), and then call event by name somewhere in your code.
Here simple tutorial http://www.cocos2d-x.org/wiki/EventDispatcher_Mechanism#Custom-Event

Are you looking for just simple distance check of an sprite ?

if( square(X1-X2 , Y1-Y2) <= 1) {
//Do somting
}