Why is a collision happening here?

I have a script that lets me drag a kinematic shape, and on action.released it gets switched out by the same shape but with a dynamic collision type.

Code
function init(self)
	msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
	if action_id == hash("leftclick") then

		if action.pressed then
			newObject = factory.create( "#cube_kinematic", vmath.vector3(action.x, action.y, 0), nil, nil, vmath.vector3(0.5, 0.5, 1))
		end

		go.set_position(vmath.vector3(action.x, action.y, 0), newObject)

		if action.released then -- Here is the object being switched out
			go.delete(newObject, true)
			newObject = factory.create( "#cube", vmath.vector3(action.x, action.y, 0), nil, nil, vmath.vector3(0.5, 0.5, 1))
		end
	end
end

I don’t understand why a collision is happening here?

1 Like

The game object isn’t immediately deleted when go.delete() is called, it’s deleted at the end of the frame. This might mean the old and new collision shapes collide with each other before the game object is deleted.

Is there a workaround? I have no idea how I should solve this issue.

You might be able to use the new and fancy physics.set_group(). If it’s instant, then setting it to a group that doesn’t collide could solve your issue.

1 Like

Another solution might be to use a timer and delay the dynamic object for a frame (delay 0 should do this)

4 Likes

Yet ANOTHER option could be to have both collision objects in the same game object, and enable/disable them as needed instead of deleting and creating game objects.

11 Likes

Ah, yes, that’s an even better solution!

3 Likes