Draw a line with physics

Thanks for the assistance i understand your logic, but I don’t understand how the function works so I have no idea how to implement it into the “draw a line” function I am using. So basically I need to add your function to this function:

[lua]-- Draw a line

local i = 1
local tempLine
local ractgangle_hit = {}
local prevX , prevY
local function runTouch(event)

if(event.phase==“began”) then
if(tempLine==nil) then
tempLine=display.newLine(event.x, event.y, event.x, event.y)
prevX = event.x
prevY = event.y
end

elseif(event.phase==“moved”) then
tempLine:append(event.x,event.y-2)
tempLine.width=tempLine.width+0.9
ractgangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y)
ractgangle_hit[i].width = 5

– Creates physic joints for line (Turn on draw mode to see the effects)

local Width = ractgangle_hit[i].width * 0.6
local Height = ractgangle_hit[i].height * 0.2

– Physic body for the line shape

local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height}
physics.addBody(ractgangle_hit[i], “static”, { bounce = -1, density=0.3, friction=0.7, shape = lineShape})
prevX = event.x
prevY = event.y
i = i + 1

elseif(event.phase==“ended”) then
tempLine.parent.remove(tempLine)
tempLine=nil
end
end
Runtime:addEventListener(“touch”, runTouch)[/lua]

Any assistance on how to combine these two functions into 1 working function will be much appreciated! [import]uid: 75779 topic_id: 32281 reply_id: 128953[/import]

Hi Russell,
Basically, what I’d do is this:

In the “moved” phase, check that the distance from the previous point in “sufficient”, using the distance check formula I provided. You can probably plug in the factors on Line 19 to get that distance. Currently, this function is drawing (well, appending actually) new segments to the line no matter if the move distance is 1 pixel or 100 pixels. That’s fine I suppose, but it adds ALOT of segments to the line, and you might not need it to be so “perfect”. By checking the distance, you could control if a new segment should be appended or not.

In contrast to this function’s behavior of drawing physics bodies as the user draws, I would suggest doing that process in a separate function AFTER the line is considered complete. You’d loop through the “ractgangle_hit” table and draw physics bodies in the method I described previously.

Out of curiosity, I don’t see where the index “i” is being reset. Can the user begin drawing a line, lift up the touch, and then continue drawing the line in another gesture? If the method should be a “draw a line in one gesture” kind of thing, then you must reset the counting variable after the touch is lifted and the line is effectively ended.

Hope this helps!
Brent
[import]uid: 9747 topic_id: 32281 reply_id: 129306[/import]

Hi Russell,
Basically, what I’d do is this:

In the “moved” phase, check that the distance from the previous point in “sufficient”, using the distance check formula I provided. You can probably plug in the factors on Line 19 to get that distance. Currently, this function is drawing (well, appending actually) new segments to the line no matter if the move distance is 1 pixel or 100 pixels. That’s fine I suppose, but it adds ALOT of segments to the line, and you might not need it to be so “perfect”. By checking the distance, you could control if a new segment should be appended or not.

In contrast to this function’s behavior of drawing physics bodies as the user draws, I would suggest doing that process in a separate function AFTER the line is considered complete. You’d loop through the “ractgangle_hit” table and draw physics bodies in the method I described previously.

Out of curiosity, I don’t see where the index “i” is being reset. Can the user begin drawing a line, lift up the touch, and then continue drawing the line in another gesture? If the method should be a “draw a line in one gesture” kind of thing, then you must reset the counting variable after the touch is lifted and the line is effectively ended.

Hope this helps!
Brent
[import]uid: 9747 topic_id: 32281 reply_id: 129306[/import]