Intersecting two objects

Hello…

I have a red circle.

local redCircle = display.newCircle(0, 0, 30)

and a green circle …

local greenCircle = display.newCircle(1400, 0, 20)

I have a transition to both circles.

transition.to(redCircle, {time=1000, x=1400}) transition.to(greenCircle, {time=100, x=0})

redCircle is going slow from left to right

greenCircle is going fast from right to left

QUESTION

How can I detect at what point the “meet” or “have a collision”

or “intersect”

something like

if redCircle.x + (30) == greenCircle.x + (20) then -- the (30) or (20) is the radius of the circles       transition.cancel( )       audio.play(sound1) end

As soon as the outer circumference meet they “know” they “crash”

so the function does something

Thanks for all your help

Victor

Perhaps this tutorial will guide you in the right direction.

https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Rob

Are you using physics?

–SonicX278

Hi Rob…

I have this function

&nbsp;&nbsp;&nbsp; local obj1 = display.newCircle(0, 0, 30) &nbsp;&nbsp;&nbsp; obj1.x = 100 &nbsp;&nbsp;&nbsp; obj1.y = 100 &nbsp;&nbsp;&nbsp; obj1:setFillColor(1, 0, 1) &nbsp;&nbsp;&nbsp; transition.to(obj1, {time=1000, x=1200}) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local obj2 = display.newCircle(0, 0, 50) &nbsp;&nbsp;&nbsp; obj2.x = 1200 &nbsp;&nbsp;&nbsp; obj2.y = 100 &nbsp;&nbsp;&nbsp; obj2:setFillColor(1, 0, 0) &nbsp;&nbsp;&nbsp; transition.to(obj2, {time=5000, x=100}) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; -- Circle-based collision detection local function hasCollidedCircle( obj1, obj2 ) &nbsp;&nbsp;&nbsp; if ( obj1 == nil ) then&nbsp; -- Make sure the first object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( obj2 == nil ) then&nbsp; -- Make sure the other object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local dx = obj1.x - obj2.x &nbsp;&nbsp;&nbsp; local dy = obj1.y - obj2.y &nbsp;&nbsp;&nbsp; local distance = math.sqrt( dx\*dx + dy\*dy ) &nbsp;&nbsp;&nbsp; local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2) &nbsp;&nbsp;&nbsp; if ( distance \< objectSize ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return false end hasCollidedCircle( obj1, obj2 )

But how do I detect they are “touching”?

The article is long…

but really good, I think this is what I am looking for

and I am not using physics

I will have just circles moving around

in a pre-defined path. so no physics

I need the famous

if obj1 is touching obj2 then …

where can I download this sample App?

that function returns either true or falls if they are intersecting or not. So you have to use it in an “if” test:

if hasCollidedCircle( obj1, obj2 ) then &nbsp;&nbsp;&nbsp; -- do the stuff when they are touching else &nbsp;&nbsp;&nbsp; -- do stuff when they are not touching end

Hi Rob…

I look at the if … you gave mebut I don’t know

where should I put it

all this is inside the create scene

&nbsp;&nbsp;&nbsp; -- fondo &nbsp;&nbsp;&nbsp; bg = display.newImageRect( sceneGroup, "images/bg.jpg", 1500, 900 ) &nbsp;&nbsp;&nbsp; bg.x = display.contentCenterX &nbsp;&nbsp;&nbsp; bg.y = display.contentCenterY &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; obj1 = display.newCircle(0, 0, 30) &nbsp;&nbsp;&nbsp; obj1.x = 100 &nbsp;&nbsp;&nbsp; obj1.y = 100 &nbsp;&nbsp;&nbsp; obj1:setFillColor(1, 0, 1) &nbsp;&nbsp;&nbsp; transition.to(obj1, {time=4000, x=1200}) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; obj2 = display.newCircle(0, 0, 50) &nbsp;&nbsp;&nbsp; obj2.x = 1200 &nbsp;&nbsp;&nbsp; obj2.y = 100 &nbsp;&nbsp;&nbsp; obj2:setFillColor(1, 0, 0) &nbsp;&nbsp;&nbsp; transition.to(obj2, {time=5000, x=100}) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; bomb = display.newCircle(0, 0, 30) &nbsp;&nbsp;&nbsp; bomb.x = 700 &nbsp;&nbsp;&nbsp; bomb.y = 600 &nbsp;&nbsp;&nbsp; bomb:setFillColor(0, 1, 0) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; -- Circle-based collision detection local function hasCollidedCircle( obj1, obj2 ) &nbsp;&nbsp;&nbsp; if ( obj1 == nil ) then&nbsp; -- Make sure the first object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( obj2 == nil ) then&nbsp; -- Make sure the other object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local dx = obj1.x - obj2.x &nbsp;&nbsp;&nbsp; local dy = obj1.y - obj2.y &nbsp;&nbsp;&nbsp; local distance = math.sqrt( dx\*dx + dy\*dy ) &nbsp;&nbsp;&nbsp; local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2) &nbsp;&nbsp;&nbsp; if ( distance \< objectSize ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return false end hasCollidedCircle( obj1, obj2 ) local function hope() &nbsp;&nbsp;&nbsp; if hasCollidedCircle( obj1, obj2 ) then &nbsp;&nbsp;&nbsp; -- do the stuff when they are touching &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.xScale = 3 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.yScale = 3 &nbsp;&nbsp; &nbsp;else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.xScale = 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.yScale = 1 &nbsp;&nbsp; &nbsp;end end hope()

I put it inside a function and I call both functions

hope( )

hasCollidedCircle( obj1, obj2 )

I put it under – hasCollidedCircle( obj1, obj2 ) function and nothing

I am expecting the little circle “bomb”

to get big as soon as both circles touch

and nothing

Thanks for your time

Victor

You would probably need to setup a Runtime “enterFrame” listener that fires every 1/30th or 1/60th of a second. The function that gets called would be where you would do this check.

Rob

Thank you very much Rob

it works!!! now…

great!!!

I added the Runtime

local function hope( ) &nbsp;&nbsp;&nbsp; if hasCollidedCircle( obj1, obj2 ) then &nbsp;&nbsp;&nbsp; -- do the stuff when they are touching &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.xScale = 3 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.yScale = 3 &nbsp;&nbsp; &nbsp;else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.xScale = 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.yScale = 1 &nbsp;&nbsp; &nbsp;end end Runtime:addEventListener("enterFrame", hope )

And now works fine.

Thanks

Perhaps this tutorial will guide you in the right direction.

https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Rob

Are you using physics?

–SonicX278

Hi Rob…

I have this function

&nbsp;&nbsp;&nbsp; local obj1 = display.newCircle(0, 0, 30) &nbsp;&nbsp;&nbsp; obj1.x = 100 &nbsp;&nbsp;&nbsp; obj1.y = 100 &nbsp;&nbsp;&nbsp; obj1:setFillColor(1, 0, 1) &nbsp;&nbsp;&nbsp; transition.to(obj1, {time=1000, x=1200}) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local obj2 = display.newCircle(0, 0, 50) &nbsp;&nbsp;&nbsp; obj2.x = 1200 &nbsp;&nbsp;&nbsp; obj2.y = 100 &nbsp;&nbsp;&nbsp; obj2:setFillColor(1, 0, 0) &nbsp;&nbsp;&nbsp; transition.to(obj2, {time=5000, x=100}) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; -- Circle-based collision detection local function hasCollidedCircle( obj1, obj2 ) &nbsp;&nbsp;&nbsp; if ( obj1 == nil ) then&nbsp; -- Make sure the first object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( obj2 == nil ) then&nbsp; -- Make sure the other object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local dx = obj1.x - obj2.x &nbsp;&nbsp;&nbsp; local dy = obj1.y - obj2.y &nbsp;&nbsp;&nbsp; local distance = math.sqrt( dx\*dx + dy\*dy ) &nbsp;&nbsp;&nbsp; local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2) &nbsp;&nbsp;&nbsp; if ( distance \< objectSize ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return false end hasCollidedCircle( obj1, obj2 )

But how do I detect they are “touching”?

The article is long…

but really good, I think this is what I am looking for

and I am not using physics

I will have just circles moving around

in a pre-defined path. so no physics

I need the famous

if obj1 is touching obj2 then …

where can I download this sample App?

that function returns either true or falls if they are intersecting or not. So you have to use it in an “if” test:

if hasCollidedCircle( obj1, obj2 ) then &nbsp;&nbsp;&nbsp; -- do the stuff when they are touching else &nbsp;&nbsp;&nbsp; -- do stuff when they are not touching end

Hi Rob…

I look at the if … you gave mebut I don’t know

where should I put it

all this is inside the create scene

&nbsp;&nbsp;&nbsp; -- fondo &nbsp;&nbsp;&nbsp; bg = display.newImageRect( sceneGroup, "images/bg.jpg", 1500, 900 ) &nbsp;&nbsp;&nbsp; bg.x = display.contentCenterX &nbsp;&nbsp;&nbsp; bg.y = display.contentCenterY &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; obj1 = display.newCircle(0, 0, 30) &nbsp;&nbsp;&nbsp; obj1.x = 100 &nbsp;&nbsp;&nbsp; obj1.y = 100 &nbsp;&nbsp;&nbsp; obj1:setFillColor(1, 0, 1) &nbsp;&nbsp;&nbsp; transition.to(obj1, {time=4000, x=1200}) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; obj2 = display.newCircle(0, 0, 50) &nbsp;&nbsp;&nbsp; obj2.x = 1200 &nbsp;&nbsp;&nbsp; obj2.y = 100 &nbsp;&nbsp;&nbsp; obj2:setFillColor(1, 0, 0) &nbsp;&nbsp;&nbsp; transition.to(obj2, {time=5000, x=100}) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; bomb = display.newCircle(0, 0, 30) &nbsp;&nbsp;&nbsp; bomb.x = 700 &nbsp;&nbsp;&nbsp; bomb.y = 600 &nbsp;&nbsp;&nbsp; bomb:setFillColor(0, 1, 0) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; -- Circle-based collision detection local function hasCollidedCircle( obj1, obj2 ) &nbsp;&nbsp;&nbsp; if ( obj1 == nil ) then&nbsp; -- Make sure the first object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( obj2 == nil ) then&nbsp; -- Make sure the other object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local dx = obj1.x - obj2.x &nbsp;&nbsp;&nbsp; local dy = obj1.y - obj2.y &nbsp;&nbsp;&nbsp; local distance = math.sqrt( dx\*dx + dy\*dy ) &nbsp;&nbsp;&nbsp; local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2) &nbsp;&nbsp;&nbsp; if ( distance \< objectSize ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return false end hasCollidedCircle( obj1, obj2 ) local function hope() &nbsp;&nbsp;&nbsp; if hasCollidedCircle( obj1, obj2 ) then &nbsp;&nbsp;&nbsp; -- do the stuff when they are touching &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.xScale = 3 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.yScale = 3 &nbsp;&nbsp; &nbsp;else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.xScale = 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.yScale = 1 &nbsp;&nbsp; &nbsp;end end hope()

I put it inside a function and I call both functions

hope( )

hasCollidedCircle( obj1, obj2 )

I put it under – hasCollidedCircle( obj1, obj2 ) function and nothing

I am expecting the little circle “bomb”

to get big as soon as both circles touch

and nothing

Thanks for your time

Victor

You would probably need to setup a Runtime “enterFrame” listener that fires every 1/30th or 1/60th of a second. The function that gets called would be where you would do this check.

Rob

Thank you very much Rob

it works!!! now…

great!!!

I added the Runtime

local function hope( ) &nbsp;&nbsp;&nbsp; if hasCollidedCircle( obj1, obj2 ) then &nbsp;&nbsp;&nbsp; -- do the stuff when they are touching &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.xScale = 3 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.yScale = 3 &nbsp;&nbsp; &nbsp;else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.xScale = 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.yScale = 1 &nbsp;&nbsp; &nbsp;end end Runtime:addEventListener("enterFrame", hope )

And now works fine.

Thanks