@@ -20,12 +20,27 @@ namespace Hop::System::Physics
20
20
using Hop::Object::Component::cTransform;
21
21
using Hop::Object::Component::cPhysics;
22
22
23
+ /* *
24
+ * @brief A point in a collision mesh.
25
+ *
26
+ */
23
27
struct MeshPoint
24
28
{
29
+ /* *
30
+ * @brief Construct a new Mesh Point from a position and radius.
31
+ *
32
+ * @param x x coordinate.
33
+ * @param y y coordinate.
34
+ * @param r radius.
35
+ */
25
36
MeshPoint (double x, double y, double r)
26
37
: x(x), y(y), r(r)
27
38
{}
28
39
40
+ /* *
41
+ * @brief Construct a new Mesh Point at (0,0) with radius 0.
42
+ *
43
+ */
29
44
MeshPoint ()
30
45
: x(0.0 ), y(0.0 ), r(0.0 )
31
46
{}
@@ -40,16 +55,36 @@ namespace Hop::System::Physics
40
55
}
41
56
};
42
57
58
+ /* *
59
+ * @brief A rectangular mesh point.
60
+ *
61
+ */
43
62
struct MeshRectangle : public MeshPoint
44
63
{
45
64
65
+ /* *
66
+ * @brief Construct a new Mesh Rectangle with points at 0.0.
67
+ *
68
+ */
46
69
MeshRectangle ()
47
70
: MeshRectangle(0.0 , 0.0 ,
48
71
0.0 , 0.0 ,
49
72
0.0 , 0.0 ,
50
73
0.0 , 0.0 )
51
74
{}
52
75
76
+ /* *
77
+ * @brief Construct a new Mesh Rectangle from vertices.
78
+ *
79
+ * @param llx lower left x.
80
+ * @param lly lower left y.
81
+ * @param ulx upper left x.
82
+ * @param uly upper left y.
83
+ * @param urx upper right x.
84
+ * @param ury upper right y.
85
+ * @param lrx lower right x.
86
+ * @param lry lower right y.
87
+ */
53
88
MeshRectangle
54
89
(
55
90
double llx, double lly,
@@ -102,16 +137,24 @@ namespace Hop::System::Physics
102
137
double llx, lly, ulx, uly, urx, ury, lrx, lry;
103
138
};
104
139
140
+ /* *
141
+ * @brief A mesh of collideable primitives.
142
+ *
143
+ */
105
144
struct CollisionMesh
106
145
{
107
146
CollisionMesh ()
108
147
{}
109
- // construct a mesh around a model space polygon
110
- // with vertices v with each mesh vertex having
111
- // radius r in model space
112
- // CollisionMesh(std::vector<Vertex> v, double r = 0.01);
113
148
114
- // construct a mesh from given points
149
+ /* *
150
+ * @brief Construct a new Collision Mesh from primitives.
151
+ *
152
+ * @param v list of collideable primitives.
153
+ * @param x x world coordinate of mesh.
154
+ * @param y y world coordinate of mesh.
155
+ * @param theta orientation of mesh.
156
+ * @param scale scale of mesh.
157
+ */
115
158
CollisionMesh
116
159
(
117
160
std::vector<std::shared_ptr<CollisionPrimitive>> v,
@@ -127,6 +170,11 @@ namespace Hop::System::Physics
127
170
updateWorldMesh (transform, phys, 0.0 );
128
171
}
129
172
173
+ /* *
174
+ * @brief Construct a new Collision Mesh from a primitives list.
175
+ *
176
+ * @param v list of collideable primitives.
177
+ */
130
178
CollisionMesh
131
179
(
132
180
std::vector<std::shared_ptr<CollisionPrimitive>> v
@@ -141,6 +189,11 @@ namespace Hop::System::Physics
141
189
computeRadius ();
142
190
}
143
191
192
+ /* *
193
+ * @brief Construct a new Collision Mesh from another.
194
+ *
195
+ * @param m the mesh to copy.
196
+ */
144
197
CollisionMesh
145
198
(
146
199
CollisionMesh & m
@@ -161,6 +214,13 @@ namespace Hop::System::Physics
161
214
someRectangles = m.someRectangles ;
162
215
}
163
216
217
+ /* *
218
+ * @brief Construct a new Collision Mesh from model and world vertices.
219
+ *
220
+ * @param model model positions of mesh primitives.
221
+ * @param world world positions of mesh primitives.
222
+ * @param tags tags of mesh primitives for sub-meshing.
223
+ */
164
224
CollisionMesh
165
225
(
166
226
std::vector<std::shared_ptr<MeshPoint>> model,
@@ -177,6 +237,12 @@ namespace Hop::System::Physics
177
237
updateTags ();
178
238
}
179
239
240
+ /* *
241
+ * @brief Obtain all primitives with the given tag.
242
+ *
243
+ * @param t tag to select.
244
+ * @return (sub) CollisionMesh selected by tag.
245
+ */
180
246
CollisionMesh getSubMesh (uint64_t t)
181
247
{
182
248
auto model = getModelByTag (t);
@@ -185,15 +251,29 @@ namespace Hop::System::Physics
185
251
return CollisionMesh (model, world, tags);
186
252
}
187
253
254
+ /* *
255
+ * @brief Flag mesh as needing an initial update.
256
+ *
257
+ */
188
258
void reinitialise () { needsInit = true ; }
189
259
260
+ /* *
261
+ * @brief Set the transform of the mesh.
262
+ *
263
+ * @param t new transform of the mesh.
264
+ */
190
265
void transform (cTransform t)
191
266
{
192
267
needsInit = true ;
193
268
cPhysics phys (t.x ,t.y ,t.theta );
194
269
updateWorldMesh (t, phys, 0.0 );
195
270
}
196
271
272
+ /* *
273
+ * @brief Add a primitive to the mesh.
274
+ *
275
+ * @param c new primitive.
276
+ */
197
277
void add (std::shared_ptr<CollisionPrimitive> c)
198
278
{
199
279
@@ -279,6 +359,11 @@ namespace Hop::System::Physics
279
359
needsInit = true ;
280
360
}
281
361
362
+ /* *
363
+ * @brief Remove primitive at index i.
364
+ *
365
+ * @param i index of primitive to remove.
366
+ */
282
367
void remove (size_t i)
283
368
{
284
369
vertices.erase (vertices.begin ()+i);
@@ -290,6 +375,13 @@ namespace Hop::System::Physics
290
375
updateTags ();
291
376
}
292
377
378
+ /* *
379
+ * @brief Check if (x,y) hits a primitive.
380
+ *
381
+ * @param x x coordinate of test point.
382
+ * @param y y coordinate of test point.
383
+ * @return int index of the first primitive click (or -1 if not clicked).
384
+ */
293
385
int clicked (float x, float y)
294
386
{
295
387
@@ -310,21 +402,47 @@ namespace Hop::System::Physics
310
402
311
403
size_t size (){return vertices.size ();}
312
404
405
+ /* *
406
+ * @brief Get a model space primitive.
407
+ *
408
+ * @param i index of primitive
409
+ * @return std::shared_ptr<MeshPoint> model space primitive.
410
+ */
313
411
std::shared_ptr<MeshPoint> getModelVertex (size_t i)
314
412
{
315
413
return vertices[i];
316
414
}
317
415
416
+ /* *
417
+ * @brief Get a world space primitive.
418
+ *
419
+ * @param i index of primitive.
420
+ * @return std::shared_ptr<CollisionPrimitive> world space primitive.
421
+ */
318
422
std::shared_ptr<CollisionPrimitive> getMeshVertex (size_t i)
319
423
{
320
424
return worldVertices[i];
321
425
}
322
426
427
+ /* *
428
+ * @brief Get a world space primitive.
429
+ *
430
+ * @param i index of primitive.
431
+ * @return std::shared_ptr<CollisionPrimitive> world space primitive.
432
+ */
323
433
std::shared_ptr<CollisionPrimitive> operator [](size_t i)
324
434
{
325
435
return worldVertices[i];
326
436
}
327
437
438
+ /* *
439
+ * @brief Update the mesh to a new transform and physics component.
440
+ *
441
+ * @remark Will integrate mesh points (i.e. handle soft meshes).
442
+ * @param transform new transform for the mesh.
443
+ * @param physics new physics component for the mesh.
444
+ * @param dt timestep.
445
+ */
328
446
void updateWorldMesh (
329
447
cTransform & transform,
330
448
cPhysics & physics,
@@ -342,27 +460,62 @@ namespace Hop::System::Physics
342
460
}
343
461
}
344
462
463
+ /* *
464
+ * @brief Updates the mesh if it is rigid.
465
+ *
466
+ * @param transform new transform.
467
+ * @param dt timestep.
468
+ */
345
469
void updateWorldMeshRigid (
346
470
const cTransform & transform,
347
471
double dt
348
472
);
349
473
474
+ /* *
475
+ * @brief Update the world mesh if it is soft.
476
+ *
477
+ * @remark Applies internal soft-body forces.
478
+ * @param transform new transform.
479
+ * @param physics mesh physics components.
480
+ * @param dt timestep.
481
+ */
350
482
void updateWorldMeshSoft (
351
483
cTransform & transform,
352
484
cPhysics & physics,
353
485
double dt
354
486
);
355
487
488
+ /* *
489
+ * @brief The optimal angle to represent the mesh in world space.
490
+ *
491
+ * @param x x coordinate of the mesh.
492
+ * @param y y coordinate of the mesh.
493
+ * @param scaleX scale of mesh in x.
494
+ * @param scaleY scale of mesh in y.
495
+ * @return double optimal orientation.
496
+ */
356
497
double bestAngle (double x, double y, double scaleX, double scaleY);
498
+
357
499
void centerOfMassWorld (double & cx, double & cy);
500
+
501
+ /* *
502
+ * @brief Rebase coordinates to centre of mess.
503
+ *
504
+ */
358
505
void modelToCenterOfMassFrame ();
359
506
360
507
double momentOfInertia (double x, double y, double mass);
508
+
361
509
void computeRadius ();
510
+
362
511
double getRadius (){return radius;}
363
512
364
513
bool getIsRigid (){ return isRigid; }
365
514
515
+ /* *
516
+ * @brief Determine if all mesh points are rigid.
517
+ *
518
+ */
366
519
void calculateIsRigid ()
367
520
{
368
521
for (auto v : worldVertices)
@@ -425,6 +578,12 @@ namespace Hop::System::Physics
425
578
}
426
579
}
427
580
581
+ /* *
582
+ * @brief Get all world space primitives by tag.
583
+ *
584
+ * @param t tag to match.
585
+ * @return std::vector<std::shared_ptr<CollisionPrimitive>> world primitives matching the tag.
586
+ */
428
587
std::vector<std::shared_ptr<CollisionPrimitive>> getByTag (uint64_t t)
429
588
{
430
589
std::vector<std::shared_ptr<CollisionPrimitive>> v;
@@ -444,6 +603,12 @@ namespace Hop::System::Physics
444
603
return v;
445
604
}
446
605
606
+ /* *
607
+ * @brief Get all model space primitives by tag.
608
+ *
609
+ * @param t tag to select.
610
+ * @return std::vector<std::shared_ptr<MeshPoint>> model primitives matching tag.
611
+ */
447
612
std::vector<std::shared_ptr<MeshPoint>> getModelByTag (uint64_t t)
448
613
{
449
614
std::vector<std::shared_ptr<MeshPoint>> v;
@@ -463,6 +628,11 @@ namespace Hop::System::Physics
463
628
return v;
464
629
}
465
630
631
+ /* *
632
+ * @brief Remove all primitives matching tag.
633
+ *
634
+ * @param t tag to match.
635
+ */
466
636
void removeByTag (uint64_t t)
467
637
{
468
638
std::vector<std::shared_ptr<CollisionPrimitive>> v;
@@ -486,11 +656,22 @@ namespace Hop::System::Physics
486
656
}
487
657
}
488
658
659
+ /* *
660
+ * @brief Get the world-space bounding box.
661
+ *
662
+ * @return Hop::Maths::BoundingBox
663
+ */
489
664
Hop::Maths::BoundingBox getBoundingBox () const
490
665
{
491
666
return getBoundingBox (worldVertices);
492
667
}
493
668
669
+ /* *
670
+ * @brief Get the bounding box of a list of primitives.
671
+ *
672
+ * @param c primitives to bound.
673
+ * @return Hop::Maths::BoundingBox
674
+ */
494
675
Hop::Maths::BoundingBox getBoundingBox
495
676
(
496
677
const std::vector<std::shared_ptr<CollisionPrimitive>> & c
@@ -518,6 +699,12 @@ namespace Hop::System::Physics
518
699
return Hop::Maths::boundingBox (v, r);
519
700
}
520
701
702
+ /* *
703
+ * @brief Get the bounding box of all primitives with tag.
704
+ *
705
+ * @param tag tag to match.
706
+ * @return Hop::Maths::BoundingBox
707
+ */
521
708
Hop::Maths::BoundingBox getBoundingBox (uint64_t tag)
522
709
{
523
710
std::vector<std::shared_ptr<CollisionPrimitive>> c = getByTag (tag);
0 commit comments