Principle of game collision

Source: Internet
Author: User

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.


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.
 

 

 

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.


  1. /** Update the display Animation Based on the buttons **/
  2. /** Search for the collision array to see if you have collided with the map physical layer **/
  3. If (mIskeyDown ){
  4. MAnimationState = ANIM_DOWN;
  5. MHeroPosY + = HERO_STEP;
  6. } Else if (mIskeyLeft ){
  7. MAnimationState = ANIM_LEFT;
  8. MHeroPosX-= HERO_STEP;
  9. } Else if (mIskeyRight ){
  10. MAnimationState = ANIM_RIGHT;
  11. MHeroPosX + = HERO_STEP;
  12. } Else if (mIskeyUp ){
  13. MAnimationState = ANIM_UP;
  14. MHeroPosY-= HERO_STEP;
  15. }

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.

  1. /** Detect whether a person is on screen **/
  2. IsBorderCollision = false;
  3. If (mHeroPosX <= 0 ){
  4. MHeroPosX = 0;
  5. IsBorderCollision = true;
  6. } Else if (mHeroPosX> = mScreenWidth ){
  7. MHeroPosX = mScreenWidth;
  8. IsBorderCollision = true;
  9. }
  10. If (mHeroPosY <= 0 ){
  11. MHeroPosY = 0;
  12. IsBorderCollision = true;
  13. } Else if (mHeroPosY> = mScreenHeight ){
  14. MHeroPosY = mScreenHeight;
  15. IsBorderCollision = true;
  16. }


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.

  1. If (mCollision [mHeroIndexY] [mHeroIndexX] =-1 ){
  2. MHeroPosX = mBackHeroPosX;
  3. MHeroPosY = mBackHeroPosY;
  4. Isaco trcollision = true;
  5. } Else {
  6. MBackHeroPosX = mHeroPosX;
  7. MBackHeroPosY = mHeroPosY;
  8. Isaco trcollision = false;
  9. }


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


Collision with the entity Layer
 
 




Collision with the entity Layer 







Collision with map Boundary 



 

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.