[Cson original] Collision Detection on rotating rectangles

Source: Internet
Author: User

You can find many methods for rotating rectangular Collision Detection on the Internet.ArticleThis collision detection was analyzed: the OBB collision detection. This article is mainly to add some of its own analysis of the details, combined with some other information on the Internet on the rotating rectangular collision detection, and discuss with you the technical details of the implementation.

First, we recommend a reference material, which provides a good explanation for rotating rectangular Collision Detection and is also applicable to other polygon collision detection. Click to download it.

Demo preview:

(Drag the bar to adjust the Rotation Angle of the rectangle, and click the mouse to move the red rectangle,The demo is relatively simple. Let's take a look at it. Just make it clear ~)

60 30

 

 

 

 

 

 


Implementation principle:

If two polygon have an axis, so that the projection of the two polygon on the axis does not overlap, there is no collision between the polygon.

All possible axes are axes perpendicular to each side of a polygon.

 

Steps:

Step 1: Obtain the two axes of the rectangle.

 

Because the sides of the rectangle are parallel to each other, the two sides in Parallel Share a axis perpendicular to them. Therefore, for each rectangle, there are only two axes to be used for detection. We only need to check whether the projection on the axis of another rectangle overlaps with the axis. For convenience, we can take the two sides adjacent to the rectangle as two axes, and then compare the two axes with the four sides of the other rectangle for overlap:

 

VaRLinesarr1 = getfourlines (rect1.pointsarr );//Four Edges of rectangle 1VaRLinesarr2 = getfourlines (rect2.pointsarr );//Four Edges of rectangle 2//Two adjacent edges of a rectangle are used as two axes and compared with the four edges of the other rectangle.If(Callback (linesarr2 [0], linesarr1) & detectaxiscollision (linesarr2 [1], linesarr1) & callback (linesarr1 [0], linesarr2) & callback (linesarr1 [1], Linesarr2 )){Return True;}Return False;

 

Step 2: Obtain the projection of the axis on the vector andReturns the projection of each side of another rectangle on the axis.

 

To obtain the projection of a line segment on the axis, we need to divide it into the projection of two vertices of the calculated line segment on the axis. How do I calculate the projection of points on the Axis? The formula is attached here:

 

 
//Projection of vertices on the AxisVaRX = (P [0] * axis [0] + P [1] * axis [1]) /(axis [0] * axis [0] + axis [1] * axis [1]) * axis [0];VaRY = (P [0] * axis [0] + P [1] * axis [1]) /(axis [0] * axis [0] + axis [1] * axis [1]) * axis [1];

P is the point to be projected, and axis is the axial volume of the projection target. Since we have previously used the adjacent two sides of the rectangle as the axis of the rectangle, the calculation of the axial axis can be:

 

 
VaRAxis = [L [1] [0]-l [0] [0], L [1] [1]-l [0] [1];//Axial Volume l is the axis of the rectangle

Calculate the projection of two points to obtain the projection of a line segment.

 

Step 4: Check whether the projection of each side on the vector overlaps with that of the axis on the vector.

 

How do I detect overlapping line segments? Because the two line segments are projected on the same axial volume, they must be parallel, so the discriminant method is relatively simple. The method provides one: the x-axis coordinates of the Line Segment endpoint are subtracted from the X-axis coordinates of the two endpoints of the other line segment, and the two results are multiplied. If the result is smaller than 0, it is proved that the line segments overlap (when the two line segments are vertical, the Y axis coordinates of the endpoint are used for judgment ):

 

 VaR Islineoverlap = Function (L1, L2 ){ //  Determine whether line segments overlap          VaR L1p1 = L1 [0], l1p2 = L1 [1], l2p1 = L2 [0], l2p2 = L2 [1 ];  If (L1p1 [0]! = L2p1 [0]) { //  Non-vertical X-axis line segments         If (L1p1 [0]-l2p1 [0]) * (l1p1 [0]-l2p2 [0]) <0 | (l1p2 [0]-l2p1 [0]) * (l1p2 [0]-l2p2 [0]) <0 | (l2p1 [0]-l1p1 [0]) * (l2p1 [0]-l1p2 [0]) <0 | (l2p2 [0]-l1p1 [0]) * (l2p2 [0]-l1p2 [0]) <0 ){  Return   True  ;}}  Else { //  Vertical X axis          If (L1p1 [1]-l2p1 [1]) * (l1p1 [1]-l2p2 [1]) <0 | (l1p2 [1]-l2p1 [1]) * (l1p2 [1]-l2p2 [1]) <0 | (l2p1 [1]-l1p1 [1]) * (l2p1 [1]-l1p2 [1]) <0 | (l2p2 [1]-l1p1 [1]) * (l2p2 [1]-l1p2 [1]) <0 ){ Return   True  ;}}  Return   False  ;} 

 

Step 5: If the projection of one axis on the vector and the projection of each side of the other rectangle on the vector do not overlap, there will be no collision. Otherwise, a collision will occur.

As long as we detect that there is no overlap between the projection of the four sides of one axis and the other rectangle on this axis, we can end the judgment and return no collision:

//Two adjacent edges of a rectangle are used as two axes and compared with the four edges of the other rectangle.If(Callback (linesarr2 [0], linesarr1) & detectaxiscollision (linesarr2 [1], linesarr1) & callback (linesarr1 [0], linesarr2) & callback (linesarr1 [1], Linesarr2 )){Return True;}Return False;

 

 

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.