IPhone game development tutorial game engine (4)

Source: Internet
Author: User

IPhone gamesDevelopment tutorialGame Engine4) It is the content to be introduced in this article. Continue to the previous chapter. This section describes the content of 2D and 3D.

Physical Engine

PhysicalEngineYesGame EngineIn charge of distance, movement and otherGamesPhysical. Not allGame EngineBoth require physicalEngine. But all imagesGamesAll have physical code to some extent.

Don't you believe it? Use tic-tac-toe as an example. It is indeed a very simple game, but even this game has a physical part. When a player selects a square to mark, we must check whether the selected square is valid. If yes, we will mark and determine whether the player wins. This is an example of the two basic tasks completed by the physical engine: Detection and solution.

Collision Detection and Solution

It is very important to maintain independence in your mind. In game code, detection is independent of determination. Not all objects will collide with other objects in the same way, and not all the detected collisions will be solved in the same way.

For example, let's assume a game: o'reilly wild adventure. If a player's virtual doll finds that he accidentally came to the o'reilly wild reserve, it must avoid strange and dangerous animals and return to safety.

The following physical interactions occur in this game:

1. collision between players and maps

2. collision between animals and maps

3. collision between players and animals

4. collision between players and destinations

First, the collision between players and maps is very simple. We detect the boundary of the player's physical area and the wall in the level. If the player will collide with the wall, we will slightly shift the player so that it will not collide with the wall.

The second type of interaction is a little more complex. We use the same detection method: To detect the Physical Areas of animals and the walls in the checkpoints. But our solutions are somewhat different, because animals are not controlled by players, but controlled by computers. When we solve Case 1, we shift the player a little so that it will not enter the wall. This is used to remind the player that he is hitting the wall and must change his direction.

If we do the same solution to situation 2, AI will not realize that it is hitting the wall and will continue to go inside the wall. Therefore, we can solve this problem in two steps. First, we need to slightly shift the animal so that it will not collide with the wall, and then notify the AI animal to hit a wall. In this way, the game logic will control the movement of animals.

In the third case, we first check the boundary of the player's body area and the animal body area. Once we detect that they will have a collision, different results may occur. If the animal is a monkey with glasses, it may escape; if the animal is a Viper or a lion, it may attack players; camels may ignore players; and locusts may be flushed.

Finally, the fourth case is a new situation. The destination is different from the map, the player and the animal because it has no graphical representation. It is an implicit trigger area, when a player enters it

It will issue an event. Fortunately, although it does not have a graphical representation, it still has a physical representation. Therefore, we can still detect the boundary between the player's physical region and the destination region. If we find that a player has reached the target, we will notify the game logic to bring it into the "Player victory" game status.

Two-dimensional collision detection

Two-dimensional collision detection is a relatively simple process. Most of the operations can be summarized as follows: rectangular to rectangular, rectangular to contain points, circular to rectangular, circular to contain points and circular to circular may also need to check line segments, but it can be avoided ).

Since these routines may be used multiple times per second, it is important to ensure that they are as efficient as possible. In order to achieve this goal, we will conduct a series of low-cost tests to prove that two objects did not collide before the collision:

 
 
  1. bool cd_rectangleToRectangle( Rect r1, Rect r2)     
  2. {     
  3. //can't be colliding because R1 is too far left of R2     
  4. if( r1.x + r1.width < r2.x ) return FALSE;     
  5. //can't be colliding because R1 is too far right of R2     
  6. if( r1.x > r2.x + r2.width ) return FALSE;     
  7. //can't be colliding because R1 is too far below R2     
  8. if( r1.y + r1.width < r2.y ) return FALSE;     
  9. //can't be colliding because R1 is too far above R2     
  10. if( r1.y < r2.y + r2.width ) return FALSE;     
  11. //if we get here, the two rects MUST be colliding     
  12. return TRUE;     
  13. }    

In this case, more code is executed when an object is collided. Most of the time, objects do not conflict with each other. We have optimized this because it is more efficient.

Before proceeding, let's look at a very important concept: the graphical representation of a game object is independent of its physical representation. A computer can only provide a limited amount of computing per second. It is very difficult to perform real-time physical simulation. Because of this, the physical code of a game has a long and proud tradition, that is, as long as it is accurate to make players feel that the game is correct.

For example, in the game we mentioned above, it is obvious that the virtual doll of our players is about to hit a rhinoceros, or our players are not very attentive, they will be chased by an angry tiger ). During the running process, our players have a lot of movements in their limbs. The same is true for the rhinoceros, which sticks out the horn on its head.

Although the player's hands and feet are required for graphical representation during the running process, do we really need to know the coordinates of the limbs in the physical representation? We really need to check whether the player's hands, feet, and head are in conflict with the rhino's head, feet, and horns. Do you forget your tail !)? Of course not, because our game simply uses rectangles to represent players and rhinos as needed.

3D Collision Detection

3D collision detection is much more difficult than 2D collision detection. You must be familiar with 3D mathematical computation, such as linear algebra. In addition, 3D games have more complex game scenarios even if the mathematical requirements are not high. Fortunately, you can rely on appropriate technologies to help reduce the number of computations. Of course, it is still necessary to make the players feel that the game is correct.

As we discussed earlier, an object's image representation is different from its physical representation. However, sometimes we need to make sure that the difference between them is smaller, the better. Imagine a first-person shooting game. We need to know whether a player has shot another player or not. Obviously, a simple box boundary cannot meet the needs, but we cannot provide a check path for each bullet to determine the path of each triangle of every virtual doll it passes through.

We use the same 3D detection, just like the one we use in 2D: a series of low-cost Negative tests are performed before precise and expensive tests are performed. Taking bullets as an example, we first test whether a bullet has crossed the boundary of a player. If they are not hit, we exit the collision check. Find the starting hitting point in the box and perform more detailed tests on all triangles in the box. This is the physical version of the detail level in graph optimization. If the check on the nearest player fails, we will perform a detailed check on the next player. In this way, we can efficiently and accurately perform more complex scenarios, such as a bullet flying between a player's legs, hitting another player behind him.

Collision Solution

When a collision is detected, it must be resolved. The first thing to consider is what low-level or high-level actions must take place. Low-level actions can be solved by physical code, such as adjusting the player's position so that it will not fall to the ground. High-level Action refers to the signal sent to the game logic part of the game engine. These signals allow the game logic to know when an animal is running into a wall or whether a player has reached its destination.

Some collision responses require multiple high-level and low-level responses at the same time. For example, in a ball play game, if a ball is hit with a bullet proof rod, it should correctly exit the low-level bullet proof rod), it will also send a signal to the game logic at the same time, this allows the hit bar to produce animated effects, sound effects, and a higher score for players ).

Pay special attention to the details when testing low-level collision solutions code. One of the most important elements that affect the gaming experience is physical code. Can virtual dolls quickly respond to player input? Can a racing game simulate a suspension system and a speed control system? Will the screen shake when a player has a cannon?

Basically, it is the work of designers who make the game feel good, but it requires the work of programmers to implement it. Do not be afraid to write some special test cases to get the correct results.

For example, a common technique used in testing is to set "flag" for objects ". When an object is in contact with another object "ground", you can set a "grounded" flag to true for this object. When the grounded flag is true, it can be considered that the object is resting, and it does not need to be subjected to the influence of gravity, or the collision between the object and the map is detected. This not only helps you avoid collision detection on a large number of static objects, but also avoids the jitter of objects near the ground in some physical simulation clocks.

Summary:IPhone gamesDevelopment tutorialGame Engine4) I hope this article will help you. Want to learn moreIPhone game engineFor more information, see the following articles:

IPhone game development tutorial Game Engine 1)

IPhone game development tutorial Game Engine 2)

IPhone game development tutorial Game Engine 3)

IPhone game development tutorial game engine 5)

IPhone game development tutorial Game Engine 6)

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.