Put your cursor over any blue highlighted text to view an example.

So here goes:

- The flag is made up of a grid of points.
- Each of the points has its own position and velocity.
- Each point will interact only with its neighbouring points.
- Each point will feel the force of gravity and possibly any wind or other forces.

I advise using multiple arrays, each holding a different property.

eg.

vec3_t gOrigin[20][20];

vec3_t gVelocity[20][20];

vec3_t gForce[20][20]; //The constant forces applied to the fabric(gravity, wind etc.)

float gSprPosX[20][20]; //I'll explain these later

float gSprPosY[20][20];

These are the basic arrays needed. I only picked the value of 20 because this is a suitable value for the size of a flag. So the grid would have 20 rows and 20 columns. The distance between the points is up to you. It is a trade-off between being able to see the individual polygons, so the fabric looks smooth, and rendering speed.

- The Initialisation
- The Physics
- The Rendering

Interactions between points:

First work out the distance between the two points in question, using

eg.

d 2 = x 2 + y 2 + z 2

Now take away the distance you want between your points. If the result is positive, the points need to go together; negative, they need to move apart. Using this value, the force that has to be exerted on the points is worked out. To do this, the position from the first point is subtracted from the second to get the direction. This is then multiplied by the result obtained earlier, which gives the force its direction and magnitude. This force then needs to act upon the two particles. Add the force to the velocity for one point and take it away from the other.

This shows a trigonal grid and how every point must interact with its neighbouring points. These red lines must be kept the same length to keep the structure rigid. However, notice that the stucture can bend if in 3D dimensions, giving its fabric properties. If a little bit of range between the points if allowed then the fabric will become stretchy. Remember to make sure that the points on the sides of the grid don't try to interact with points that don't exist. That would just cause Half-Life to crash on running. It's this sort of problem that makes the maths difficult. An infinite fabric would be comparitively easy.

This section is very difficult to get right because one bit of confusion over whether a value is + or - can result in the points repelling instead of attacting or the other way round.

The interval between this function being called depends on the frame-rate. So without any correction, the flag will flap far faster on a system with a frame-rate of 72fps than on a system with only 20fps. So a way is needed to regulate the speed. This is done by moving the flag less per calculation, if there is a higher frame-rate. To do this I recommend

ie.

dTime = gEngfuncs.GetClientTime()-LastTime;

LastTime = gEngfuncs.GetClientTime();

Using a loop, this function will have to be applied to every point for each of its neighbours. The purpose of this interaction is to keep the fabric in the shape you want it. To make the fabric elastic, just make the reaction force less strong, so the grid is allowed to stretch a little.

Example of how the points map onto a texture.

In addition to the triangle and upside-down triangles, you also need to draw some triangles at the sides to avoid a zig-zag pattern. If you do all this you're left with the corners having a bit cut off, but that's barely noticeable, especially when the fabric's flapping about in the wind.

Another important force is a dampening force, this force will allow the effect of air resistance. It also ensures that if there are any slight problems in the maths, the fabric wont start doing unexpected things. To add this

eg.

gVelocity[a][b] = gVelocity[a][b] * pow(0.95, dTime);

Once you've got a working version of that then you should understand far more how the system works.

- When using a rectangular grid, so each point only interacts with the one above, the one below, the one to the left and the one to thr right, the grid can collapse into an acute lattice pattern.
- If the flag is bent over, you may be able to see part of the flag that is behind the other part. This problem is very difficult to overcome, while keeping the frame-rate up.
- Similar to the last point, if there are two TRIapi effects running then the one rendered last will appear in front, irrespective of their positions in 3D space.
- Yet another graphical glitch, entities will appear in front of the fabric, this also goes with particles too. The mappers will have to deal with this and make sure that there can not be a situation, where this problem can be observed.
- As each point only reacts with the points next to it, the fabric can go through itself, if this happens, you will know what I mean.

Good Luck!

You must log in to post a comment. You can login or register a new account.