-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlatBody.cs
351 lines (289 loc) · 11.1 KB
/
FlatBody.cs
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
343
344
345
346
347
348
349
350
351
using System;
namespace FlatPhysics
{
public enum ShapeType
{
Circle = 0,
Box = 1,
}
public sealed class FlatBody
{
private FlatVector position;
private FlatVector linearVelocity;
private float angle;
private float angularVelocity;
private FlatVector force;
public readonly ShapeType ShapeType;
public readonly float Density;
public readonly float Mass;
public readonly float InvMass;
public readonly float Restitution;
public readonly float Area;
public readonly float Inertia;
public readonly float InvInertia;
public readonly bool IsStatic;
public readonly float Radius;
public readonly float Width;
public readonly float Height;
public readonly float StaticFriction;
public readonly float DynamicFriction;
private readonly FlatVector[] vertices;
private FlatVector[] transformedVertices;
private FlatAABB aabb;
private bool transformUpdateRequired;
private bool aabbUpdateRequired;
public FlatVector Position
{
get { return this.position; }
}
public FlatVector LinearVelocity
{
get { return this.linearVelocity; }
internal set { this.linearVelocity = value; }
}
public float Angle
{
get { return this.angle; }
}
public float AngularVelocity
{
get { return this.angularVelocity; }
internal set { this.angularVelocity = value; }
}
private FlatBody(float density, float mass, float inertia, float restitution, float area,
bool isStatic, float radius, float width, float height, FlatVector[] vertices, ShapeType shapeType)
{
this.position = FlatVector.Zero;
this.linearVelocity = FlatVector.Zero;
this.angle = 0f;
this.angularVelocity = 0f;
this.force = FlatVector.Zero;
this.ShapeType = shapeType;
this.Density = density;
this.Mass = mass;
this.InvMass = mass > 0f ? 1f / mass : 0f;
this.Inertia = inertia;
this.InvInertia = inertia > 0f ? 1f / inertia : 0f;
this.Restitution = restitution;
this.Area = area;
this.IsStatic = isStatic;
this.Radius = radius;
this.Width = width;
this.Height = height;
this.StaticFriction = 0.6f;
this.DynamicFriction = 0.4f;
if(this.ShapeType is ShapeType.Box)
{
this.vertices = vertices;
this.transformedVertices = new FlatVector[this.vertices.Length];
}
else
{
this.vertices = null;
this.transformedVertices = null;
}
this.transformUpdateRequired = true;
this.aabbUpdateRequired = true;
}
private static FlatVector[] CreateBoxVertices(float width, float height)
{
float left = -width / 2f;
float right = left + width;
float bottom = -height / 2f;
float top = bottom + height;
FlatVector[] vertices = new FlatVector[4];
vertices[0] = new FlatVector(left, top);
vertices[1] = new FlatVector(right, top);
vertices[2] = new FlatVector(right, bottom);
vertices[3] = new FlatVector(left, bottom);
return vertices;
}
private static int[] CreateBoxTriangles()
{
int[] triangles = new int[6];
triangles[0] = 0;
triangles[1] = 1;
triangles[2] = 2;
triangles[3] = 0;
triangles[4] = 2;
triangles[5] = 3;
return triangles;
}
public FlatVector[] GetTransformedVertices()
{
if(this.transformUpdateRequired)
{
FlatTransform transform = new FlatTransform(this.position, this.angle);
for(int i = 0; i < this.vertices.Length; i++)
{
FlatVector v = this.vertices[i];
this.transformedVertices[i] = FlatVector.Transform(v, transform);
}
FlatWorld.TransformCount++;
}
else
{
FlatWorld.NoTransformCount++;
}
this.transformUpdateRequired = false;
return this.transformedVertices;
}
public FlatAABB GetAABB()
{
if (this.aabbUpdateRequired)
{
float minX = float.MaxValue;
float minY = float.MaxValue;
float maxX = float.MinValue;
float maxY = float.MinValue;
if (this.ShapeType is ShapeType.Box)
{
FlatVector[] vertices = this.GetTransformedVertices();
for (int i = 0; i < vertices.Length; i++)
{
FlatVector v = vertices[i];
if (v.X < minX) { minX = v.X; }
if (v.X > maxX) { maxX = v.X; }
if (v.Y < minY) { minY = v.Y; }
if (v.Y > maxY) { maxY = v.Y; }
}
}
else if (this.ShapeType is ShapeType.Circle)
{
minX = this.position.X - this.Radius;
minY = this.position.Y - this.Radius;
maxX = this.position.X + this.Radius;
maxY = this.position.Y + this.Radius;
}
else
{
throw new Exception("Unknown ShapeType.");
}
this.aabb = new FlatAABB(minX, minY, maxX, maxY);
}
this.aabbUpdateRequired = false;
return this.aabb;
}
internal void Step(float time, FlatVector gravity, int iterations)
{
if(this.IsStatic)
{
return;
}
time /= (float)iterations;
// force = mass * acc
// acc = force / mass;
//FlatVector acceleration = this.force / this.Mass;
//this.linearVelocity += acceleration * time;
this.linearVelocity += gravity * time;
this.position += this.linearVelocity * time;
this.angle += this.angularVelocity * time;
this.force = FlatVector.Zero;
this.transformUpdateRequired = true;
this.aabbUpdateRequired = true;
}
public void Move(FlatVector amount)
{
this.position += amount;
this.transformUpdateRequired = true;
this.aabbUpdateRequired = true;
}
public void MoveTo(FlatVector position)
{
this.position = position;
this.transformUpdateRequired = true;
this.aabbUpdateRequired = true;
}
public void Rotate(float amount)
{
this.angle += amount;
this.transformUpdateRequired = true;
this.aabbUpdateRequired = true;
}
public void RotateTo(float angle)
{
this.angle = angle;
this.transformUpdateRequired = true;
this.aabbUpdateRequired = true;
}
public void AddForce(FlatVector amount)
{
this.force = amount;
}
public static bool CreateCircleBody(float radius, float density, bool isStatic, float restitution, out FlatBody body, out string errorMessage)
{
body = null;
errorMessage = string.Empty;
float area = radius * radius * MathF.PI;
if(area < FlatWorld.MinBodySize)
{
errorMessage = $"Circle radius is too small. Min circle area is {FlatWorld.MinBodySize}.";
return false;
}
if(area > FlatWorld.MaxBodySize)
{
errorMessage = $"Circle radius is too large. Max circle area is {FlatWorld.MaxBodySize}.";
return false;
}
if (density < FlatWorld.MinDensity)
{
errorMessage = $"Density is too small. Min density is {FlatWorld.MinDensity}";
return false;
}
if (density > FlatWorld.MaxDensity)
{
errorMessage = $"Density is too large. Max density is {FlatWorld.MaxDensity}";
return false;
}
restitution = FlatMath.Clamp(restitution, 0f, 1f);
float mass = 0f;
float inertia = 0f;
if (!isStatic)
{
// mass = area * depth * density
mass = area * density;
inertia = (1f / 2f) * mass * radius * radius;
}
body = new FlatBody(density, mass, inertia, restitution, area, isStatic, radius, 0f, 0f, null, ShapeType.Circle);
return true;
}
public static bool CreateBoxBody(float width, float height, float density, bool isStatic, float restitution, out FlatBody body, out string errorMessage)
{
body = null;
errorMessage = string.Empty;
float area = width * height;
if (area < FlatWorld.MinBodySize)
{
errorMessage = $"Area is too small. Min area is {FlatWorld.MinBodySize}.";
return false;
}
if (area > FlatWorld.MaxBodySize)
{
errorMessage = $"Area is too large. Max area is {FlatWorld.MaxBodySize}.";
return false;
}
if (density < FlatWorld.MinDensity)
{
errorMessage = $"Density is too small. Min density is {FlatWorld.MinDensity}";
return false;
}
if (density > FlatWorld.MaxDensity)
{
errorMessage = $"Density is too large. Max density is {FlatWorld.MaxDensity}";
return false;
}
restitution = FlatMath.Clamp(restitution, 0f, 1f);
float mass = 0f;
float inertia = 0f;
if (!isStatic)
{
// mass = area * depth * density
mass = area * density;
inertia = (1f / 12) * mass * (width * width + height * height);
}
FlatVector[] vertices = FlatBody.CreateBoxVertices(width, height);
body = new FlatBody(density, mass, inertia, restitution, area, isStatic, 0f, width, height, vertices, ShapeType.Box);
return true;
}
}
}