Need more precision in detecting collision as user draws

So, I have a canvas in which users can draw. If the user draws a bounded region, like a square, circle, O etc, then I replace it with a circle of fixed radius.

User uses a circle brush to draw.

I have used the physics engine as follows(gravity set to 0):

  • The point of starting touch event is a dynamic physics body. (Red circle) 
  • Every circle which is a multiple of 5 is a dynamic physics body, till 20 circles are drawn. After that all circles drawn are static physics bodies(Green). The intermediate - 1 to 4, 6 to 9, 11 to 14 and 16 to 19 are not physics objects (yellow) to avoid collision.

But, I couldn’t detect collision when the static green end touches the red points. 

So, I created a code like this,

  • If the distance between the current point (event.x and event.y) and the points where points are dynamic (dynamicPtX, dynamicPtY) is less than 5, then we mark the point being drawn at that time to be dynamic.
  • Therefore,when two dynamic objects collide, collision should be detected.

Problems:

  • Only the start point is being detected for collision. That too only in some cases. i.e, as you drag the green line towards the red point, only the first red point responds.
  • If the user drags their finger very slowly, the initial circles coincide and decide it to be a collision.

Here is the code :

local function onCollision( event ) if(event.phase == "began") then isCircle = 1 elseif (event.phase == "ended") then isCircle = 1 end end local function myTouchListener( event ) if ( event.phase == "began" ) then coorX = event.x coorY = event.y elseif ( event.phase == "moved" ) then local paint = { 1, 0, 0.5 } s = s+1 strokeCircle[s] = display.newCircle(event.x,event.y,5) --Brush used by user to draw on touch if(s \<= 20) then if(s% 5 ==0)then --The initial dynamic points (red) physics.addBody(strokeCircle[s],"dynamic",{density = 0,isSensor=1,radius = 3}) strokeCircle[s]:setFillColor(1,0,0.5) ptr = ptr + 1 dynamicPtX[ptr] = event.x dynamicPtY[ptr] = event.y else strokeCircle[s]:setFillColor(1,1,0.5) --The non physics points surrounding dynamic ones (yellow) end elseif((s\>20) and (s\<28) ) then strokeCircle[s]:setFillColor(1,1,0.5) elseif ( math.sqrt(math.pow((event.x - coorX),2) + math.pow((event.y - coorY),2) ) \< 2) then physics.addBody(strokeCircle[s],"dynamic",{density = 0,isSensor=1,radius = 4}) strokeCircle[s]:setFillColor(1,0,0.5) else physics.addBody(strokeCircle[s],"static",{density = 0,isSensor=1,radius = 4}) strokeCircle[s]:setFillColor(0,1,0) end --Check if current point is closer to any of previous dynamic points. if ( math.sqrt(math.pow((event.x - coorX),2) + math.pow((event.y - coorY),2) ) \< 5) or ( math.sqrt(math.pow((dynamicPtX[1] - event.x),2) + math.pow((dynamicPtY[1] - event.y),2) ) \< 5) or ( math.sqrt(math.pow((dynamicPtX[2] - event.x),2) + math.pow((dynamicPtY[2] - event.y),2) ) \< 5) or ( math.sqrt(math.pow((dynamicPtX[3] - event.x),2) + math.pow((dynamicPtY[3] - event.y),2) ) \< 5) or ( math.sqrt(math.pow((dynamicPtX[4] - event.x),2) + math.pow((dynamicPtY[4] - event.y),2) ) \< 2) then physics.addBody(strokeCircle[s],"dynamic",{density = 0,isSensor=1,radius = 5}) strokeCircle[s]:setFillColor(1,0,0.5) print("s ="..s.." ,event.x, dynamicPtX[1] = "..event.x..","..dynamicPtX[1]) print("s ="..s.." ,event.x, dynamicPtX[2] = "..event.x..","..dynamicPtX[2]) print("s ="..s.." ,event.x, dynamicPtX[3] = "..event.x..","..dynamicPtX[3]) print("s ="..s.." ,event.x, dynamicPtX[4] = "..event.x..","..dynamicPtX[4]) end elseif ( event.phase == "ended" ) then print("farthestPtX , farthestPtY"..farthestPtX..","..farthestPtY) print("coorX , coorY"..coorX..","..coorY) centerX = (farthestPtX + coorX) /2 centerY = (farthestPtY + coorY) /2 print("Center is "..centerX..","..centerY) s = -1 ptr = -1 end return true --prevents touch propagation to underlying objects end --The canvas to draw local myButton = display.newRect( display.contentCenterX,display.contentCenterY,display.viewableContentWidth, display.viewableContentHeight ) myButton:addEventListener( "touch", myTouchListener ) Runtime:addEventListener("collision",onCollision)

Hi @sudha,

First thing… have you confirmed that using “isSensor=1” actually creates a sensor? It should be a boolean true or false… perhaps 0 or 1 works, but we document that it should be a boolean, so I suggest you first change that aspect in all places you use it.

Best regards,

Brent

Hi @sudha,

First thing… have you confirmed that using “isSensor=1” actually creates a sensor? It should be a boolean true or false… perhaps 0 or 1 works, but we document that it should be a boolean, so I suggest you first change that aspect in all places you use it.

Best regards,

Brent