Flash pragmatism (II)-the shortest path principle (I) is to simplify complicated problems.
There is not only one way to achieve the goal, but the one at the moment is often not the shortest one. Therefore, the first step before solving the problem should be to find the shortest path to achieve the goal. Although some people may like to complete complex algorithms to gain a sense of accomplishment, this is another topic.
We must understand that we are the workers of implementation tools, rather than scholars who want to study in the air.
Magician's perspective, not the audience's perspective
The first is a typical example.
The middleware asked this question about collision detection.
It is also irregular, concave, and convex collision detection. It can be used to determine whether two objects match edges. At last, the surrounding blocks are automatically detected and automatically adsorbed when you put them down. It must be said that this topic is really difficult, not to find a method, but to find a method that is acceptable in efficiency. There should be some optimization methods, but I won't, because it still has to be absorbed. I know which side is the edge. But then I thought something was wrong and asked him what he was actually going to do.
Only then can we know that this guy was doing the -- puzzle.
The so-called jigsaw puzzle refers to a piece of picture being split into various irregular shapes and then disrupted, and then let the players re-fight, the demand is like this. Well, in this case, write down the original positions of these fragments and determine whether the current coordinates of the fragments on your hand are close to this position.
There is no need for computers to use human thinking, and designers to use players' thinking. A full simulation is not the best solution.
First, consider simple approximation.
Foreigners are always pleasantly surprised. Foreigners often have simple ideas and are rarely overly-designed. In any case, their experience should be enriched. Although a demand can be achieved, the accumulation of nuances will still produce great changes. This time, it is about an SNS game that a colleague is playing. it collects money and flies it to a number indicating money. this effect is quite chic.
In China, even a straight line flight is unnecessary, let alone a curve. However, the curve is indeed more vivid than a straight line. Then I will carefully observe its trajectory, consider how it is implemented, and want to prepare a class for future use. The general idea is to use the quadratic Bell curve formula to calculate the trajectory. although a mathematical formula is required, it should not be a problem. besides, TweenMax also provides this function, but since it is a curve, there is a curvature problem, and the reference effect is not a fixed 1/4 circle, and this approach is too small. The next step is to consider physical movement. However, the original effect seems to be circular buffering. the closer the target is, the slower it is, and the end of the parallel y axis is. this will be difficult to use physical simulation ......
In the end, my mind suddenly went viral. Let the x axis move at a constant speed, and the y axis to slow down the circle to buffer the movement, it looks like it is in the picture ......
That is, the code:
TweenLite.to(target,{x:tx }); TweenLite.to(target,{y:ty,ease:Circ.easeIn });
The result is an arc, which is exactly the same as the original product. Obviously, this class is boring.
Even if there is a complicated solution, you should consider whether there is a simpler solution. The complicated solution is only applicable to a wider range of applications. However, since it is "so simple", why bother with that.
To do not know what TweenLite is, see the following address: http://www.greensock.com/tweenlite/
"Select technology as needed" "Use applicable technologies as needed"
Speaking of this, the problem of the quad-tree is raised. The quad-tree is indeed a classic solution for filtering and traversing screen items. For details about how to do this, refer to other information on the internet. Unfortunately, the principle is somewhat confusing, and the implementation is also disgusting. Therefore, it is not widely used at present.
But isn't it used to filter items on the screen? Because there are two coordinates, you can classify items by coordinates. Here, we have made the mistake of "applying applicable technologies to demands" rather than "selecting technologies based on requirements. The quadtree can be used to quickly filter items on the screen, but it does not mean that only the quadtree can be used to filter items on the screen. You need to know that the quad-tree can process arbitrary screen scaling and fast screening in an infinitely large coordinate system, but what we actually need is screening within a fixed screen size within dozens of screens.
In fact, there is a well-understood alternative solution. We can create a two-dimensional data, for example, if the map is 10000*10000 and the screen size is 1000*1000, we will create a 20*20 two-dimensional array, then, the items whose coordinates are in the range of (0-500, 0-500) are included in the (500) of the array, and (-, 0-) are included in the () of the array, (1000-500, 0-1000) exists in (1500,500) of the array, and (1000--) exists in () of the array, put (1000-1500,-) in the () of the array ...... Then, all the items in the map are saved in a two-dimensional array in this way, and the array is updated when the items are moved.
To get the screen range, start with the screen center coordinate, divide by 500 to get the coordinates of a block, and then take the nine blocks around him, the items in these blocks certainly overwrite the entire screen. Although there will be more items out of the screen, this excess consumption is acceptable.
This is the same as the result of the use of the quad-tree, both of which can quickly locate items on the screen, so that there are a large number of objects in the game, but as long as they appear on the same screen at the same time, it will not consume too much traversal performance.
For Cross tree traversal, you can refer to the log of aging people :( http://wxsr.blogbus.com/logs/60788934.html)
Special handling should be used in special cases
Bullets will penetrate the wall, which is a classic problem.
In reality, bullets move linearly, but not in computers. Bullets must be moved at intervals, and the speed of bullets is very fast, small, and when the walls are thin, there is a probability that the collision detection will fail. This is actually a classic problem. for example, if you have used to play the games, you can go through the wall to a certain extent. after all, collision detection is a high-consumption operation, which can be simplified and simplified, especially in the case of a large number of bullets in a flying shooting game, it is not worthwhile to adopt complicated judgment logic.
It should be noted that in a set environment, the probability of bullets going through the wall is actually very low. This kind of game bullets can be seen and avoided, and the speed cannot be too fast. Even if the bullet is indeed faster, as long as your walls are not thin enough to see it, it is generally not worn. If both conditions are met, I can only think that your game design is slightly faulty.
Well, when it comes to the solution, the most thorough of course is to calculate the moving track and determine whether it is at the edge of the obstacle. this is not difficult, but the performance consumption will double, just for special circumstances, is this necessary?
If all the bullets in the game go through the speed of the wall, this game will basically not be used. Therefore, only certain high-speed shells can penetrate the wall. It is not appropriate to make the collision judgment of other normal speed bullets to reduce the performance. how can this problem be solved?
You only need to extend the tail of a specific bullet, that is, modify the bullet material, add a transparent tail to the tail and participate in the collision. Although it looks like a dot, it is a line. In this way, it is much more difficult to penetrate the wall. However, this extension cannot be felt because it is a high-speed bullet, without a trace, and cannot catch the tail.
The reconciliation of steps may greatly simplify the logic.
At that time, I was trying to make an explosion. It is the way a piece of glass is sliced and dispersed. For example, you can refer to the combat switching of FFX and the switching animation after DMC3 levels are completed.
The other part of this effect is very simple, and the difficulty lies in cutting. The splitting method is random. First, create a bunch of random points and combine the three points into a triangle. The problem is how to combine them. Triangles certainly cannot overlap, so you need to combine close points into triangles and ensure they are not stacked together, the problem is changed to "what kind of points are called close points ". Where did I see this algorithm, but I don't remember it ......
I couldn't find it, so I found an approximate solution instead. The key problem here is the random point. as long as the point is not random, the combination triangle can be used ), (), () the combination of the convention, and then ensure that the random time will not cross.
First, we create a situation that does not take random into account. just like below, we cut the rectangle evenly and divide the rectangle into two triangles. In this case, the combination rule from vertex to triangle is fixed.
Take this as the original state, and then adjust the vertex position. we can see that even if each vertex is moved separately, as long as each vertex is still active within the rectangle range, the triangle will not overlap.
The result is as follows: Although not completely random, it can basically meet the requirements.
Apart from vertices that must be on the edge, the coordinates of freely moving vertices in the center can be simply obtained.
x = dx * (i + Math.random()) y = dy * (j + Math.random())
Dx and dy are the length and width of the segmented small rectangle, and I and j are the x and y numbers of the vertex.
In another way, there will be a new direction. Creating a vertex and then combining a triangle are different from creating a triangle and then adjusting the vertex.