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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 |
One problem I see in your code is trying to change the angle of a physics object (newLine). Once you do an physics.addBody on a Display Object, you can no longer change it properties with normal Display Objects APIs. The object is now a physics object and the physics engine does not track any property changes (e.g. rotation) you make.
-Tom