Collision Detection of Game Genie under j2s

Source: Internet
Author: User
Collision Detection, also known as conflict detection, is a method to determine whether the elves conflict with each other. Collision Detection is used to determine when a physical interaction occurs between Elves. For example, in a game, if the player genie collide with the little plane genie, the player's life will be reduced. Collision detection is a mechanism used to detect the collision between a player and an enemy (or a bullet. There are many ways to handle Collision Detection in game design. The simplest way is to compare the border of each sprite's rectangle with that of other genie. This method is very efficient, but there is also a problem. For example, not all genie are rectangular. When they miss each other, it may cause some errors. Conflicts may occur due to overlapping corners, but actually only transparent areas. The less the shape of the Sprite is close to the rectangle, the more error types there will be. It can be imagined how simple and not accurate the rectangular collision detection, unless the genie originally used is just a rectangle. One way to improve this technology is to narrow down conflicting rectangles to reduce errors. This method slightly improves the situation, but may lead to other potential errors, that is, allow the genie to overlap in some cases without sending a conflict signal. The rectangular collision after shrinking is as effective as the simple rectangular collision, because all we do is to compare whether the rectangles are intersecting. Currently, the most effective collision detection is based on the sprite image data to detect the collision (I .e., the pixel detection method). This method is not actually used to check whether the transparent regions of the genie overlap, but whether the sprite images overlap. In this case, only when the sprite image is duplicated will a collision conflict be obtained. This is an ideal collision detection technology because it is very accurate and
Moving objects of any shape will not cause errors. Without the collision detection algorithm, the simulation of moment-shaped collision detection will have a terrible situation during the running of the game, although the background can be moved, and the effect of a player's plane, and the player can also control the forward direction of the body, but there will be a cause of defect-when the enemy plane is hit by our plane, there is no response, the enemy plane is still running, and the score does not increase. It seems that nothing has happened. This is because no collision detection code is added for the game. Collision detection is an important algorithm frequently used in games. It checks whether two objects in the game have surface contact. For example, whether a bullet hits the enemy plane or whether the enemy plane and the player plane collide. Only the collision detection can cause the attacker to be hit and the score is increased. You can use a series of methods in the Sprite class to use the midp api for collision conflicts. All these methods are called collidesWith (). The collidesWith () method includes three methods. The first collidesWith () method detects the collision between two elves: collidesWith (Sprite s, Boolean pixelLevel); to detect a conflict, you only need to call this method on the genie and pass in another genie. The 2nd parameters determine whether conflict detection is based on the "pixel-connected pixel" method, which is equivalent to the image data detection method. If you only want to use the simple rectangle detection method to detect Between Two elves, you can pass the false value to the collidesWith () method. The other two methods use an image and a tiled layer to detect conflicts between a genie. When an image is used, the XY position of the image is specified, and collision detection is performed on the image and genie. A tiled layer is similar to a Sprite, but it uses multiple tiled images to create a large synthetic image. For example, it uses a tiled layer to create a wall-filled maze, then it detects conflicts between one of the genie to ensure that the genie is within the range. The code for implementing collision detection is as follows:

/* Determine whether enemyShip is hit by a player bullet */
For (int I = 0; I <MAX_SHOT; I ++)
{
For (int j = 0; j <MAX_enemyShip; j ++)
{
If (playerShot [I]. isVisible ())&&
(PlayerShot [I]. collidesWith (enemyShip [j], true )))
{

...... // Collision handling

}
}
}

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

// Check whether a player has collided with a plane
For (int I = 0; I <this. MAX_SP; I ++)
If (smallPlane [I]. isVisible ()&&
SmallPlane [I]. collidesWith (playerSprite, true ))
{

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

}

This article is from the "Nanhu miner technology space" blog, please be sure to keep this source http://gaochaojs.blog.51cto.com/812546/187185

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.