Physics Contact Response (SOLVED)

I am currently in the process of creating a prototype game with Defold.

I have 2 objects that have sphere collision objects on them. I also have the appropriate groups/masks to detect a collision.

I have an explosion particle effect that I want to be played at the first collision point. I understand from the API reference that this is a vector3 and is obtainable by:

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") then
		conPos = message.position
	end

        if message_id == hash("collision_response") and message.group == hashes.group_EnemyBullets then
	    print("Player hit with: ", message.group)
	    local url = msg.url(message.other_id)
	    url.fragment = "script"
	    local amt = go.get(url, hash("enemyBulletDamage"))
	    print("conPos: ", conPos)
	    msg.post(".", hashes.health_TakeHit, { amount = amt, contactPos = conPos })
        end

        if message_id == hash("collision_response") and message.group == hashes.group_Enemy then
	    local url = msg.url(message.other_id)
	    url.fragment = "script"
	    local amt = go.get(url, hash("enemyPhysicalDamage"))
	    msg.post(".", hashes.health_TakeHit, { amount = amt, contactPos = conPos })
        end
end

I post ā€˜conPosā€™ to a script that activates the particle effect and it works.

It is perfect on the first contact, but if I move object 1 and it receives another contact from object 2 the particle effect activates at object 1ā€™s previous position.

I would assume that ā€˜conPosā€™ would be updated with the current contact point but it doesnā€™t seems to be operating like that. Ideally, I would like the particle effect to activate at the point of contact no matter the location or which side of the object.

Does anyone have any thoughts on this?

Thanks for your time and attention.

EDIT: Formatting

So on ā€œcontact_point_responseā€ you store the conPos and on ā€œcollision_responseā€ you use whatever value is in ā€œconPosā€ to play a particle effect at that position?

Note that there is not a 1:1 relation between contact_point_response messages and collision_response messages. contact_point_response can be sent multiple times per frame while collision_response happens once per frame. Iā€™m not sure the order of these two messages is guaranteed either.

Canā€™t you play the particle effect immediately when you get the ā€œcontact_point_responseā€ instead of waiting for ā€œcollision_responseā€?

To test ā€¦ I moved all my physics related code into contact_point_response instead of collision_response and it all works perfectly. I donā€™t know if that move is a detriment (since I am still learning Defold) or not but I will continue prototyping and see what happens.

Thanks!

1 Like

One difference might be that you can get multiple contact_point_response messages and if you use these messages as a trigger to remove hitpoints or similar they might deplete at a higher rate than if you use collision_response since youā€™ll only get one of those per frame.