T4 Add Spring Force using Hooke's Law

Find getSpringForce

Search [TODO 1] in ClothSim.cpp, you are expected to find the following:

// 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

Complete 1.1 and 1.2 using the Hooke's Law

Add spring force in [TODO 2] vertex forces

variable name

spring force: spring

Add that to forces[v] under [TODO 2]

Build and Run

Press the Space Bar, you should be able to see the cloth simulation and finally it looks like the following

In another view

Add the wind

You can also add the wind force under [TODO 2], press Space Bar and press v, you are going to see the effects of the wind force.

Last updated