Raycasting. Thank You Corona!

Please make the code

Its not perfect but here it is.

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() local ground = display.newImage( "ground.png" ) ground.x = 160 ground.y = 445 physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } ) local crate1 = display.newImageRect( "crate.png",20,20 ) crate1.x = 30 crate1.y = 110 physics.addBody( crate1, { density=3.0, friction=0.5, bounce=0.3 } ) local crate2 = display.newImageRect( "crate.png",20,20 ) crate2.x = 90 crate2.y = 170 physics.addBody( crate2,"static", { density=3.0, friction=0.5, bounce=0.3 } ) local crate3 = display.newImageRect( "crate.png",20,20 ) crate3.rotation = 45 crate3.x = 25 crate3.y = 280 physics.addBody( crate3,"static",{ density=3.0, friction=0.5, bounce=0.3 } ) local crate4 = display.newImageRect( "crate.png",30,30 ) crate4.x = 280 crate4.y = 395 physics.addBody( crate4,"static",{ density=3.0, friction=0.5, bounce=0.3 } ) local from\_x = 0 local from\_y = 0 local to\_x =400 local to\_y = 480 local hits = {} local ray = {} local gameloop = function(event)     for i = 1, 200 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( from\_x, from\_y, to\_x-(i\*2), to\_y, 1 )     if hits[i] then         -- Draw a line to the first hit.         ray[i] = display.newLine( from\_x, from\_y, hits[i][1].x, hits[i][1].y )     else         -- no hit.         ray[i] = display.newLine( from\_x, from\_y, to\_x, to\_y )     end     ray[i].width = 2     end end Runtime:addEventListener("enterFrame", gameloop)

Icy Spark,  Thank you, we must also change here:

local gameloop = function(event) for i = 1, 200 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( from\_x, from\_y, to\_x-(i\*2), to\_y, 1 ) if hits[i] then -- Draw a line to the first hit. ray[i] = display.newLine( from\_x, from\_y, hits[i][1].x, hits[i][1].y ) else -- no hit. ray[i] = display.newLine( from\_x, from\_y, to\_x-(i\*2), to\_y ) end ray[i].width = 2 end end Runtime:addEventListener("enterFrame", gameloop)

We are waiting for the bug fixes you described Corona

Just wanted to let everyone know that rayCasting is working great now.  Thank you for your super quick bug fix Corona, and for adding the “normal”.  All is perfect!

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.

Hi All, in case anyone is trying to get the early examples in this forum to work. heres some code changes that work for me

[lua]local gameloop = function(event)

    for i = 1, 200 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( from_x, from_y, to_x-(i*2), to_y, 1 ) – depreciated

        hits[i] = physics.rayCast( from_x, from_y, to_x-(i*2), to_y, “closest” ) – NEW

        if hits[i] then

            – Draw a line to the first hit.

            – ray[i] = display.newLine( from_x, from_y, hits[i][1].x, hits[i][1].y ) – depreciated

            ray[i] = display.newLine( from_x, from_y, hits[i][1].position.x, hits[i][1].position.y ) – NEW

        else

            – no hit.

            – ray[i] = display.newLine( from_x, from_y, to_x, to_y )

            ray[i] = display.newLine( from_x, from_y, to_x-(i*2), to_y )

        end

        – ray[i].width = 2 – depreciated in Gpx2.0

        ray[i].strokeWidth = 2 

    end

end

Runtime:addEventListener(“enterFrame”, gameloop)[/lua]

Hi All, in case anyone is trying to get the early examples in this forum to work. heres some code changes that work for me

[lua]local gameloop = function(event)

    for i = 1, 200 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( from_x, from_y, to_x-(i*2), to_y, 1 ) – depreciated

        hits[i] = physics.rayCast( from_x, from_y, to_x-(i*2), to_y, “closest” ) – NEW

        if hits[i] then

            – Draw a line to the first hit.

            – ray[i] = display.newLine( from_x, from_y, hits[i][1].x, hits[i][1].y ) – depreciated

            ray[i] = display.newLine( from_x, from_y, hits[i][1].position.x, hits[i][1].position.y ) – NEW

        else

            – no hit.

            – ray[i] = display.newLine( from_x, from_y, to_x, to_y )

            ray[i] = display.newLine( from_x, from_y, to_x-(i*2), to_y )

        end

        – ray[i].width = 2 – depreciated in Gpx2.0

        ray[i].strokeWidth = 2 

    end

end

Runtime:addEventListener(“enterFrame”, gameloop)[/lua]