-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpring.h
47 lines (39 loc) · 964 Bytes
/
Spring.h
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
42
43
44
45
46
#pragma once
#include "Mesh.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <vector>
// 质点间弹簧
struct SprP {
unsigned int v1;
unsigned int v2;
float length;
};
// 约束弹簧
struct SprC {
unsigned int v;
glm::vec3 pos;
};
class Spring
{
public:
Spring(Mesh* mesh_ptr) : mesh_ptr_(mesh_ptr) {
initSprings();
velocities_.resize(mesh_ptr->vertices.size(), glm::vec3(0.0f, 0.0f, 0.0f));
accelerations_.resize(mesh_ptr->vertices.size(), glm::vec3(0.0f, 0.0f, 0.0f));
}
void update(float delta_time);
void restoreVelocities();
void restoreParticles();
private:
Mesh* mesh_ptr_;
std::vector<SprP> springs_p_;
std::vector<SprC> springs_c_;
float c_p = 2.0f, c_c = 2.0f; // 质点间作用与约束作用的常数项
std::vector<glm::vec3> accelerations_;
std::vector<glm::vec3> velocities_;
void initSprings();
void updateAccelerations();
void updateVelocities(float delta_time);
void updatePositions(float delta_time);
};