Hello! So i got fixed you code and got it working like you want a couple hours ago but i had to go so i couldn’t post it!
But now i’m back so here it goes!!!
So what i did was add a onCollision function that does this –
When “ball1” and “ball2” collide then isSensor turns on for both of those objects so they instantly pass through each other… And when the collision between them ends then the isSensor is turned off.
But there was a problem… When the isSensor was on when the objects were by the wall then they would just pass through the wall…
So what i did was made it so when the “myArea” collides with “ball1” or “ball2” then isSensor is turned off and instantly bounces the object.
Ok so here’s the code!!!
local physics = require( "physics" ) physics.start() physics.setGravity( 0, 0 ) local myArea = display.newLine( 40, 40, 440, 40, 440, 280, 40, 280, 40, 40) myArea.strokeWidth = 2 myArea:setStrokeColor( 1, 1, 1 ) physics.addBody( myArea, "static", { bounce = 1 }) myArea.myName = "myArea" local ball1 = display.newCircle(100, 100, 20) physics.addBody( ball1, "dynamic", { bounce = 1, radius = 20 }) ball1.isFixedRotation = true ball1:setLinearVelocity( -200, 100 ) ball1.myName = "ball1" local ball2 = display.newCircle(160, 240, 45) physics.addBody( ball2, "dynamic", { bounce = 1, radius = 45 }) ball2.isFixedRotation = true ball2:setLinearVelocity( -100, -200 ) ball2.myName = "ball2" local function onCollision(event) if event.phase == "began" then if event.target.myName == "ball1" and event.other.myName == "ball2" then ball1.isSensor = true ball2.isSensor = true elseif event.target.myName == "myArea" and event.other.myName == "ball1" then ball1.isSensor = false elseif event.target.myName == "myArea" and event.other.myName == "ball2" then ball2.isSensor = false end elseif event.phase == "ended" then if event.target.myName == "ball1" and event.other.myName == "ball2" then ball1.isSensor = false ball2.isSensor = false end end end ball1:addEventListener( "collision", onCollision ) ball2:addEventListener( "collision", onCollision ) myArea:addEventListener( "collision", onCollision )
Good Luck!
IsSensor - https://docs.coronalabs.com/api/type/Body/isSensor.html