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

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

During each step you will need to multiply the vectors by a certain factor. If the force vector is too large, the points will pass each other during their movement. Think about the consequences of this. From now on these particles will push away from each other to try and return to their proper positions. This can cause extremely unpredictable results, sually involving every point travelling away from every other point at an exponential velocity.

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.

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

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!

- Categories
- Archived Articles
- VERC Archive
- VERC - Half-Life
- VERC - coding
- Tutorials
- Images Required
- Programming

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