Android Research game development Collision detection

Source: Internet
Author: User



Game collisions can be roughly divided into these categories

1. The protagonist and the border collision, limiting the protagonist can not walk out of the phone screen outside.
2. Collision with the physical layer, with the map of the House table chair and so on.
3. The collision between the protagonist and the game character, here refers to NPC and so on.
4. The protagonist and the script box collision, such as entering the room out of a plot dialogue and so on.

This shows that the collision in the game is mainly divided into

1. Collision between the point and the rectangle
2. The collision between the rectangle and the rectangle
3. The collision between the circle and the circle
4. The collision between the circle and the rectangle

Today I would like to introduce the most special one of the above collision mode between the protagonist and the physical layer collision. : The information of the map block is stored in each small square, and if 1 in a two-bit array means that the physical layer is not allowed to pass, others can pass. That is to say-1 the location of this map block draws something from this physical layer. The protagonist calculates his or her index in a two-bit array by the origin of its coordinates at the time of the walk, and then determines whether it can be passed by finding the values in the array according to the index. Do not understand the animation see Android Research game development frame animation implementation

Draw a Map

Based on the map editor generated by the array, must first draw the background layer map, and then draw the physical map and so on.
The array generated by the physical collision layer is not drawn, and it is only necessary to detect collisions with the physical layer each time the protagonist moves the coordinates.

Background bottom

Physical Layer

Collision Layer

Speaking of which, some friends may ask, why is the first layer separate from the second? Can't these two layers be merged?

: With two layers is mainly to solve the physical layer picture can not all display, like the following Lotus, its drawing area around the rectangle white is transparent area, so we need to first draw the map background layer in the drawing of the physical layer so that the transparent area can be covered.

How code is implemented

First we need to use the onkeydown () and onkeyup () method to determine the current phone that button is pressed, according to the key information update character animation, detect whether the collision.
Miskeydown = True means pressing the arrow key down
Miskeyleft = True indicates that the arrow key is pressed left
Miskeyright = True indicates that the arrow key is pressed right
Miskeyup = True indicates that the arrow key is pressed
MHEROPOSX represents the x-coordinate of a person
Mheroposy represents the y-coordinate of a character
Manimationstate indicates the ID of the playback animation because the character walks have 4 sets of direction animation so manimationstate can modify the animation of the play


123456789101112131415         /** Display Animation According to the button update **/        /** in the collision array to see if they collide with the physical layer of the map **/        if(Miskeydown){            manimationstate= Anim_down;            Mheroposy+= Hero_step;        }Else if(Miskeyleft){            manimationstate= Anim_left;            Mheroposx-= Hero_step;        }Else if(Miskeyright){            manimationstate= Anim_right;            Mheroposx+= Hero_step;        }Else if(Miskeyup){            manimationstate= anim_up;            Mheroposy-= Hero_step;        }

Each time the protagonist moves, we need to check if his X y-coordinate is out of the screen.
If the screen is out, keep his current position intact.


12345678910111213141516         /** detect whether people are out of screen **/        isbordercollision= false;        if(Mheroposx<=0){            Mheroposx= 0;            isbordercollision=true;        }Else if(Mheroposx>=Mscreenwidth){            Mheroposx= Mscreenwidth;            isbordercollision=true;        }        if(Mheroposy<=0){            Mheroposy= 0;            isbordercollision=true;        }Else if(Mheroposy>=Mscreenheight){            Mheroposy= Mscreenheight;            isbordercollision=true;        }

The protagonist should have two coordinates.

: Why the protagonist to have 2 coordinate points is the top left corner of the 00 Point program is not able to make logical judgments, such as the detection of the protagonist to the right to move out of the screen, because the coordinate point in the upper left corner so the whole picture out of the screen after the program to determine the characters out of the display, so you can not use the top It is necessary to use the following points to calculate the logical coordinates of the person. My suggestion is that we only calculate the point below and then, based on this point, calculate the origin of the upper left corner of the image and draw it in the notification image.

Judging collision Detection

Mcollision[][] This two-dimensional number is generated by the map editor, which is used to save all the information of the collision Layer-1 means that it cannot be passed
Mheroindexx, Mheroindexy represents the current character's index position in a two-bit array, and the values in the two-dimensional array are found to determine if the collision
MBACKHEROPOSX, Mbackheroposy Backup the coordinates of the person before the collision will use the backup coordinates after the collision to avoid the character to return after the collision.


123456789         if(mcollision[mheroindexy][Mheroindexx]== -1){            Mheroposx= Mbackheroposx;            Mheroposy= Mbackheroposy;            isacotrcollision= true;        }Else {            Mbackheroposx= Mheroposx;            Mbackheroposy= Mheroposy;            isacotrcollision= false;        }

I used a crash demo I wrote to show you the effects of the game crash.

collision with the physical layer Boundary Collision Finally, because of the code more I do not post in the blog, the following gives the download of the demo source download Welcome to read each other to learn from each other, study each other and discuss progress together. Source Download: Android Collision

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.