summaryrefslogtreecommitdiff
path: root/physics/Model.cs
blob: 550e3d8768f9f791cf04c5739abc01e454ce803c (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//Based on http://people.freedesktop.org/~krh/akamaru.git

//http://en.wikipedia.org/wiki/Verlet_integration
//http://www.teknikus.dk/tj/gdc2001.htm

using System;
using System.Collections.Generic;

namespace PhysModel
{
	public struct Point
	{
		public double X;
		public double Y;

		public Point (double x, double y)
		{
			X = x;
			Y = y;
		}

		public static Point Origin = new Point (0, 0);

		public static Point operator + (Point a, Vector b)
		{
			return new Point (a.X + b.X, a.Y + b.Y);
		}

		public static Point operator - (Point a, Vector b)
		{
			return new Point (a.X - b.X, a.Y - b.Y);
		}

		public static Vector operator - (Point a, Point b)
		{
			return new Vector (a.X - b.X, a.Y - b.Y);
		}

		//?
		public static Point operator / (Point a, Vector b)
		{
			return new Point (a.X / b.X, a.Y / b.Y);
		}
	}

	public struct Vector
	{
		public double X;
		public double Y;

		public Vector (double x, double y)
		{
			X = x;
			Y = y;
		}

		public static Vector operator + (Vector a, Vector b)
		{
			return new Vector (a.X + b.X, a.Y + b.Y);
		}

		public static Vector operator - (Vector a, Vector b)
		{
			return new Vector (a.X - b.X, a.Y - b.Y);
		}

		//sanity check this
		/*
		public static Vector operator * (Vector a, Vector b)
		{
			return new Vector (a.X * b.X, a.Y * b.Y);
		}
		*/

		//doesn't make sense
		/*
		public static Vector operator / (Vector a, Vector b)
		{
			return new Vector (a.X / b.X, a.Y / b.Y);
		}
		*/

		public static Vector operator * (Vector a, double b)
		{
			return new Vector (a.X * b, a.Y * b);
		}

		//?
		public static Vector operator * (double a, Vector b)
		{
			return new Vector (b.X * a, b.Y * a);
		}

		public static Vector operator / (Vector a, double b)
		{
			return new Vector (a.X / b, a.Y / b);
		}
	}
	
	public class Model
	{
		public List<Particle> Particles = new List<Particle> ();
		public List<Spacer> Spacers = new List<Spacer> ();
		public List<String> Strings = new List<String> ();
		public List<Stick> Sticks = new List<Stick> ();
		public List<Spring> Springs = new List<Spring> ();
		public List<Anchor> Anchors = new List<Anchor> ();
		public List<Polygon> Polygons = new List<Polygon> ();
		public List<Offset> Offsets = new List<Offset> ();
		public List<OffsetSpring> OffsetSprings = new List<OffsetSpring> ();

		public double K;
		public double Friction;
		public double Elasticity;
		public Vector Gravity;
		public int ConstrainIterations;

		public double Theta;

		public void Add (Particle n)
		{
		}

		public void AccumulateForces ()
		{
			foreach (Particle n in Particles)
				n.AccumulateForces (this);

			foreach (Spring s in Springs)
				s.AccumulateForces (this);
		}

		public void Constrain ()
		{
			foreach (Anchor a in Anchors)
				a.Constrain ();

			foreach (Stick s in Sticks)
				s.Constrain ();

			foreach (Offset o in Offsets)
				o.Constrain ();

			foreach (Particle n in Particles)
				foreach (Polygon p in Polygons)
					p.Constrain (n); //wrong way round
		}

		public void Step (double delta_t)
		{
			AccumulateForces ();

			foreach (Particle n in Particles)
				n.Integrate (delta_t);

			for (int i = 0 ; i != ConstrainIterations ; i++)
				Constrain ();

			Theta += delta_t;
		}

		public Particle FindNearest (Point pos)
		{
			Particle nearest = null;
			double nearest_distance = Double.MaxValue;

			foreach (Particle n in Particles) {
				double distance = n.GetDistance (pos);

				if (nearest == null || distance < nearest_distance) {
					nearest_distance = distance;
					nearest = n;
				}
			}

			return nearest;
		}
	}

	public class Stick
	{
		public Particle A;
		public Particle B;
		public int Length;

		public void Constrain ()
		{
			Point p = A.Position;
			Vector d = B.Position - p;

			double distance = EstimateDistance (d, Length);
			double fraction = (distance - Length) / distance / 2;
			A.Position = p + d * fraction;
			B.Position = p + d * (1 - fraction);
		}

		static double EstimateDistance (Vector d, double r)
		{
			return (r + (d.X * d.X + d.Y * d.Y) / r) / 2;
			//return Math.Sqrt (d.X * d.X + d.Y * d.Y);
		}
	}

	public class String
	{
		public int Length;
	}

	public class Spring : Stick
	{
		public void AccumulateForces (Model model)
		{
			Point pos = A.Position;
			Vector d = B.Position - pos;

			double distance = Math.Sqrt (d.X*d.X + d.Y*d.Y);

			Vector u = d / distance;

			double displacement = distance - Length;

			A.Force += u * model.K * displacement;
			B.Force -= u * model.K * displacement;
		}
	}

	public class OffsetSpring : Spring
	{
	}

	public class Spacer
	{
	}

	public class Anchor
	{
		public Particle Particle;
		public Point Position;
		//public Link Link;
		
		public void Constrain ()
		{
			Particle.Position = Position;
		}
	}

	/*
	public class Rectangle : Polygon
	{
	}
	*/

	public class Polygon
	{
		public List<Point> Points = new List<Point> ();
		public List<Vector> Normals = new List<Vector> ();
		public bool Enclosing;
		public Model Model;

		public bool Contains (Point point)
		{
			for (int i = 0 ; i != Points.Count ; i++) {
				Vector d = point - Points[i];

				if (Normals[i].X * d.X + Normals[i].Y * d.Y >= 0)
					return Enclosing;
			}

			return !Enclosing;
		}

		public void ReflectParticle (Particle n, double elasticity)
		{
			int edge = 0;
			double distance = Double.MinValue;
			Vector norm = new Vector (0, 0);

			for (int i = 0 ; i != Points.Count ; i++) {
				double d = Normals[i].X * (n.Position.X - Points[i].X) +
					Normals[i].Y * (n.Position.Y - Points[i].Y);

				if (d > distance) {
					distance = d;
					edge = i;
					norm = Normals[i];
				}
			}

			n.Position -= (1 + elasticity) * distance * norm;

			distance = norm.X * (n.PreviousPosition.X - Points[edge].X) +
					norm.Y * (n.PreviousPosition.Y - Points[edge].Y);

			n.PreviousPosition -= (1 + elasticity) * distance * norm;
		}

		//TODO: maybe this should be in Particle or elsewhere?
		public void Constrain (Particle n)
		{
			if (Contains (n.Position))
					ReflectParticle (n, Model.Elasticity);
		}

		public void Init ()
		{
			//Compute outward pointing normals
			Normals = new List<Vector> (Points.Count);
			for (int i = 0 ; i != Points.Count ; i++) {
				int j = (i + 1) % Points.Count;
				Vector d = Points[j] - Points[i];
				double length = Math.Sqrt (d.X*d.X + d.Y*d.Y);
				//Normals[i] = new Vector (-d.Y / length, d.X / length);
				Normals.Add (new Vector (-d.Y / length, d.X / length));
			}
		}
	}

	public class Offset
	{
		public Offset (Vector delta)
		{
			this.Delta = delta;
		}

		public List<Particle> Particles = new List<Particle> ();
		public Vector Delta;
		//public Link Link;

		public void Constrain ()
		{
			Point pos = Point.Origin;

			for (int i = 0 ; i != Particles.Count ; i++)
				pos += Particles[i].Position - Point.Origin;

			pos = pos / new Vector (Particles.Count, Particles.Count) - Delta * (Particles.Count - 1) / 2;

			for (int i = 0 ; i != Particles.Count ; i++)
				Particles[i].Position = pos + Delta * (double)i;
		}
	}
}