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