-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspring.cpp
More file actions
42 lines (31 loc) · 1.7 KB
/
spring.cpp
File metadata and controls
42 lines (31 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "spring.h"
#include "global.h"
#include <GLFW/glfw3.h>
Spring::Spring(){
this->stiffness = 50.0f;
this->rest_length = 0.2f;
}
Spring::Spring(float stiff, float rest_len, ClothVertex* v1, ClothVertex* v2){
this->stiffness = stiff;
this->rest_length = rest_len;
this->end_vertices[0] = v1;
this->end_vertices[1] = v2;
}
glm::vec3 Spring::calculateSpringForce(){
return -this->stiffness*(glm::normalize((this->end_vertices[1])->position - (this->end_vertices[0])->position)) *
(glm::length((this->end_vertices[1])->position - (this->end_vertices[0])->position) - this->rest_length);
}
void Spring::AddForce(float delta_t){
float damping_coef = 0.05f; // should fiddle with the the damping coeficient
glm::vec3 damping_force0 = -damping_coef * ((this->end_vertices[0])->position - (this->end_vertices[0])->previous_position) / delta_t;
glm::vec3 damping_force1 = -damping_coef * ((this->end_vertices[1])->position - (this->end_vertices[1])->previous_position) / delta_t;
glm::vec3 force = this->calculateSpringForce();
glm::vec3 p1 = this->end_vertices[0]->position;
glm::vec3 p2 = this->end_vertices[1]->position;
float time = glfwGetTime();
float wind_strength = 0.03;
glm::vec3 wind1 = wind_strength * glm::vec3(sin(p1.x * p1.y * time), cos(p1.z * time), sin(cos(9*p1.x*p1.y*p1.z*time)));
glm::vec3 wind2 = wind_strength * glm::vec3(sin(p2.x * p2.y * time), cos(p2.z * time), sin(cos(9*p2.x*p2.y*p2.z*time)));
(this->end_vertices[0])->AddForce(-force + damping_force0 + (this->end_vertices[0])->mass * Global::gravity);
(this->end_vertices[1])->AddForce( force + damping_force1 + (this->end_vertices[1])->mass * Global::gravity);
}