Objective: To achieve a collision between the horizontal direction and the vertical direction, which is more realistic.
1: To solve the horizontal collision, we must first identify a problem that what on the map cannot pass through, that is to say, we must first define some object brick types on the graph for collision detection.
Preparations for the start of the horizontal detection function:
In order to simplify the horizontal detection, because mario's anchor is in the center position, both sides of the detection are replaced by the center position on the left and right. The two conditions shown in the figure are used to check whether Mario is late for the big mushroom and the award mushroom.
The last and third sentence in the map is to convert the GL coordinate and the brick coordinate. This is very important because the game uses the GL coordinate system, but the tmx map uses the brick coordinate, you need to convert the coordinates of Mario in the game to the coordinate of bricks, then use the last sentence in the sentence to find the types of bricks under the coordinate of the current bricks, and make the judgment of the switch statement:
We can see which bricks are subject to horizontal Collision Detection and which are not needed. When encountering an obstacle, although Mario has a walking animation, his position cannot be moved and he stays in front of the obstacle.
All of the above shows the detection process on the right side of Mario. The same is true on the left side and on the right side.
2: collision detection in the vertical direction, as well as the left and right translations and jumps standing on the map ladder do not follow the implementation:
Start of the detection function in the vertical direction:
In the first condition, Mario fell into the trap and died. The following detection is not required. The second condition is that the control of Mario's head does not go out of the map area.
In the game, Mario needs to be able to step on the edge of bricks in the vertical direction,
This effect cannot use the midpoint of Mario's foot as the detection point. We should put the detection code in a loop, from the lower left to the lower right corner of Mario. If we find that we can stand, then you can stand and exit the loop. The remaining parts are always in the horizontal direction.
Vertical Detection involves the detection of Mario landing and falling down from the top of the head to the bricks. The difference between the latter and the current jumpofffset is positive and negative. When jumpoffset is positive, it indicates that Mario is in the take-off phase, and vice versa.
Mario's whereabouts detection is no different from the above.
Finally, Mario stood on the ladder to detect and follow the ladder. Because the ladder is implemented by placing objects on a map, there is an array of ladders in ccgamelayer, which stores all the ladders. The ladders have the moving component values that move up or down or left or right, there is also the length of the ladder, because the ladder is placed in the vacant place, so Mario can not head to the ladder, so the Ladder detection only Mario whereabouts. Code snippet of the update function for detecting a ladder: