How can two body don't collide?

I want that ball1 and ball2 don’t collide, when still balls collide with myArea’s stroke.

[lua]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 })

local ball1 = display.newCircle(160, 240, 20)
physics.addBody( ball1, “dynamic”, { bounce = 1 })
ball1.isFixedRotation = true
ball1:setLinearVelocity( 100, 100 )

local ball2 = display.newCircle(160, 240, 45)
physics.addBody( ball2, “dynamic”, { bounce = 1 })
ball2.isFixedRotation = true
ball2:setLinearVelocity( -100, -100 )[/lua]

How can i manage this?

Thanks!

Note: Orientation is landscape in this sample.

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! :frowning:

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

Thank you for your post SonicX278.

I am new in Physics library but i read isSensor property before i start this topic.

I think maybe there is a stable method.

Sory but; there is a bug in this method.

You can see it when ball1 radius set 80 and ball2 radius set 70 :frowning:

Hello! Yes i admit this is a buggyish method… But its all i could think of in less than 15 min!

Well i can’t give you anything right now as i have to finish my stuff for the day and head to bed, i have a long day tomorrow.

As soon as i can make something for you tomorrow and if no one helped you before me then ill be glad to help

Side Note – Questions like these are answered faster in the general questions and discussions part of the forums.

Good Luck!

Please note.  Adding a body to a line is not recommended as you’re often going to find it does not work as you expect.

Set this to see where the bodies are and you’ll probably see that your ‘line body’ is either too thin or not in the right place.

physics.setDrawMode( "hybrid" ) -- after physics.start()

I’d use a thin rectangle instead and set the field . isBullet on any moving bodies if you want them to have a chance of hitting a thin body without passing through with no collision detected or responded to.

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! :frowning:

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

Thank you for your post SonicX278.

I am new in Physics library but i read isSensor property before i start this topic.

I think maybe there is a stable method.

Sory but; there is a bug in this method.

You can see it when ball1 radius set 80 and ball2 radius set 70 :frowning:

Hello! Yes i admit this is a buggyish method… But its all i could think of in less than 15 min!

Well i can’t give you anything right now as i have to finish my stuff for the day and head to bed, i have a long day tomorrow.

As soon as i can make something for you tomorrow and if no one helped you before me then ill be glad to help

Side Note – Questions like these are answered faster in the general questions and discussions part of the forums.

Good Luck!

Please note.  Adding a body to a line is not recommended as you’re often going to find it does not work as you expect.

Set this to see where the bodies are and you’ll probably see that your ‘line body’ is either too thin or not in the right place.

physics.setDrawMode( "hybrid" ) -- after physics.start()

I’d use a thin rectangle instead and set the field . isBullet on any moving bodies if you want them to have a chance of hitting a thin body without passing through with no collision detected or responded to.