T4 Add Spring Force using Hooke's Law
Find getSpringForce
// TODO: calculate spring force
glm::vec3 ClothSim::getSpringForce(int direction, int id) {
// the rest spring length
float lenRest = 0;
if (direction == DIRS.NORTH || direction == DIRS.SOUTH) lenRest = restLengthZ;
else if(direction == DIRS.WEST || direction == DIRS.EAST) lenRest = restLengthX;
else lenRest = restLengthXZ;
// the spring vector
glm::vec3 vSpring = mesh->vertices[id].pos - mesh->vertices[getId(direction, id)].pos;
// the spring length
float lenSpring = glm::length(vSpring); // distance
// the unit vector of spring direction
glm::vec3 vSpringUnit = vSpring /lenSpring;
// [TODO 1]: calculate spring force vector based on Hooke's law F = -k delta_x
// 1.1 calculate the length change delta_len
// replace 0 with your formula
float delta_len = 0;
// 1.2 Use Hooke's law F = -k * delta_len * vSpringUnit
// k : spring_factor
// replace glm::vec3(0.0) with your formula
glm::vec3 spring_force = glm::vec3(0.0);
return spring_force;
}Calculate the spring force using Hooke's Law

Add spring force in [TODO 2] vertex forces
Build and Run


Add the wind

Last updated