need help with collision

Hi, in a level of my game, players have to draw a line for connects 2 dots. I think I will need to use a collision event, but…how?

here’s the code for the line, and the dot will be called dot.png

local lines = {}  
local lineGroup = display.newGroup()  
local line\_number = 1  
local line\_width = 5  
local prev\_x, prev\_y, ball  
local isDrawing = false  
local i = 1  
   
local function draw\_line(e)  
   
 if e.phase == "began" then  
  
 for i = #lines, 1, -1 do  
 if (lines[i]) then  
 lines[i].parent:remove(lines[i])  
 lines[i] = nil  
 end  
 end  
 lines = {}  
 line\_number = 1  
  
 prev\_x = e.x  
 prev\_y = e.y  
 isDrawing = true  
   
 elseif e.phase == "moved" then  
 lines[line\_number] = display.newLine(prev\_x, prev\_y, e.x, e.y)   
 lines[line\_number]:setColor(255, 255, 255)  
 lines[line\_number].width = line\_width  
 dist\_x = e.x - prev\_x  
 dist\_y = e.y - prev\_y  
 -- Add a physics body that's a flat polygon that follows each segment of the line  
 physics.addBody(lines[line\_number], "static", { density = 1, friction = 0.5, bounce = 0, shape = {0, 0, dist\_x, dist\_y, 0, 0} } )  
 prev\_x = e.x  
 prev\_y = e.y  
 line\_number = line\_number + 1  
 elseif e.phase == "ended" then  
 end  
end  
   
Runtime:addEventListener("touch", draw\_line)  

Then, when the dot is reached by the line, I will print on the screen an “OK” message.

Thanks :slight_smile: [import]uid: 76800 topic_id: 31739 reply_id: 331739[/import]

static bodies don’t really cause collision events, AFAIK, but dynamic bodies fire collision events when they hit a static.

I would suggest you use a touchable circle that follows your finger as you draw, and check collision on that, and then do you need it to check whether there’s a continuous unbroken chain between point 2 (that you’re touching now) and point 1 (that you touched first?) that would take some more thinking but I have a couple ideas.

[import]uid: 63787 topic_id: 31739 reply_id: 128576[/import]

static bodies don’t really cause collision events, AFAIK, but dynamic bodies fire collision events when they hit a static.

I would suggest you use a touchable circle that follows your finger as you draw, and check collision on that, and then do you need it to check whether there’s a continuous unbroken chain between point 2 (that you’re touching now) and point 1 (that you touched first?) that would take some more thinking but I have a couple ideas.

[import]uid: 63787 topic_id: 31739 reply_id: 128576[/import]