ray casting

local physics=require("physics") physics.start() physics.setGravity( 0, 0 ) local beamGroup=display.newGroup() local maxBeam=50 local function clearObject( object ) display.remove( object ) object = nil end local function resetBeams() -- Clear all beams/bursts from display for i = beamGroup.numChildren,1,-1 do local child = beamGroup[i] display.remove( child ) child = nil end end local function drawBeam( startX, startY, endX, endY ) -- Draw a series of overlapping lines to represent the beam local beam1 = display.newLine( beamGroup, startX, startY, endX, endY ) beam1.strokeWidth = 2 ; beam1:setStrokeColor( 1, 0.312, 0.157, 0.9 ) ; beam1.blendMode = "add" ; beam1:toBack() local beam2 = display.newLine( beamGroup, startX, startY, endX, endY ) beam2.strokeWidth = 4 ; beam2:setStrokeColor( 1, 0.312, 0.157, 0.706 ) ; beam2.blendMode = "add" ; beam2:toBack() local beam3 = display.newLine( beamGroup, startX, startY, endX, endY ) beam3.strokeWidth = 6 ; beam3:setStrokeColor( 1, 0.196, 0.157, 0.392 ) ; beam3.blendMode = "add" ; beam3:toBack() end local function castRay(startX,startY,endX,endY) local hit=physics.rayCast( startX, startY, endX, endY,"closet" ) -- There is a hit; calculate the entire ray sequence (initial ray and reflections) if ( hits and beamGroup.numChildren \<= maxBeams ) then -- Store first hit to variable (just the "closest" hit was requested, so use 'hits[1]') local hitFirst = hits[1] -- Store the hit X and Y position to local variables local hitX, hitY = hitFirst.position.x, hitFirst.position.y -- Draw the next beam drawBeam( startX, startY, hitX, hitY ) -- Check for and calculate the reflected ray local reflectX, reflectY = physics.reflectRay( startX, startY, hitFirst ) local reflectLen = 1600 local reflectEndX = ( hitX + ( reflectX \* reflectLen ) ) local reflectEndY = ( hitY + ( reflectY \* reflectLen ) ) -- If the ray is reflected, cast another ray if ( reflectX and reflectY ) then timer.performWithDelay( 40, function() castRay( hitX, hitY, reflectEndX, reflectEndY ); end ) end -- Else, ray casting sequence is complete else -- Draw the final beam drawBeam( startX, startY, endX, endY ) -- Fade out entire beam group after a short delay transition.to( beamGroup, { time=800, delay=400, alpha=0, onComplete=resetBeams } ) end end local ship =display.newRect(display.contentWidth/2,display.contentHeight/2+200,60,20) ship:setFillColor(1,0.3,0.6,0.5) physics.addBody( ship,"static" ) local function fireLaser( event ) castRay(ship.x,ship.y,ship.x,ship.y-1000) end local mirror=display.newRect(display.contentWidth/2,0,display.contentWidth,40) physics.addBody( mirror,"static" ) mirror:setFillColor(0,1,0,1) mirror.rotation=45 --physics.addBody( mirror,"dynamic" ) ship:addEventListener("tap",fireLaser)

I created laser and mirror but the mirror is not reflecting the ray instead it is just passing I can’t figure out the problem,help me out 

You’ve got quite a few issues there.

You’ve made a lot of types. For instance, you’ve set the rayCast’s behaviour to “closet” instead of “closest”. Then, the very same row, you have “local hit”, but two rows later you are checking if “hits” exists. You’ve created “hit”, not “hits”, so that check can never be true. Because of this, your function always jumps to the “else” part.

Now, I’m not sure if there are other mistakes, but it is possible. Also, I’m not sure if your code looks wonky just because how you copy pasted it here, but just in case, pay attention to proper code indentation to improve the readability of your code. It’ll make it easier for you and everyone else to understand it and to catch issues.

Hi, thanks the program worked but since I want to make a game the laser should fire whenever I tap ii however it only works only first time and but when I tap it second time it doesn’t work and yes apart from the mistakes you pointed there was one more maxBeams and maxBeam  .

While copying sample projects and tutorials can be great for learning how to develop games, it is only useful if you actually take the time to study and understand what it is that you are copying and why it works as it does.

Your problem lies here:

transition.to( beamGroup, { time=800, delay=400, alpha=0, onComplete=resetBeams } )

When you need to debug your code and there isn’t a clear crash, then just follow your code in the order that it runs until you find possible issues. In this case, the issue is that either new rays aren’t cast or they are invisible. In the aforementioned line of code, you transition the group’s alpha to zero, i.e. you make the group and everything in it invisible. Your function fires exactly as it should, but you just need to reset the alpha to one somewhere, otherwise everything you add there will remain invisible. The most logical place for this, i.e. beamGroup.alpha = 1,  would be at the end of your resetBeams function.

You’ve got quite a few issues there.

You’ve made a lot of types. For instance, you’ve set the rayCast’s behaviour to “closet” instead of “closest”. Then, the very same row, you have “local hit”, but two rows later you are checking if “hits” exists. You’ve created “hit”, not “hits”, so that check can never be true. Because of this, your function always jumps to the “else” part.

Now, I’m not sure if there are other mistakes, but it is possible. Also, I’m not sure if your code looks wonky just because how you copy pasted it here, but just in case, pay attention to proper code indentation to improve the readability of your code. It’ll make it easier for you and everyone else to understand it and to catch issues.

Hi, thanks the program worked but since I want to make a game the laser should fire whenever I tap ii however it only works only first time and but when I tap it second time it doesn’t work and yes apart from the mistakes you pointed there was one more maxBeams and maxBeam  .

While copying sample projects and tutorials can be great for learning how to develop games, it is only useful if you actually take the time to study and understand what it is that you are copying and why it works as it does.

Your problem lies here:

transition.to( beamGroup, { time=800, delay=400, alpha=0, onComplete=resetBeams } )

When you need to debug your code and there isn’t a clear crash, then just follow your code in the order that it runs until you find possible issues. In this case, the issue is that either new rays aren’t cast or they are invisible. In the aforementioned line of code, you transition the group’s alpha to zero, i.e. you make the group and everything in it invisible. Your function fires exactly as it should, but you just need to reset the alpha to one somewhere, otherwise everything you add there will remain invisible. The most logical place for this, i.e. beamGroup.alpha = 1,  would be at the end of your resetBeams function.