How do i detect this type of collision?

So i have this code… you can put it into any main.lua and it will work…

local physics = require("physics") physics.start() local box = display.newRect( display.contentCenterX, display.contentCenterY - 200, 30, 30 ) box:setFillColor( 1, 0, 0 ) box.rotation = 45 box.myName = "box" physics.addBody( box, "dynamic", { bounce = 0 } ) local leftLine = display.newRect( display.contentCenterX - 45, display.contentCenterY + 200, 20, 100 ) leftLine:setFillColor( 0, 0, 1 ) leftLine.rotation = -45 leftLine.myName = "leftLine" physics.addBody( leftLine, "static", { bounce = 0 } ) local rightLine = display.newRect( display.contentCenterX + 45, display.contentCenterY + 200, 20, 100 ) rightLine:setFillColor( 0, 0, 1 ) rightLine.rotation = 45 rightLine.myName = "rightLine" physics.addBody( rightLine, "static", { bounce = 0 } ) local function onCollision( event ) if event.phase == "began" then if event.target.myName == "box" and event.other.myName == "leftLine" and event.other.myName == "rightLine" then print("hiii") end end end box:addEventListener( "collision", onCollision )

So what i want is the onCollision function to fire when the box touches both of the line… and nothing to happen if the box touches just one…

Thanks!

For this, you’ll likely need to keep a “counter” variable on the box, and when it collides with one of the lines, increment the count by 1. Likewise, when the box ends collision with one of the lines (“ended” phase), reduce the count by 1. Then, on lines following that, check if the count value is 2… that would indicate that the object has collided with both lines at once.

Brent

Brilliant thinking Brent!!! I know exactly what to do now!! If anything I’ll be back here asking questions haha

Thanks!

Thanks brent! It worked!

For this, you’ll likely need to keep a “counter” variable on the box, and when it collides with one of the lines, increment the count by 1. Likewise, when the box ends collision with one of the lines (“ended” phase), reduce the count by 1. Then, on lines following that, check if the count value is 2… that would indicate that the object has collided with both lines at once.

Brent

Brilliant thinking Brent!!! I know exactly what to do now!! If anything I’ll be back here asking questions haha

Thanks!

Thanks brent! It worked!