Collision detection of Game elves under the J2ME

Source: Internet
Author: User

Collision detection, also known as conflict detection, is a way to determine whether the elves are conflicting with each other. Collision detection is used to determine when physical interactions occur between elves. For example, in a game, the player's life will be reduced if the player's sprite collides with a small-plane sprite. Collision detection is a mechanism for detecting whether a player has collided with an enemy (or a bullet). In game design, there are many ways to handle collision detection. The easiest way to do this is to compare the rectangular boundaries of each sprite with the rectangular boundaries of other sprites. This method is very efficient, but there is also a problem, such as not all the elves are rectangular, when they are relatively close to miss each other, may cause a certain degree of error. It is possible that the overlap of corners causes conflicts, but in fact only the overlapping of transparent areas. The less the sprite's shape is closer to the rectangle, the more error types it will have. It can be imagined how simple and imprecise the rectangular collision detection is, unless the originally used sprite is rectangular. One way to improve the technology is to narrow the conflict rectangle to reduce the error. This approach improves the situation slightly, but it is also possible to raise other potential bugs that allow the wizard to overlap in some cases without sending a conflicting signal. A smaller rectangular collision is just as effective as a simple rectangular collision because all you do is compare whether the rectangle intersects. At present, the most effective collision detection is based on the sprite image data to detect collisions (that is, pixel detection method), this method is not actually to see whether the transparent area of the wizard is coincident, but the spirit of the image itself is coincident. In this case, a collision conflict is only obtained when the sprite image is coincident. This is an ideal collision detection technique because it is very accurate and does not cause any errors in the movement of objects of any shape.

Simulation of rectangular collision detection diagram

If there is no collision detection algorithm, in the course of this game, there will be a terrible situation, although the background can be moved, and the effect of the player's aircraft, and players can control the direction of the body, but there will be a cause of defects-when the enemy plane was hit by our plane, there is no response, The enemy plane still travels, the score does not increase any more, as if nothing happened. This is because there is no code to add collision detection to the game. Collision detection is one of the most important algorithms used in games. Because the detection of two objects in the game is surface contact. For example, whether the bullets hit enemy aircraft, enemy planes collided with the player and so on. Only the collision detection is possible to hit the enemy after the above situation, and the score has increased.

Using the MIDP API to crash collisions, you can use a series of methods in the Sprite class, all of which are called Collideswith (). The Collideswith () method consists of three species, and the first Collideswith () method detects collisions between two sprites:

Collideswith (Sprite S,boolean pixellevel);

To detect a conflict, simply call the method on the wizard and pass in another sprite. The 2nd parameter determines whether the collision detection is based on pixel-attached pixels, which is equivalent to the method of image data detection. If you only want to use a simple rectangular detection method to detect between two sprites, you can pass the value of False to the Collideswith () method.

The other two methods use an image and a tiled layer to detect a sprite conflict. In the case of using an image, the XY position of the image is specified, and collision detection is performed on the image and sprite. Tiled layers are similar to sprites, but it uses multiple tiled images to create a large composite image, such as using a tiled layer to create a walled maze, and then detecting an elf conflict to ensure that the sprite is within range.

Implement collision detection code as follows:

/*判断enemyShip是否被玩家子弹击中*/
     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)))
         {

……//碰撞处理
}
}
}
…………
//查看玩家是否与小飞机相撞
                         for(int i=0;i<this.MAX_SP;i++)
                           if(smallPlane[i].isVisible() &&
                               smallPlane[i].collidesWith(playerSprite,true))
                           {

…………
                         }

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.