Analysis of basic collision detection algorithm in J2ME

Source: Internet
Author: User

In the game, often need to carry out collision detection, such as to determine whether the previous obstacles and whether the bullets hit the aircraft, are to detect whether two objects collide, and then according to the results of the test to make a different treatment. Collision detection of the object may be some shape and complexity, these need to be combined collision detection, that is, the complex object processing into one of the basic shape of the combination, and then the different detection.

Here is a brief introduction to the two most basic shapes when the collision process.

1. The rectangle and the rectangle collide

General rules of object collisions can be processed into a rectangular collision, the principle is to detect whether two rectangles overlap. We assume that the parameters of rectangle 1 are: the coordinates of the upper-left corner are (x1,y1), the width is w1, the height is H1, and the parameters of Rectangle 2 are: the coordinates of the upper-left corner are (x2,y2), the width is W2, and the height is H2.

The relationship between the distance and the width of the coordinates of the center point in the X and Y directions can be processed mathematically when testing. That is, the absolute value of the distance of the two rectangular center point in the x direction is less than equal to the rectangle width and one-second, while the absolute value of the distance in the y direction is less than one-second of the rectangle height. Here is the mathematical expression:

X Direction: | (x1 + w1/2) – (x2 + W2/2) | < | (W1 + w2)/2|

Y direction: | (Y1 + h1/2) – (y2 + H2/2) | < | (H1 + H2)/2|

In a program, you can do this by simply translating the above conditions into code.

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

Here is a brief introduction to the circular collision.

2, round and round collision

The circular and circular collisions should be said to be one of the simplest collisions, because in mathematics there is a formula for calculating the distance between the two centers, for the two circles to overlap. Then the condition becomes: calculates whether the distance between the two centers is less than the radius of two circles.

Suppose the upper-left corner of the Circle 1 is (x1,y1), the radius is R1, the coordinates of the upper-left corner of the Circle 2 are (x2,y2) and the radius is R2.

Because there is no floating-point number in the MIDP1.0 and the floating-point number is slow, we make a simple transformation of the condition: the square is squared on both sides of the condition, so that the arithmetic steps are removed.

Here is the mathematical expression:

(X1–X2) 2 + (Y1–y2) 2 < (r1 + R2) 2

In a program, you just have to convert the above conditions into code.

The above mentioned is only the most basic collision detection algorithm implementation, and the actual programming process encountered collision detection problem is much more complex than these, but also need other forms of detection, but also need to do more in-depth study.

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.