X-ray and Rectangle detection collision in 2d game

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.