local main = display.newGroup() local physics = require("physics") physics.start() physics.setGravity( 0, 0 ) physics.setDrawMode( "hybrid" ) local cub = display.newGroup() local cub = display.newRect(200, 200, 40, 40) cub.x = 205 --cub:setFillColor(255, 255, 255) physics.addBody(cub, { density = 1, friction = 0.5, bounce = 1.6 } ) cub.isSensor = true cub.id = "cub" main:insert(cub ) -- f\_sensor local f\_sensor = display.newRect(cub.x-15, cub.y-154, 30, 30) physics.addBody(f\_sensor, {density = 0.0001, friction = 1.0, bounce = 0.1}) f\_sensor.isFixedRotation = true f\_sensor.isSensor = true f\_sensor.sensor = "sensor" f\_sensor.alpha = 0 f\_sensor.id = "cub" main:insert(f\_sensor) local Pivot = physics.newJoint( "pivot", cub, f\_sensor, cub.x,cub.y) local x1,x2,y1,y2 = 0 local startX = 0 local startY = 0 local endX = 0 local endY = 0 local function distanceBetween(x1, y1, x2, y2) local dist\_x = x2 - x1 local dist\_y = y2 - y1 local distanceBetween = math.sqrt((dist\_x\*dist\_x) + (dist\_y\*dist\_y)) return distanceBetween end function onTouch(e) if(e.phase == "began") then startX = e.x startY = e.y center = display.newRect(e.x, e.y, 10, 10) elseif(e.phase == "moved") then endX = e.x endY = e.y distance = distanceBetween(startX, startY, e.x, e.y) rr2 = (math.atan2( endX-startX,endY-startY )\*180 / math.pi)-180 if distance \> 50 then distance = 50 end Dv\_x = math.cos( math.rad(rr2-90) )\*-distance/1\*2 Dv\_y = -math.sin( math.rad(rr2-90) )\*-distance/1\*2 cub.rotation = -rr2 f\_sensor.rotation = cub.rotation print("Dv\_y " .. Dv\_y) elseif(e.phase == "ended") then distance = 0 Dv\_x = 0 Dv\_y = 0 display.remove(center) end end Runtime:addEventListener("touch", onTouch) local startX = 0 local startY = 0 local endX = 0 local endY = 0 local t = 0 local crate = display.newRect( 0, 0, 50, 50 ) crate.x = 350 crate.y = 80 physics.addBody( crate, "static",{ density=3.0, friction=0.5, bounce=0.3 } ) main:insert(crate) local chaser = display.newRect(600, 50, 10, 10) physics.addBody(chaser, "dynamic", {isSensor = true, density = 1.0, friction = 1.0, bounce = 0.1}) main:insert(chaser) chaser.id = "cub" chaser.alpha = 0 -- start local from\_x = 0 local from\_y = 0 local to\_x = 200 local to\_y = 380 local hits = {} local ray = {} local obj\_x = {} local obj\_y = {} local gameloop = function(event) cub:setLinearVelocity(Dv\_x,Dv\_y) -- camera chaser.chaseSpeed = 6 local xDist = cub.x - chaser.x local yDist = cub.y - chaser.y chaser:setLinearVelocity(xDist \* chaser.chaseSpeed, yDist \* chaser.chaseSpeed) main.x = (chaser.x \* -1) + display.contentWidth / 2 main.y = (chaser.y \* -1) + display.contentHeight / 2 --n = n + 1 for i = 1, 50 do if ray[i] then -- Update the ray by removing the old one and creating a new one. display.remove( ray[i] ) end hits[i] = physics.rayCast( cub.x, cub.y, f\_sensor.x+(i\*6), f\_sensor.y, 1 ) if hits[i] and hits[i][1].object.id ~= "cub" then -- Draw a line to the first hit. obj\_x[i] = hits[i][1].position.x obj\_y[i] = hits[i][1].position.y ray[i] = display.newLine( cub.x, cub.y, obj\_x[i], obj\_y[i] ) else ray[i] = display.newLine( cub.x, cub.y, f\_sensor.x+(i\*6), f\_sensor.y) end ray[i].width = 5 main:insert(ray[i]) end end local timerUpd = timer.performWithDelay( 5, gameloop,-1 )
how to make a lantern? What would the beam is not distorted. Code is attached above.