This is where I am now. I can’t seem to get the physics body to join the points correctly though. Definitely a work a progress and pretty messy. I took it out of my game and put it into its own main.lua file if you want to have a go at making it work.
[code]
–main.lua
require “physics”
obj = {}
obj.drawing = {}
distanceBetween = function(point1, point2)
local deltaX = point1.x - point2.x
local deltaY = point1.y - point2.y
return math.ceil(math.sqrt(deltaX * deltaX + deltaY * deltaY))
end
draw = function(e)
local function disallowErase(platform)
platform.allowErase = false
end
function clickListener(e)
if obj.touchMode == “erase” then
platformTouch = e.target
platform = e.target.platform
if platform.allowErase then
obj.playerInk = obj.playerInk + platform.distance
obj:updateInkCount()
platform:removeSelf()
platformTouch:removeSelf()
end
end
end
– print(e.phase)
if e.phase == “began” then
obj.drawing.points = {}
obj.drawing.count = 0
obj.drawing.phys = {}
local xCoord, yCoord = e.x, e.y
local point = {x = xCoord, y = yCoord }
obj.drawing.points[obj.drawing.count] = point
obj.drawing.phys[obj.drawing.count] = { one = point, two = point }
elseif e.phase == “moved” then
if not obj.drawing.count then
obj.drawing.count = 0
obj.drawing.points = {}
end
– print(“Count: " … obj.drawing.count)
local xCoord, yCoord = e.x, e.y
local newPoint = { x = xCoord, y = yCoord }
local oldPoint = obj.drawing.points[obj.drawing.count]
local distance = distanceBetween(oldPoint, newPoint)
if distance < 10 then
return false
end
print(”-" … obj.drawing.count)
obj.drawing.count = obj.drawing.count + 1
obj.drawing.points[obj.drawing.count] = {x = xCoord, y = yCoord }
local platform = display.newLine(oldPoint.x, oldPoint.y, newPoint.x, newPoint.y)
platform.width = 9
platform.alpha = .2
local halfDeltaX = math.abs((newPoint.x - oldPoint.x)) * .5
local halfDeltaY = math.abs((newPoint.y - oldPoint.y)) * .5
local halfWidth = platform.width / 9
local phyX, phyY = halfDeltaX, halfDeltaY
if phyX < 3 then
phyX = 3
end
if phyY < 3 then
phyY = 3
end
local x1, y1 = -phyX, -phyY – -halfDeltaX, -halfWidth
local x2, y2 = phyX, -phyY – halfDeltaX, -halfWidth
local x3, y3 = phyX, phyY – halfDeltaX, halfWidth
local x4, y4 = -phyX, phyY – -halfDeltaX, halfWidth
–local platformShape = { x1,y1,x2,y2,x3,y3,x4,y4 }
–local platformFilter = { categoryBits = 1, maskBits = 6}
–physics.addBody(platform, “static”, { shape = platformShape, friction = .15, bounce = .1, filter = platformFilter })
obj.drawing.points[obj.drawing.count].segment = platform
local x2,y2 = platform:localToContent(x2,y2)
local x4,y4 = platform:localToContent(x4,y4)
local pointOne = { x = x2, y = y2 }
local pointTwo = { x = x4, y = y4 }
obj.drawing.phys[obj.drawing.count] = { one = pointOne, two = pointTwo }
return true
– print(drawing.points[drawing.count].x … " " … drawing.points[drawing.count].y)
– print(drawing.points[drawing.count-1].x … " " … drawing.points[drawing.count-1].y)
elseif e.phase == “ended” then
local physicsShape = {}
local segment = obj.drawing.points[1].segment
print("Number of points " … #obj.drawing.points)
for i=1, #obj.drawing.points do
local x, y = segment:contentToLocal(obj.drawing.phys[i].one.x, obj.drawing.phys[i].one.y)
table.insert(physicsShape, x)
table.insert(physicsShape, y)
local a,b = segment:localToContent(x,y)
local test = display.newCircle(a,b,2,2)
test:setFillColor(255,0,0)
– print("{"…x…", “…y…”)")
end
–print("---------")
local number = #obj.drawing.points
for i=0, number -1 do
local x, y = segment:contentToLocal(obj.drawing.phys[i].two.x, obj.drawing.phys[i].two.y)
table.insert(physicsShape, x)
table.insert(physicsShape, y)
local a,b = segment:localToContent(x,y)
local test = display.newCircle(a,b,2,2)
test:setFillColor(0,0,255)
–print("{"…x…", “…y…”)")
end
–for k,v in pairs(physicsShape) do print(k,v) end
–print("Physics points " … #physicsShape/2)
physics.addBody(segment, “static”, { shape = physicsShape })
end
end
local function main()
physics.start()
physics.setDrawMode(“hybrid”)
Runtime:addEventListener(“touch”, draw)
return true
end
main()
[/code] [import]uid: 49205 topic_id: 9644 reply_id: 39875[/import]