Do not use the physical engine to prevent cluster behavior enemies crowding

Source: Internet
Author: User

Transferred from: http://www.gameres.com/thread_256279_1_1.html

Gameres release, text/paladin_t, reprint please specify gameres and author


Pursuit, clustering is a very common mode of enemy behavior. Moving closer to the player's position is called pursuit; clustering refers to the collective action of a group of enemies, like a group of sardines. Swarm enemies often cooperate with chase to exert pressure on the player, so this article implicitly uses the chase to do the clustering example.

If the enemy of the cluster does not have the ability to perceive other enemies within the group, it will create a very strange phenomenon: crowding. I've seen a lot of games that can control the protagonist's pocket to keep the swarm of enemies in a pile, or you won't be able to move them in a straight line.

<ignore_js_op>

This is annoying, the player will think the enemy is very silly, if the player to take refuge from the enemy's strategy, the challenging elements only left to the decision-making and movement speed of the game. The more disgusting is often the enemy than you next to me I am next to you also close, even all superimposed on a point.

<ignore_js_op>

Hey, that's a little stupid. This is the most simple implementation of raw, who can think out.

<ignore_js_op>
The enemy has always been a mere trend of the protagonist (and may also make a calculation of the protagonist's mobile pre-judgment, but not in the scope of this article), the protagonist from point A to point B in the process of the enemy has always maintained such a trend.

<ignore_js_op>

Use the color curve to describe the trajectory of the enemy in the example diagram, perhaps this looks like this:

<ignore_js_op>

<ignore_js_op>

See, the range of enemy groups is getting smaller. As a developer and player, this prototype level of practice will leave me with a low-finish impression of this game.

Physics engine in the modern game is no longer a strange thing, regardless of 3D games or 2D games, can find the physical mechanism and game expression combination of very good examples, some games go farther, the use of physical mechanisms more in-depth, physics is the whole fun of this game. If you happen to have a physics engine in your game that you can take advantage of, maybe you can use it to prevent enemies from getting too close, such as giving each enemy a collision-proof crash that is slightly larger than the bounding box.

<ignore_js_op>

It looks like the enemy has a repulsion field with each other, cool! It's done!

You have to admit that people who do game tricks are not content with thinking about how to use it well. I am not rejecting the use of the physics engine, but I very much like the physics mechanism, I have given my first app to achieve a physical engine, efficient, stable operation, the most important thing is that the compact version of the physical engine features not much just enough to meet my needs, there is no need to use a bigger library. I do not want to use the physics engine to prevent the swarm behavior of the enemy crowding together because: there is absolutely no need to use AH. Some RTS different classes have a matching game, need different classes of density balance, if using the physical engine is difficult to do. Asked a few peers, it does not seem that the physics of the engine to the matter is not a rut, no way, and have to do their own hands. Let's take a look at what happens if you don't use the physical engine to handle the "shoving" between enemies.

<ignore_js_op>

If there are two enemies, you only need to do 1 calculations to keep one with the other at an appropriate distance, if there are 3 enemies, 3 such calculations need to be done, if there are 4, the need to calculate 6 times, if there are 5, to count 10 times ... The complexity of this algorithm is O (n (n-1)/2), it is conceivable that the number of enemies this will become a CPU disaster. Experience tells me that the complexity of the "N times N" factor such as the algorithm is not desirable, need to find ways to optimize or make a compromise.

Thinking optimization solution from the reality, we each have in the Phalanx team March experience, left and right footsteps of the unified relying on the "121" slogan and step sound to do synchronous data source; the relative position of the square in the matrix is fixed we do not need or can not be obtained by obtaining the position information of all other people in the matrix to do feedback adjustment, As a matter of fact, we are only looking at the range of our eyes to make vague judgments with our nearest classmates. Consider the act of an enemy, regardless of the other enemy's own act of acting alone:

<ignore_js_op>

This is very easy, just want to go to the main character position, we might as well, we can take the example of the priority calculation action of the enemy 1 as a phalanx when the first class, they just need to walk their own on the line.

Take the enemy 1 and the lead position line (length R) on a point, (for example, you can set R for half of the length of the segment), from the protagonist position to this point (with R as a radius) to make a circle, at least we know the total number of enemy groups have a few, the circle evenly divided into n parts, Each one is heading towards the corresponding dividing end, well, maybe you have to make some fine-tuning for each enemy. As the enemy 1 from the protagonist more and more close, R is getting smaller, this circle will increasingly shrink to the protagonist, so we magically reached the enemy of this set of behavior:

1. Group Mobility trends are uniform
2. The individual will not be very close until the circle shrinks to a very small

<ignore_js_op>

The circle only needs to be divided once, a certain enemy just need to know what is the number of a speed vector can be calculated, see, the complexity of a drop to O (n), a single calculation of the vector calculation is also reduced.

To talk about a little more, the swarm of enemies into a number of heaps how to achieve it, so to see, each heap to do a clustered group to apply the above algorithm, and then a certain group as a whole, a number of such groups in the same application of clustering algorithm (or other behavioral patterns).

<ignore_js_op>

This algorithm code can be obtained on GitHub.


Wen/paladin_t, author micro-blog

(turn) do not use the physical engine to prevent the swarm behavior of enemies crowding

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.