Java for accurate collision detection.

Source: Internet
Author: User
Tags abs

CNMM22 original.

[http://blog.csdn.net/cnmm22/article/details/45220551]

In my approach, you can implement precise bevel rectangles, parallelogram, irregular rectangles, irregular polygons, and round collision detection in Java.

We know that in Java there is a class, X.getrect (). intersects (X1.getrect () can implement collision detection for a regular rectangle:

This is an intolerable "collision detection". This is not the game effect I want.

After making my method:

CNMM22 original, reproduced please casually.

Implementation method: We have to divide all collisions into two categories: with circles and with polygons.

Round and Round solutions: detects the center distance of two colliding objects, compared to the sum of the two radii.

I've encapsulated all the methods I want to use so that you can use them directly:

    /**  步骤一、检测两个圆心距离 */    publicstaticdoublegetDistancedoubledouble oy) {        double _x = Math.abs(ox - p.x);        double _y = Math.abs(oy - p.y);        return Math.sqrt(_x * _x + _y * _y);    }

Each round transparent PNG round picture, we have a size, we get its radius by size.

and the two center coordinates of the collision we are very good to get, this believe I do not need to explain.

/* Step two, detect two center distance, and compare the radius of the two, sample code: /
.
.
.
.

            Point p1 = new Point(x + W / 2, y + H / 2);            if (Unit.getDistance(p1, w.yx, w.yy) < w.yr) {

.
.
.
.

The above completes the round collision detection.

Polygon: Sao complex point,.

Solution: 1, stroke, 2, detect side to point distance.

For example, this graph:

First, 4 points are traced so that the 4 dots can be combined into 4 line segments. The distance between each segment and the center of the object is detected separately.

Example code:
............................

img = tk. GetImage(Wall. Class. getClassLoader(). GetResource("Images/4/k8.png"));RX1 =1;RX2 =14784;W =168;h = the;RX3 =2;x3 =x;Y3 =y+5;W3 = W- the;H3 = h-5;px11 =x+ -;px12 =y+ *;px21 =x+136;px22 =y+7;Px31 =x+4;px32 =y+ -;px41 =x+149;px42 =y+ -;

............................

px11,px12, is its first point, px21,px22, is its second point, and so on. X, Y is the top-left drop point of the vector graph, no matter whether that point has pixels or transparent, it's X, Y. This way through the image tool, we can easily get a set of PX1-PXN, Px12-pxn2.

Sample code: (detects the distance between each segment and the center of the object, respectively)

if (Unit.pointToLine(w.px11, w.px12, w.px21, w.px22, x + W / 2, y + H / 2) < 24) {                stay();                return true;            }            if (Unit.pointToLine(w.px11, w.px12, w.px31, w.px32, x + W / 2, y + H / 2) < 24) {                stay();                return true;            }            if (Unit.pointToLine(w.px31, w.px32, w.px41, w.px42, x + W / 2, y + H / 2) < 24) {                stay();                return true;            }            if (Unit.pointToLine(w.px21, w.px22, w.px41, w.px42, x + W / 2, y + H / 2) < 24) {                stay();                return true;

where x + w/2, y + h/2, is the point coordinate, 24 is the detection distance. These are just examples.

    /** Package Method: Detect edge to point distance */    Static DoublePointtoline (intX1,intY1,intX2,intY2,intX0,inty0) {DoubleSpace =0;DoubleA, B, C; A = Linespace (x1, y1, x2, y2);//Length of line segmentb = Linespace (x1, y1, x0, y0);//(X1,Y1) distance to the pointc = Linespace (x2, y2, x0, y0);//(X2,Y2) distance to the point        if(c <=0.000001|| b <=0.000001) {space =0;returnSpace }if(A <=0.000001) {space = B;returnSpace }if(c * C >= A * a + b * b) {space = B;returnSpace }if(b * b >= A * a + c * c) {space = C;returnSpace }Doublep = (A + B + c)/2;//half perimeter        Doubles = math.sqrt (P * (p-a) * (p-b) * (p-c));//Helen formula to find areaSpace =2* S/A;//Return point-to-line distance (using triangular area formula for height)        returnSpace }

In any case, to solve polygon collision detection, strokes will be a considerable amount of work, but once used skillfully, it is actually quite simple. Because there are graphical tools.

This quickly gets px11,px12,px21,px22; px31,px32,px41,px42 is similar.

This completes the collision detection of irregular rectangles.

How does that polygon measure?

Polygon method is similar, multi-stroke several side out, and then distinguish the point to line distance. I'm not going to repeat it.

What if the object to be detected is an ellipse?

You can refer to this method:

Remember, the more edges you stroke, the higher the accuracy, the greater the effort.

Using my method, you can maximize the accuracy of collision detection, and avoid the huge performance loss caused by pixel-level collision detection, if the collision object is a polygon collision with a polygon, or a polygon collides with a curve, it is not possible to use my method to detect, in fact, this collision is very rare, in this case, Can build a pixel matrix, general practice such as a 200*200 pixel picture, we need to do a [200]*[200] array, about 40,000 times to complete the 1-frame drop collision detection, but also to consider the transparency of the calculation, its performance consumption is considerable.

Java for accurate collision detection.

Related Article

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.