About sensors and collision listener (add and remove)

Hi friends!

About sensors. I have a sensor shape that collides with a static body, my question is:
I receive ADD event, but inmediately, but while my senser is still "inside" the static body, I receive REMOVE event. Is that correct? Am I doing something wrong? I thought I had to receive REMOVE when both shapes became not overlapped. Is that correct?

Thanks for help and your time.
Right now add/remove are for contact points, not overlap status.
Thanks for reply.

Therefore, How should I manage an enemy that automatically walks until collides to turn around (very basic AI)? Using sensors or contact points?
What about the problem of jumping through platforms? My idea was to put a small sensor an my character base, and if not overlapped (and not falling), deactivate collision,

I'm disconcerted. Where should I use sensors? In which situations?

You said "Right now add/remove are for contact points, not overlap status." How can I know info about overlapped status?

Thanks a lot, you are very helpful for me. Nice lib 😉
hey, i'd love to know the solution if you found one. I have the same problem.
Any idea?

BTW, To implement jump I have to know if my character is over ( actually colliding ) with the floor, should I use "Persist" in my contact listener? Or what do you use?

Thanks a lot
I'm still exploring the lib(so don't take this for absolute truth 😉), but from what I understand, you could use TestPoint to check if your shape is overlapping another shape. Take the center of your sensor and test against the shape you just received a remove event. If it returns true, then your sensor is fully overlapping the other shape. Else, it is outside the shape.
Tiblanc wrote:I'm still exploring the lib(so don't take this for absolute truth 😉), but from what I understand, you could use TestPoint to check if your shape is overlapping another shape. Take the center of your sensor and test against the shape you just received a remove event. If it returns true, then your sensor is fully overlapping the other shape. Else, it is outside the shape.


I guess (not sure) that is not the best way, I believe we should use Contact Listener, but I don't know how.
Thanks for help.
ricardo_ruiz_lopez wrote:Thanks for reply.

Therefore, How should I manage an enemy that automatically walks until collides to turn around (very basic AI)? Using sensors or contact points?
What about the problem of jumping through platforms? My idea was to put a small sensor an my character base, and if not overlapped (and not falling), deactivate collision,

I'm disconcerted. Where should I use sensors? In which situations?

You said "Right now add/remove are for contact points, not overlap status." How can I know info about overlapped status?

Thanks a lot, you are very helpful for me. Nice lib 😉


I'm by no means an expert, so there are probably better ways to go about this. Would be nice if someone who knew wrote up some better examples of custom behavior outside of the really small filter example in the user guide. Here are the only wiki articles on this topic (that I saw):
http://www.box2d.org/wiki/index.php?title=Buffering_ContactPoints
http://www.box2d.org/wiki/index.php?title=Listening_to_contact_events_in_Box2D_AS3

Sensors are useful if you don't want to calculate the angle of collision yourself, so you know the contact came from the right, left, etc (work around without sensors is to calculate angle of collision yourself with the real shape), or to provide some buffer, so you know a collision with the real shape is coming up or is close enough to count, say for grabbing.

Whether you use them or not, you are still dependent upon the contact listener. For reactions (i.e. hitting a wall), I would think you should be able to do that on the contact add. For status type events (i.e. am I standing on the ground so I can jump), I keep a counter that I increment on contact adds and decrement on removes. So a status check is just checking whether my count is greater than 0 or not. Downside is I have to keep a counter for everything I want a status on. And for some reason the count do get flaky over time on some occasions.

As for canceling collisions, there doesn't seem to be an "official" way to do that yourself outside of the built-in contact filtering using the flags. Here is a non-offical way to do it (see bottom): http://www.box2d.org/forum/viewtopic.php?f=3&t=1216&hilit=cancel+collision

I hope that makes sense.
Thanks Edge, Erin and everryone for help.

Edge wrote:Sensors are useful if you don't want to calculate the angle of collision yourself, so you know the contact came from the right, left, etc (work around without sensors is to calculate angle of collision yourself with the real shape), or to provide some buffer, so you know a collision with the real shape is coming up or is close enough to count, say for grabbing.


I'm trying to do it using sensors, following your suggestions. But I have some problems. Erin tells sensors only generate ADD and REMOVE, but I have a strange result. I have a character with a sensor, I move it close a wall, if sensors intersects with the wall, I see an ADD response, but if I continue walking to the wall I receive an ADD and REMOVE event, but my sensor is still over the wall, however when I go back I receive an REMOVE event. Why I receive ADD and REMOVE in the middle??? If you see attached pictures you will understand it. Please take a look.

Edge wrote:Whether you use them or not, you are still dependent upon the contact listener. For reactions (i.e. hitting a wall), I would think you should be able to do that on the contact add. For status type events (i.e. am I standing on the ground so I can jump), I keep a counter that I increment on contact adds and decrement on removes. So a status check is just checking whether my count is greater than 0 or not. Downside is I have to keep a counter for everything I want a status on. And for some reason the count do get flaky over time on some occasions.


Does that apply also to sensors? In this case, I guess that is the problem I am having, am I right?

@Erin: Is a bad idea to use 3 sensors (right, left botton) on all my characters if I'm going to use a not too much powerful device (iPhone)?, I mean, computationally. Or should I really consider using contact point and test their normals?

Apart from useful Edge links, does anyone know more info about how to post-process contacts? Box2D is easy to use, but contacts are a bit difficult to manage.

Thanks in advance.
You can get add/remove events when the contact points features change. Contact points are created by clipping. When the clipped features change, so do the contact point ids.

Original thread: https://box2d.org/forum/viewtopic.php?t=3203