110 likes | 167 Views
Cloth Simulation. By Chris Szendrovits based on Jim Adams “Soft Body Mesh” demo. Cloth Points & Springs. Cloth Point (mesh vertex). Mesh vertices need the ability to deform and then recover This is done by adding cloth points and cloth springs
E N D
Cloth Simulation By Chris Szendrovits based on Jim Adams “Soft Body Mesh” demo
Cloth Points & Springs Cloth Point (mesh vertex) • Mesh vertices need the ability to deform and then recover • This is done by adding cloth points and cloth springs • Cloth Points have a position and a mass, and begin with the same position as the mesh vertices • Cloth Springs contain two points, a resting length, a damping value, and a stiffness value Cloth Spring (polygon edge)
Cloth Point Example struct sClothPoint { D3DXVECTOR3 m_vecPos; // Current point coords D3DXVECTOR3 m_vecForce; // Force applied to point D3DXVECTOR3 m_vecVelocity; // Velocity of point float m_Mass; // Mass of object };
Cloth Spring Example class cClothSpring { public: int m_Point1; // First point in spring int m_Point2; // Second point in spring float m_RestingLength; // Resting length of spring float m_Stiffness; // Spring stiffness constant value float m_Damping; // Spring damping value cClothSpring *m_Next; // Next in linked list public: cClothSpring() { m_Next = NULL; } ~cClothSpring() { delete m_Next; m_Next = NULL; } };
Applying Forces to Cloth • A number of different forces may affect a cloth’s points: wind, gravity, friction, and collision • The springs themselves also apply forces to eachother • A Spring will expand and contract according to their damping and stiffness values • Stiffness defines how much force a spring exerts in an attempt to return to its natural length • Damping smooths out the motion of the points by reducing the amount of force a spring exerts
Wind Forces • Applying wind forces isn’t as simple as adding a directional vector to the cloth points (eg. Gravity) • The applied force is proportional to the surface area of the triangle • Luckily, using the cross product to calculate the triangle normal gives you a length vector that is proportional to the area of the triangle D3DXVec3Cross(&vecNormal, &vecEdge1, &vecEdge2); D3DXVec3Normalize(&vecNormal, &vecNormal);
Wind Forces continued.. • The wind force will always be in the direction of the triangle normal • First we need to find, using a dot product calculation, the angle between the wind and the normal. float angle = D3DXVec3Dot(&vecNormal, vecWind); • By using the angle to scale the normal we can calculate the amount of force to apply to each cloth point D3DXVECTOR3 vecWindForce = vecNormal * angle; ClothPoint[i].m_vecForce += vecWindForce; Normal Force Wind
Spring Forces • Each spring begins with an initial resting length • This is the distance between the two points that the spring connects D3DXVECTOR3 vecSpring = ClothPoint[a].m_vecPos – ClothPoint[b].m_vecPos; float SpringLength = D3DXVec3Length(&vecSpring);
Spring Forces continued.. • When the cloth points begin to move, we need to calculate its current length in comparison with its resting length • With this value we either pull or push the cloth points back to their natural resting length float SpringForce = m_Stiffness * (SpringLength – m_RestingLength); vecSpring /= SpringLength; // Normal the spring to get the direction vecSpring *= SpringForce; // Calculate a force vector // Add forces to cloth points ClothPoint[a].m_vecForce += vecSpring; ClothPoint[b].m_vecForce -= vecSpring;
Damping Forces • As the cloth moves through air, friction forces, or linear damping, will slow it down • Damping is a force that is proportional to the velocity (F=kV) D3DXVECTOR3 vecRelVelocity = ClothPoint[a].m_vecVelocity – ClothPoint[b].m_vecVelocity; float Velocity = D3DXVec3Dot(&vecRelVelocity, &vecSpring) / SpringLength; float DampingForce = m_Damping * Velocity; vecSpring *= (SpringForce + DampingForce);
Summary • First create the cloth points and cloth springs • Next update the cloth points based on all the forces being exerted on them • Damping, Spring, Wind, Gravity, Collision • Finally, rebuild the mesh based on the new position of the cloth points