In this article, I will try to explain how you can implement the motion of objects (hereinafter referred to as particles) on the surface of 3D geometry.

## 1. Particle Creation

To position the particles on the model, we will select a random triangle from the list of polygons. To do this, you can use the following methods:

1. If the model is represented by a list of vertices, then generate a random number in the interval [0; кол-во вершин / 3 – 1]. Thus, we will get the index of the first vertex of the triangle we need, and the other two vertices will be obtained by the increment of the index on 1 and 2.

2. If the model is represented by a list of vertices and indexes, then generate a random number in the interval [0; кол-во индексов / 3 – 1]. Thus, we will get the index of the first index of the triangle we need, and the other two indices will be incremented on 1 and 2. From the list of vertices, we get the vertices we need from the calculated indices.

Once the triangle is selected, we need to position the particle inside it using barycentric coordinates (Figure 1).

**Barycentric coordinates** are the weighted average of the triangle vertices (*P _{0}, P_{1,} P_{2}*) and are expressed as scalars

*w*, such as:

_{0}, w_{1}, w_{2}Dot *P* lies inside the triangle if all three weights are non-negative and their sum is 1. Therefore, we can generate the first two coefficients randomly, and get the third by subtracting from 1:

Next, we substitute the vertices of the triangle of our choice and the calculated weights into the barycentric coordinate formula.

## 2. Particle Motion

In order to define the velocity of a particle, it is necessary to define a velocity vector, which must be parallel to the plane of the triangle and perpendicular to the normal vector of this plane (Figure 2).

Get this vector *V* can be subtracted by subtracting the position of the particle *P* from the position of one of the three vertices of the triangle (Figure 3). However, in order to make the motion of particles random, it is necessary to rotate the resulting vector by a random angle *θ* around the normal of the triangle.

To perform such a rotation, you need to find the normal of the triangle through the vector product and the right-hand rule.

Then perform the vector product of our velocity vector *V* and the resulting normal *N* (Figure 5).

As a result, we get two perpendicular vectors *V* and *Q*, which lie in the plane of the triangle and make up a rectangular coordinate system (Figure 6).

Any vector in the plane of a triangle can be expressed as a linear combination of vectors *V* and *Q*:

Where is *θ* – Random rotation angle.

## 3. Triangle Entry Check

In the process of motion, the particle can go beyond the triangle (Figure 7).

Barycentric coordinates can be used to determine this event. To do this, rewrite the formula of barycentric coordinates in the form of a matrix equation:

In here *w _{0}*,

*w*and

_{1}*w*– Weights that determine the position of the point

_{2}*P*relative to the triangle. To find these coefficients, you need to invert the matrix:

If all three weights are non-negative and their sum is 1, then the point lies inside the triangle. If this condition is not met, then the point lies outside the triangle, and the particle must be moved to the next triangle.

## 4. Transition Between Triangles

In order to determine to which next triangularTo bind a particle, you need to find the edge of the current triangle closest to the particle. The adjacent triangle that shares this edge with the current triangle will be the new triangle (Figure 8).

Before calculating the distance to an edge, it is necessary to verify that the particle is within the edge (Figure 9).

In order to determine whether a particle goes beyond the edge or not, we will use the following algorithm:

1. How to Calculate Vectors *P _{0}P_{1}* and

*P*Where is

_{0}P*P*and

_{0}*P*– coordinates of the ends of the edge, and

_{1}*P*– Coordinates of the particle.

2. How to Calculate the Dot Product of Vectors *P _{0}P_{1}* and

*P*.

_{0}P3. If the dot product is less than zero, then the particle is behind the point *P _{0}* and is not within the edge (Figure 10).

4. How to Calculate the Dot Product of Vectors *P _{1}P_{0}* and

*P*.

_{1}P5. If the dot product is less than zero, then the particle is behind the point *P _{1}* and is not within the edge (Figure 11).

6. If both dot products are greater than zero, then the particle is within the edge (Figure 12).

To determine the vector of displacement of a particle to an edge, we can project the position of the particle onto the edge and plot a vector from the position of the particle to the projection point.

Formulaically, this can be expressed as follows:

Distance *d* between the projection point and the particle can be obtained by calculating the length *Displacement* Vector. We will also use the resulting displacement to move the particle to the plane of the triangle.

Next, we need to adjust the motion of our particle so that it moves along the new triangle (Figure 14).

In other words, we want to rotate the current vector of motion of the particle in space so that it is perpendicular to the normal of the new triangle. It turns out that the transformation of the motion vector we need will be equal to the transformation that rotates from the first normal to the second normal (Figure 15).

To rotate the vector in space, we will use a quaternion. A quaternion representing rotation around an axis by an angle can be constructed as follows:

If we consider that the dot product and the vector product of the two normalized vectors are:

then we can directly construct a quaternion representing such a rotation based on the results of the dot product and the vector product *N _{0}* and

*N*. However, if you look at how the components of a quaternion that performs an angle rotation are calculated, you will notice that half of the angle is used. And this means that the

_{1}*N*and

_{0}*N*The quaternion will rotate twice as much as we need. One solution is to compute

_{1}*half-way*Vector Between

*N*and

_{0}*N*, and use dot and vector product

_{1}*N*and

_{0}*half-way*vector to construct the quaternion we need.

We also need to handle two edge cases before Calculating Quaternion:

1. If the dot product *N _{0}* and

*N*is equal to 1, the vectors are already aligned and no rotation is required.

_{1}2. If the dot product *N _{0}* and

*N*is -1, the vectors are opposite and a 180-degree rotation is required. In this case, multiply the velocity vector by -1.

_{1}## 5. Orientation of the particle model in space

To display the model correctly in the scene, you must orient its direction along the motion vector *V*. In this case, the front of the model is pointed in the opposite direction of the axis *Z *(Figure 17).

We can accomplish this orientation by creating a quaternion that represents the rotation from the vector *Forward (-Z)*, which indicates the direction of the model, to the displacement vector. However, after such a rotation, the vector of the model *UP* will be distorted (Figure 18).

We need the vector of the model *UP* was parallel to the normal *N* of the current triangle (Figure 19).

Therefore, we will additionally create a second quaternion that will perform rotation from the distorted vector *UP’* To the Triangle Normal:

## 6. Demonstration

## Conclusion

You can easily master the formulas that were used in the article by solving problems on our website shader-learning.com.

Shader Learning is a platform for learning and practicing writing shader programs in OpenGL. The platform helps you master mathematical models and techniques that are used to create various effects, such as color correction, noise management, raytracing, and lighting.

———-

### Acknowledgment and Usage Notice

The editorial team at TechBurst Magazine acknowledges the invaluable contribution of Алькевич Александр the author of the original article that forms the foundation of our publication. We sincerely appreciate the author’s work. All images in this publication are sourced directly from the original article, where a reference to the author’s profile is provided as well. This publication respects the author’s rights and enhances the visibility of their original work. If there are any concerns or the author wishes to discuss this matter further, we welcome an open dialogue to address potential issues and find an amicable resolution. Feel free to contact us through the ‘Contact Us’ section; the link is available in the website footer.