Cc.exports.LineCollideRect (Startline,endline,rect)--detection of collisions between vectors and rectangles
--Get the four vertex positions of a rectangle
Local p = {CC.P (RECT.X,RECT.Y), CC.P (Rect.x + rect.width,rect.y), CC.P (Rect.x+rect.width,rect.y + rect.height), CC.P ( Rect.x,rect.y+rect.height)}
Local bRight = Pointposinvector (startline,endline,p[1])--the first point of the record rectangle is on the left or right side of the Ray
For i = 2,4 do--traversal of the other three points
If BRight ~= pointposinvector (startline,endline,p[i]) then--if the other vertices are not the same as the left and right edges of the first vertex, then the Rays collide with the rectangle to
return True
End
End
return False
End
function Cc.exports.PointPosInVector (startpos,endpos,v)--Determines the point at the left and right of the vector, true to the left, false to the right, and startpos as the starting point of the vector. Endpos is the end point of the vector, V is the vertex
if (startpos.x-v.x) * (ENDPOS.Y-V.Y)-(STARTPOS.Y-V.Y) * (endpos.x-v.x) > 0 Then
return True
End
return False
End
X-ray and Rectangle detection collision in 2d game