Collision_response message multiple times per frame?

contact_point_response can be sent multiple times per frame while collision_response happens once per frame.

Mentioned in this thread: Physics Contact Response (SOLVED) - #2 by britzl

Without knowing for sure what a frame entails in this scenario, I am currently confused by this.

My scenario is that a bullet hits an enemy.
The enemy listens on collision_response and in the handler I ask my playerbulletmanager (lua module) to get the data tied to that bullet instance.

The bullet is ‘consumed’ and thus deleted by go.delete, all inside this message handling function.

Immediately after this message, there’s another identical message. Possibly because I have some overlappping shapes in my collision object? Not sure if that brings this effect.

Now, I handle this by first checking if my playerbulletmanager still “has” this bullet (it does not, it’s deleted since last message). This works fine, but I want to know if this is correct behavior.

I’d like to do something like this in my manager:

function M.get_bullet(instance)
	assert(bullets[instance] ~= nil, "Bullet does not exist: " .. tostring(instance))
	return bullets[instance]
end

It’s just nice to have, to help me ensure I don’t do unexpected things.
But this throws tons of errors now due to multiple collision_response messages, since the bullet is already gone!

But I don’t really want to ask for that bullet more than a single time. And it feels convoluted that my enemy should also keep a table on which bullet instances it has already consumed and not ask for again.

If I remember correctly you should only get one collision_response message per collision object that your bullet is colliding with but you could get multiple contact_point_response messages if the collision objects that collide has more than one shape. Could that be the scenario you’re experiencing?

I’ve used the debugger and checked - contact_point_response does happen multiple times, but so does collision_response.

I only act on the collision_response.
Also, it’s intermittent, not always happening twice.

I can share some code when I get back from work.

@britzl I added you to a simple test project “physicstest” that displays the situation.

Basically object1 (bullet) goes towards object2 (enemy). Upon contact, there are two collision response messages, since there are two (layered on top of each other) collision shapes in the collision object of the bullet.

This is not verbatim to my situation, but I do have some shape overlap to make my enemy’s hit area as expected as possible.

I would expect go.delete on the bullet object to prevent the next collision response from happening, but I suppose go.delete simply queues it up for next pass of… processing things - and before deleted there’s a collision message to take care of?

Yes, exactly this. The object is not really deleted until after the frame “has ended” so to speak. This means that you may need to do some additional book keeping.

2 Likes

Ok cool, as long as I know the playing field, I’ll adapt, overcome and improvise.

2 Likes

That’s the spirit!