Hi, ive been working on code to draw a STRAIGHT line when you swipe across the screen that has static physics attached to it. I wanted my physics bodies to be as precise on the object as possible so here’s what i came up with ( the triangle is for reference)
[lua] local function drawLine(event)
local angle = 0
if (event.phase == “ended”) then
local a = event.x - event.xStart
local b = event.y - event.yStart
local c = (a^2 + b^2)^(1/2)
local sideA = display.newLine(event.xStart,event.yStart,event.x,event.yStart)
local sideB = display.newLine(event.x,event.yStart,event.x,event.y)
local hyp = display.newLine(event.xStart,event.yStart,event.x,event.y)
angle = math.deg(math.acos(a/c))
newLine = display.newRect(event.xStart,event.yStart,c,3)
newLine:setReferencePoint(display.TopLeftReferencePoint)
if (event.y < event.yStart) then
newLine.rotation = angle*-1
physics.addBody(newLine,“static”,{density = 1,friction = 0})
end
if (event.y > event.yStart) then
newLine.rotation = angle
physics.addBody(newLine,“static”,{density = 1,friction = 0})
end
end[/lua]
everything works how i wanted to except when i had another object fall from the screen it got kind of weird. The more vertical i drew the line, the farther and farther away my object would actually bounce of something with physics. The strangest thing about it is when i checked my physics body using draw mode, the physics rectangle was right on my line and not somewhere else. Why is the physics not where it should be and why is draw mode telling me everything is alright? [import]uid: 80180 topic_id: 14926 reply_id: 314926[/import]