Android research-Game Development Collision Detection

Source: Internet
Author: User

Android research-Game Development Collision Detection
Zookeeper

Game collisions can be roughly divided into these categories

1. the collision between the protagonist and the boundary restricts that the protagonist cannot go out of the screen of the mobile phone.
2. the collision between the protagonist and the physical layer, as well as the house, tables, chairs, and so on in the map.
3. the collision between the main character and the game character. Here it refers to the NPC and so on.
4. the collision between the main character and the script box, for example, entering the room for a story dialog.

It can be seen that the collision in the game can be divided

1. collision between points and rectangles
2. collision between rectangles
3. collision between a circle and a circle
4. collision between a circle and a rectangle

Today, I will mainly introduce the collision between the protagonist and the physical layer in the most special collision mode. : The information of the map block is stored in each small block. If-1 is used in the two arrays, it indicates that the physical layer cannot pass, and others can pass. That is to say, the location of the-1 Map block draws the physical layer. When the main character is walking, he can use his coordinate origin to calculate his own index in the two-digit array, and then find the value in the Array Based on the index to determine whether he can pass. If you do not understand the animation, please refer to the Android research-game development frame animation implementation.

Draw a map

Based on the array generated by the map editor, you must first draw the background layer map and then draw the physical map.
The array generated by the physical collision layer does not need to be drawn. You only need to check whether it is in conflict with the physical layer every time the protagonist moves the coordinates.

Background layer

Physical Layer

Collision Layer

Some may ask why the first layer should be separated from the second layer? Can these two layers be combined into one?

: Two layers are used to prevent all physical layer images from being displayed. For example, the following lotus flower shows a transparent area in the white area around the rectangle, therefore, we need to draw the background layer of the map in the physical layer so that the transparent area can be covered.

Code Implementation

First, we need to use the onKeyDown () and onKeyUp () methods to determine whether the current mobile phone's button is pressed, and update the character animation based on the key information to detect whether it is a collision.
MIskeyDown = true: press the direction key
MIskeyLeft = true indicates that the direction key is pressed to the left.
MIskeyRight = true: press the direction key to the right
MIskeyUp = true indicates that the direction key is pressed.
MHeroPosX indicates the X coordinate of a person
MHeroPosY indicates the Y coordinate of the character
MAnimationState indicates the ID of the animation to be played. Because there are four groups of animations walking, mAnimationState can modify the animation group to be played.


123456789101112131415 /** Update the display Animation Based on the buttons ** // you can search for an animation in the collision array to check whether it is in conflict with the map physical layer. **/if (mIskeyDown) {mAnimationState = ANIM_DOWN; else + = HERO_STEP;} else if (mIskeyLeft) {mAnimationState = ANIM_LEFT; else-= HERO_STEP;} else if (mIskeyRight) {mAnimationState = ANIM_RIGHT; else + = HERO_STEP ;} else if (mIskeyUp) {mAnimationState = ANIM_UP; mHeroPosY-= HERO_STEP ;}

When the protagonist moves, we also need to check whether the X-Y coordinates of the protagonist exceed the screen.
If the screen is exceeded, the current position remains unchanged.


12345678910111213141516 /** Detect whether a person is on the screen **/isBorderCollision = false; if (mHeroPosX & lt; = 0) {mHeroPosX = 0; isBorderCollision = true ;} else if (mHeroPosX & gt; = mScreenWidth) {mHeroPosX = mScreenWidth; Enabled = true ;}if (mHeroPosY & lt; = 0) {mHeroPosY = 0; isBorderCollision = true ;} else if (mHeroPosY & gt; = mScreenHeight) {mHeroPosY = mScreenHeight; isBorderCollision = true ;}

The protagonist should have two coordinate points.

: The reason why the protagonist has two coordinate points is that the 00-point program in the upper left corner cannot be used for logical judgment. For example, if the protagonist moves to the right and beyond the screen, because the coordinate point is in the upper left corner, the program can determine the person's output only after the picture is displayed. Therefore, the point in the upper left corner cannot be used as the coordinate origin of the protagonist, we need to use the following points to calculate the logical coordinates of the characters. I suggest that we only calculate the following points and calculate the origin point in the upper left corner of the Image Based on this point, and then draw the image in the notification image.

Judge Collision Detection

MCollision [] [] the two-dimensional number is generated by the map editor. It is used to save all information of the collision layer.-1 indicates that it cannot pass through
MHeroIndexX and mHeroIndexY represent the index position of the current character in the two arrays. Based on the index, locate the value in the two-dimensional array to determine whether the collision is performed.
MBackHeroPosX and mBackHeroPosY are used to back up the coordinates of the characters before the collision. Once the collision occurs, the backup coordinates are used to avoid returning the coordinates after the collision.


123456789 If (mCollision [Photo] [Photo] =-1) {mHeroPosX = photo; mHeroPosY = photo; isaco trcollision = true;} else {mBackHeroPosX = mHeroPosX; photo = mHeroPosY; isaco trcollision = false ;}

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

Collision with the entity Layer Border collision At last, I will not post my blog because of the large number of codes. Below we will download the Demo source code. You are welcome to download, read, and learn from each other, study each other, and discuss and make progress together. Download source code: Android collision

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.