I've been having no issues with contact listeners, except for one small thing.
I have a box controlled with the arrow keys (the player), and a set of circles you can collect as if they are items (set as sensors). So, I simply check each contact point added to a stack, and if one of the shapes of the contact point is one of the circular items, then I destroy the item. This works perfectly, unless the player is colliding with an actual solid and static circle; in which case any circular item seems to not be detected until I release contact with the solid circle. However, the player can be touching a solid and static box and any circular items are detected just fine.
Does anyone know why this is occurring?
Thanks.
Any ideas?
(Should I provide more details/graphics?)
Ehm.
It sounds to me like you are only processing the first contact. Are you assuming that the player only collides with a single other circle, and just proccessing the first/only contact in the contact list?
That might be why you are only processing the contact with the static circle every time, in stead of the other ones.
It's just a guess though.
I'm having a little trouble understanding the problem, exactly, but are you trying to destroy the body from within a function that's being called by the contact listener? If so, that's a Bad Thing. You shouldn't attempt to alter the physics world within the contact listener, or within a function that's being called by the contact listener. Try to set a flag or add your body to a list of "objects to destroy" and then destroy 'em at the next safe opportunity.
I am indeed making a list of all added contact points, and then looping through each one after the time step. For debugging purposes I decided to visually plot each contact point that is added, and disable the destruction of the sensors. This is what I observed:
player: dynamic polygon
wall: static polygon
circle: static circle
item: static circle sensor
-If player collides with only item, then a contact point is produced between player and item.
-If player is currently in contact with wall and then collides with item, then a contact point is produced between player and item.
-If player is currently in contact with circle and then collides with item, then a contact point between player and item fails to be produced.
If I change item into a static polygon sensor, then the second statement fails to produce a contact point, and the third statement is successful.
I hope this makes things clear.
i wonder if you are assuming that shape1 can be the hero and shape 2 cannot be the hero in your contact listeners
it may just have to do with odering on contact lists.
or perhaps you may be only paying attention to a single collision.
a = 1
a = 2
a will be 2
a = 2
a = 3
a will not be 2
so the order that the collisions are detected could also bring out an error in your code.
take a good look at your contactListener class
-and make sure to treat shape1 and shape2 anonymously where they could be flipped, and your code would still sort it out.
as far as i have tested contacts work perfectly, they just have to be interpreted correctly which can lead to trouble when I make assumptions about the info being passed in.
good luck.
Well, I figured out a temporary workaround. For each item, I'm giving it a combination of a box and a circle (remember, these items are sensors). That way it will be detected if the player is sliding against another box or a circle.
Well, the workaround still fails about 20% of the time. I really have no idea why this anomaly is occurring.