1 #ifndef STAN_SPRING_NETWORK_H
2 #define STAN_SPRING_NETWORK_H
3 
4 #include "vec3n.h"
5 
6 #define SPRING_STRUCT (0)
7 #define SPRING_SHEAR (1)
8 #define SPRING_BEND (2)
9 
10 class SpringNetwork
11 {
12 public:
13 	class Spring
14 	{
15 	public:
16 		int type;  // index into coefficients spring_k[]
17 		float restlen;
18 		int a, b;      // spring endpoints vector indices
19 		int iab, iba;  // indices into off-diagonal blocks of sparse matrix
Spring()20 		Spring() {}
Spring(int _type,int _a,int _b,float _restlen)21 		Spring(int _type, int _a, int _b, float _restlen) : type(_type), a(_a), b(_b), restlen(_restlen) { iab = iba = -1; }
22 	};
23 	Array<Spring> springs;
24 	float3N X;        // positions of all points
25 	float3N V;        // velocities
26 	float3N F;        // force on each point
27 	float3N dV;       // change in velocity
28 	float3Nx3N A;     // big matrix we solve system with
29 	float3Nx3N dFdX;  // big matrix of derivative of force wrt position
30 	float3Nx3N dFdV;  // big matrix of derivative of force wrt velocity
31 	float3Nx3N S;     // used for our constraints - contains only some diagonal blocks as needed S[i,i]
32 	int awake;
33 	float3 bmin, bmax;
34 	union {
35 		struct
36 		{
37 			float spring_struct;
38 			float spring_shear;
39 			float spring_bend;
40 		};
41 		float spring_k[3];
42 	};
43 	float spring_damp;
44 	float spring_air;
45 	float cloth_step;  // delta time for cloth
46 	float3 cloth_gravity;
47 	float cloth_sleepthreshold;
48 	int cloth_sleepcount;
49 
50 	SpringNetwork(int _n);
51 	Spring &AddBlocks(Spring &s);
CreateSpring(int type,int a,int b,float restlen)52 	Spring &CreateSpring(int type, int a, int b, float restlen) { return AddBlocks(springs.Add(Spring(type, a, b, restlen))); }
CreateSpring(int type,int a,int b)53 	Spring &CreateSpring(int type, int a, int b) { return CreateSpring(type, a, b, magnitude(X[b] - X[a])); }
UpdateLimits()54 	void UpdateLimits() { BoxLimits(X.element, X.count, bmin, bmax); }
Wake()55 	void Wake() { awake = cloth_sleepcount; }
56 	void Simulate(float dt);
57 	void PreSolveSpring(const Spring &s);
58 	void CalcForces();
59 };
60 
61 #endif  //STAN_SPRING_NETWORK_H
62