In this example I'll show you how to create triggers in Box2d world. We'll create a trigger, on collision with which we will toggle one Box2d objects (setting it active and inactive again). For start we'll use existing Box2D draggable project and modify it.

So what we need is a method to create a trigger, which basically is a sensor. Sensors are objects, that does not collide with other objects, but they still dispatch collision events for dynamic objects. Think of it as ghost objects.

In this same method we will create simple Box2d, the one we will toggle later.

--create a trigger
function scene:trigger(triggerX, triggerY, triggerW, triggerH, 
	targetX, targetY, targetW, targetH)
	--create simple box2d object using wall method
	local target = self:wall(targetX, targetY, targetW, targetH)
	--create trigger
	local block =
	block:setFillStyle(Shape.SOLID, 0xffff00)  
	block:moveTo(-triggerW/2, -triggerH/2)
	block:lineTo(triggerW/2, -triggerH/2)
	block:lineTo(triggerW/2, triggerH/2)
	block:lineTo(-triggerW/2, triggerH/2)
	block:lineTo(-triggerW/2, -triggerH/2)
	x = triggerX-(triggerW/2) 
	y = triggerY-(triggerH/2)
	--create box2d physical object
	local body ={type = b2.STATIC_BODY}
	body:setPosition(block:getX(), block:getY())
	body:setAngle(block:getRotation() * math.pi/180)
	local poly =
	poly:setAsBox(block:getWidth()/2, block:getHeight()/2)
	--all the magic is here, it is a sensor
	local fixture = body:createFixture{shape = poly, density = 1.0, 
	friction = 0.1, restitution = 0.8, isSensor = true}
	block.body = body
	block.body.type = "trigger" = target.body

Then we need to create a method, which will handle collision and check if ball collides with sensor, than we'll trigger object's toggling

--define collision event handler function
function scene:onBeginContact(e)
	--getting contact bodies
	local fixtureA = e.fixtureA
	local fixtureB = e.fixtureB
	local bodyA = fixtureA:getBody()
	local bodyB = fixtureB:getBody()
	--check if first colliding body is a trigger
	--it should be first, because it was created before dragging ball object
	if bodyA.type and bodyA.type == "trigger" and not bodyB.triggered then
		--marking dragging ball, so it won't trigger again
		--bodyB.triggered = true
		--creating timer to delay changing world
		--because by default you can't change world settings 
		--in event callback function
		--delay 1 milisecond for 1 time
		local timer =, 1)
		--setting timer callback
		timer:addEventListener(Event.TIMER_COMPLETE, function()
			--change active state of target body (toggle it)
		--start timer

And last thing to do is to add this event handler to event listener in init method. Nothing that we haven't done before.

--add collision event listener, self.onBeginContact, self)

You can see the result here:

And of course you can download sample Gideros project of this example