The Nineth chapter of Flash Theory course collision Detection Ⅱ

Source: Internet
Author: User

Back to "flash Basic Theory Class-Catalog"

Distance Collision Detection

At the beginning of this section, we get rid of the built-in HitTest method, but the collision detection in our own hands. This is to use the distance between two objects to determine the collision occurred.

For example, if your car is 100 meters away from my car, we know the two cars are far enough away to collide. However, if our car is 6 meters wide and 12 meters long, and the center of my car and the center of your car is only 5 meters, then there will be some metal bends, the insurance policy will be longer. In other words, the two cars could not be together unless some part of the car was knocked out. This is the idea of the whole distance collision detection. We want to confirm the minimum distance between the two objects, and then look at the current distance to compare the size of the two. If the current distance is less than the minimum distance, it is known that there is a collision between the objects.

The Hittestobject method works best on rectangles, but degrades when dealing with other graphics, and our approach works best when working with circles. If the shape of the processing is biased with the circle, the accuracy will be reduced. But there is a problem with the rectangular boundary in the HitTest: when there is a collision, there is no response, because the center point is not close enough.

Simple Distance Collision detection

Let's start with the ideal state: two circles. You can still use the ball class. (You may now understand why the term "reuse" is often associated with object-oriented programming.) )

In the application of distance collision detection, the registration point of the circle should be at the center point, the ball class exactly meets the requirements. First, create two balls and set one of them to be drag-and-drop. Then the collision detection is carried out in the Enterframe function. So far, the procedure is the same as the first example in this chapter. Only when judging collisions, not using if (Ball1.hittestobject (BALL2)), but judging the distance in the IF statement. We have learned how to calculate the distance between two objects, and recall the Pythagorean theorem in chapter three. So, the beginning of the program should be this:

var dx:Number = sprite2.x - sprite1.x;
var dy:Number = sprite2.y - sprite1.y;
var dist:Number = Math.sqrt(dx * dx + dy * dy);

OK, now the distance has been, how to judge it? Take a look at Figure 9-4.

Fig. 9-4 the distance of the collision

We see two movies collide, each one 60 pixels wide, then each radius is 30. So, when they collide, the actual difference is 60 pixels. Ah ha! That's the answer. For two circles of the same size, a collision will occur if the distance is less than the diameter. This example code (DISTANCE.AS) is very similar to objecthistest.as, and the Onenterframe method is set:

private function onEnterFrame(event:Event):void {
 var dx:Number = ball2.x - ball1.x;
 var dy:Number = ball2.y - ball1.y;
 var dist:Number = Math.sqrt(dx * dx + dy * dy);
 // 默认 ball 的直径为 80 (半径为 40)
 if (dist < 80) {
  trace("hit");
 }
}

After testing, we found that the results of this collision were not related to the angle near the ball. No collisions will occur without touching the target ball. But it's obviously not appropriate to use numeric values in your code, because in that case, you'll have to modify the code every time you change the size of the ball. Besides, what if the size of the two balls is different? We need to abstract this method into formulas that can be adapted to any situation.

As shown in Figure 9-5. Two different sizes of ball, colliding with each other. The ball on the left is 60 pixels, 40 pixels to the right. We can use the procedure to inspect their width properties. The first ball has a radius of 30 and another radius of 20. Therefore, the distance they collide should actually be 50. Within the ball class, radius (RADIUS) attributes have been set and can be judged directly.

Fig. 9-5 The collision distance of two different volume objects

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.