Basic Collision Detection Algorithm in j2s

Source: Internet
Author: User

Author: Chen yuefeng

From:Http://blog.csdn.net/mailbomb

 

In the game, collision detection is often required. For example, to determine whether there are obstacles in the front and whether a bullet hits the plane, it is to detect whether two objects collide, then, different processing is performed based on the detection results.

Collision detection objects may have some shapes and complexities. These require combined collision detection, which means to combine complex objects into a combination of basic shapes and then perform different detection.

The following describes how to deal with the collision of the two most basic shapes.

1, Rectangular and rectangular collision

Generally, an object collision rule can be processed as a rectangular collision. The principle is to check whether two rectangles overlap. Let's assume that the rectangle1The coordinates in the upper left corner are(X1, Y1), Width isW1, The height isH1; Rectangle2The coordinates in the upper left corner are(X2, Y2), Width isW2, The height isH2.

During the detection, the coordinates of the center point can be processed in MathematicsXAndYThe relationship between the distance and the width in the direction. That is, the centers of the two rectangles areXThe absolute value of the distance is less than or equal to 1/2 of the width andYThe absolute value of the distance is less than or equal to 1/2 of the height of the rectangle. The following is a mathematical expression:

XDirection:| (X1 + W1/2)-(X2 + W2/2) | <| (W1 + W2)/2 |

YDirection:| (Y1 + H1/2)-(y2 + H2/2) | <| (H1 + H2)/2 |

InProgram, You only need to convert the above conditionsCodeYou can achieve it.

But rectangular collision is just a rough collision detection method, because many actual objects may not be a regular rectangle.

Next we will introduce round collision.

2Round and circular collision

The collision between a circle and a circle is the simplest collision, because in mathematics, there is a formula for calculating the distance between the two circles if they overlap. Then the condition is: calculate whether the distance between the two centers is smaller than the radius and of the two circles.

Assume the circle1The coordinate in the upper left corner is(X1, Y1), Radius isR1, Circle2The coordinates in the upper left corner of is(X2, Y2), Radius isR2.

BecauseMidp1.0There is no floating point number, and the floating point operation is slow, so we make a simple transformation to the condition: the two sides of the condition are square, so that the operation steps of the open side are removed.

The following is a mathematical expression:

(X1-X2)2 + (Y1-Y2) 2 <(R1 + R2) 2

In the program, you only need to convert the above conditions into code.

The most basic collision detection is described above.AlgorithmBut the collision detection problems encountered in the actual programming process are much more complicated than these, and other forms of detection are required, and more in-depth learning is required.

 

Email: cqucyf@263.net

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.