PeriDyno 1.2.1
Loading...
Searching...
No Matches
Vehicle.cpp
Go to the documentation of this file.
1#include "Vehicle.h"
2
3#include "Module/CarDriver.h"
4
5#include "Mapping/DiscreteElementsToTriangleSet.h"
8
9//Modeling
10#include "GltfFunc.h"
11
12//Rigidbody
14
15//Rendering
17
18
19namespace dyno
20{
21 //Jeep
22 IMPLEMENT_TCLASS(Jeep, TDataType)
23
24 template<typename TDataType>
26 ArticulatedBody<TDataType>()
27 {
28 auto driver = std::make_shared<CarDriver<DataType3f>>();
29 this->stateTopology()->connect(driver->inTopology());
30 this->animationPipeline()->pushModule(driver);
31 }
32
33 template<typename TDataType>
35 {
36
37 }
38
39 template<typename TDataType>
41 {
43 this->clearVechicle();
44
45 std::string filename = getAssetPath() + "Jeep/JeepGltf/jeep.gltf";
46 if (this->varFilePath()->getValue() != filename)
47 {
48 this->varFilePath()->setValue(FilePath(filename));
49 }
50
51 auto instances = this->varVehiclesTransform()->getValue();
52 uint vehicleNum = instances.size();
53 for (size_t i = 0; i < vehicleNum; i++)
54 {
55 RigidBodyInfo rigidbody;
56 rigidbody.bodyId = i;
57 rigidbody.friction = this->varFrictionCoefficient()->getValue();
58
59 auto texMesh = this->stateTextureMesh()->constDataPtr();
60
61 //wheel
62 std::vector <int> Wheel_Id = { 0,1,2,3 };
63 std::map<int, CapsuleInfo> wheels;
64 std::map<int, std::shared_ptr<PdActor>> actors;
65 //Capsule
66 for (auto it : Wheel_Id)
67 {
68 auto up = texMesh->shapes()[it]->boundingBox.v1;
69 auto down = texMesh->shapes()[it]->boundingBox.v0;
70
71 wheels[it].center = Vec3f(0.0f);
72 wheels[it].rot = Quat1f(M_PI / 2, Vec3f(0, 0, 1));
73 wheels[it].halfLength = 0.1;
74 wheels[it].radius = std::abs(up.y - down.y) / 2;
75
76 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[it]->boundingTransform.translation()) + instances[i].translation();
77 rigidbody.angle = Quat1f(instances[i].rotation());
78
79 actors[it] = this->addCapsule(wheels[it], rigidbody, 100);
80 }
81
82
83 //body
84 int body = 5;
85 int backWheel = 4;
86
87 std::vector <int> box_Id = { body,backWheel };
88 std::map<int, BoxInfo> boxs;
89 //box
90 for (auto it : box_Id)
91 {
92 auto up = texMesh->shapes()[it]->boundingBox.v1;
93 auto down = texMesh->shapes()[it]->boundingBox.v0;
94
95 boxs[it].center = Vec3f(0.0f);
96
97 boxs[it].halfLength = (up - down) / 2;
98
99 rigidbody.offset = Vec3f(0.0f, 0.0f, 0.0f);
100 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[it]->boundingTransform.translation()) + instances[i].translation();
101 rigidbody.angle = Quat1f(instances[i].rotation());
102 actors[it] = this->addBox(boxs[it], rigidbody, 100);
103 }
104
105 //bindShapetoActor
106 for (auto it : box_Id)
107 {
108 this->bindShape(actors[it], Pair<uint, uint>(it, i));
109 }
110 //bindShapetoActor
111 for (auto it : Wheel_Id)
112 {
113 this->bindShape(actors[it], Pair<uint, uint>(it, i));
114 }
115
116 rigidbody.offset = Vec3f(0);
117
118 Real wheel_velocity = 10;
119
120 //wheel to Body
121 for (auto it : Wheel_Id)
122 {
123 auto& joint = this->createHingeJoint(actors[it], actors[body]);
124 joint.setAnchorPoint(actors[it]->center);
125 joint.setMoter(wheel_velocity);
126 joint.setAxis(Quat1f(instances[i].rotation()).rotate(Vec3f(1, 0, 0)));
127 }
128
129 auto& jointBackWheel_Body = this->createFixedJoint(actors[backWheel], actors[body]);
130 jointBackWheel_Body.setAnchorPoint(actors[backWheel]->center); //set and offset
131 }
132
133 //**************************************************//
135 }
136
138
139 //Tank
140 IMPLEMENT_TCLASS(Tank, TDataType)
141
142 template<typename TDataType>
144 ArticulatedBody<TDataType>()
145 {
146 auto driver = std::make_shared<CarDriver<DataType3f>>();
147 this->stateTopology()->connect(driver->inTopology());
148 this->animationPipeline()->pushModule(driver);
149
150 auto instance = std::make_shared<InstanceTransform<DataType3f>>();
151
152 this->stateCenter()->connect(instance->inCenter());
153 this->stateRotationMatrix()->connect(instance->inRotationMatrix());
154 this->stateBindingPair()->connect(instance->inBindingPair());
155 this->stateBindingTag()->connect(instance->inBindingTag());
156
157 this->animationPipeline()->pushModule(instance);
158 }
159
160 template<typename TDataType>
162 {
163
164 }
165
166 template<typename TDataType>
168 {
169 this->clearRigidBodySystem();
170 this->clearVechicle();
171
172 std::string filename = getAssetPath() + "gltf/Tank/Tank.gltf";
173 if (this->varFilePath()->getValue() != filename)
174 {
175 this->varFilePath()->setValue(FilePath(filename));
176 }
177
178 auto instances = this->varVehiclesTransform()->getValue();
179 uint vehicleNum = instances.size();
180 for (size_t i = 0; i < vehicleNum; i++)
181 {
182 RigidBodyInfo rigidbody;
183 rigidbody.bodyId = i;
184 rigidbody.friction = this->varFrictionCoefficient()->getValue();
185
186 auto texMesh = this->stateTextureMesh()->constDataPtr();
187
188 //wheel
189 std::vector <int> Wheel_Id = { 2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
190 std::map<int, CapsuleInfo> wheels;
191 std::map<int, std::shared_ptr<PdActor>> actors;
192 //Capsule
193 for (auto it : Wheel_Id)
194 {
195 auto up = texMesh->shapes()[it]->boundingBox.v1;
196 auto down = texMesh->shapes()[it]->boundingBox.v0;
197
198 wheels[it].center = Vec3f(0.0f);
199 wheels[it].rot = Quat1f(M_PI / 2, Vec3f(0, 0, 1));
200 wheels[it].halfLength = 0.1;
201 wheels[it].radius = std::abs(up.y - down.y) / 2;
202
203 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[it]->boundingTransform.translation()) + instances[i].translation();
204 rigidbody.angle = Quat1f(instances[i].rotation());
205 actors[it] = this->addCapsule(wheels[it], rigidbody, 100);
206 }
207
208
209 //body
210 int body = 0;
211 int gun = 1;
212
213 std::vector <int> box_Id = { body,gun };
214 std::map<int, BoxInfo> boxs;
215 //box
216 for (auto it : box_Id)
217 {
218 auto up = texMesh->shapes()[it]->boundingBox.v1;
219 auto down = texMesh->shapes()[it]->boundingBox.v0;
220
221 boxs[it].center = Vec3f(0.0f);
222
223 boxs[it].halfLength = (up - down) / 2 * 0.5;
224
225 rigidbody.offset = Vec3f(0.0f, 0.0f, 0.0f);
226 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[it]->boundingTransform.translation()) + instances[i].translation();
227 rigidbody.angle = Quat1f(instances[i].rotation());
228 actors[it] = this->addBox(boxs[it], rigidbody, 100);
229 }
230
231 //bindShapetoActor
232 for (auto it : box_Id)
233 {
234 this->bindShape(actors[it], Pair<uint, uint>(it, i));
235 }
236 //bindShapetoActor
237 for (auto it : Wheel_Id)
238 {
239 this->bindShape(actors[it], Pair<uint, uint>(it, i));
240 }
241
242 rigidbody.offset = Vec3f(0);
243
244 Real wheel_velocity = 10;
245
246 //wheel to Body
247 for (auto it : Wheel_Id)
248 {
249 auto& joint = this->createHingeJoint(actors[it], actors[body]);
250 joint.setAnchorPoint(actors[it]->center);
251 joint.setMoter(wheel_velocity);
252 joint.setAxis(Quat1f(instances[i].rotation()).rotate(Vec3f(1, 0, 0)));
253 }
254
255 auto& jointGun_Body = this->createFixedJoint(actors[gun], actors[body]);
256 jointGun_Body.setAnchorPoint(actors[gun]->center); //set and offset
257 }
258
259 //**************************************************//
261 }
262
264
265 //TrackedTank
266 IMPLEMENT_TCLASS(TrackedTank, TDataType)
267
268 template<typename TDataType>
270 ArticulatedBody<TDataType>()
271 {
272 auto driver = std::make_shared<CarDriver<DataType3f>>();
273 this->stateTopology()->connect(driver->inTopology());
274 this->animationPipeline()->pushModule(driver);
275
276 this->statecaterpillarTrack()->setDataPtr(std::make_shared<EdgeSet<DataType3f>>());
277 auto wireframe = std::make_shared<GLWireframeVisualModule>();
278 this->statecaterpillarTrack()->connect(wireframe->inEdgeSet());
279 this->graphicsPipeline()->pushModule(wireframe);
280
281 }
282
283 template<typename TDataType>
288
289
290 template<typename TDataType>
292 {
293 this->clearRigidBodySystem();
294 this->clearVechicle();
295
296 std::string filename = getAssetPath() + "gltf/Tank/TrackedTank.gltf";
297 if (this->varFilePath()->getValue() != filename)
298 {
299 this->varFilePath()->setValue(FilePath(filename));
300 }
301
302 auto instances = this->varVehiclesTransform()->getValue();
303 uint vehicleNum = instances.size();
304 for (size_t i = 0; i < vehicleNum; i++)
305 {
306 RigidBodyInfo rigidbody;
307 rigidbody.bodyId = i;
308 rigidbody.friction = this->varFrictionCoefficient()->getValue();
309
310 auto texMesh = this->stateTextureMesh()->constDataPtr();
311 std::map<int, std::shared_ptr<PdActor>> actors;
312
313#define FULLBODY
314
315#ifdef FULLBODY
316 //wheel
317 std::vector <int> Wheel_Id = { 1,2,3,4,6,7,8,10,11,12,13,15,16,17 };
318 //Capsule
319 for (auto it : Wheel_Id)
320 {
321 auto up = texMesh->shapes()[it]->boundingBox.v1;
322 auto down = texMesh->shapes()[it]->boundingBox.v0;
323
324 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[it]->boundingTransform.translation()) + instances[i].translation();
325 rigidbody.angle = Quat1f(instances[i].rotation());
326 rigidbody.motionType = BodyType::Dynamic;
327 auto actor = this->createRigidBody(rigidbody);
328 actors[it] = actor;
329
330 CapsuleInfo capsule;
331 capsule.rot = Quat1f(M_PI / 2, Vec3f(0, 0, 1));
332 capsule.radius = (up[1] - down[1]) / 2;
333 capsule.halfLength = (up[2] - down[2]) / 2;
334
335 float r = (up[1] - down[1]) / 2;
336 this->bindCapsule(actor, capsule, 1000);
337 this->bindShape(actor, Pair<uint, uint>(it, i));
338
339 }
340
341
342 //Gear
343 std::vector<int> gear = { 0,5,9,14 };
344 for (size_t c = 0; c < 4; c++)
345 {
346 int cid = gear[c];
347 auto up = texMesh->shapes()[cid]->boundingBox.v1;
348 auto down = texMesh->shapes()[cid]->boundingBox.v0;
349 //first gear
350 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[cid]->boundingTransform.translation()) + instances[i].translation();
351 rigidbody.angle = Quat1f(instances[i].rotation());
352
353 rigidbody.motionType = BodyType::Dynamic;
354 auto actor = this->createRigidBody(rigidbody);
355 actors[cid] = actor;
356
357 for (uint sec = 0; sec < 14; sec++)
358 {
359 CapsuleInfo capsule;
360 capsule.radius = 0.015;
361 capsule.halfLength = (up[1] - down[1]) / 2;
362 Vec3f offset = sec <= 6 ? Vec3f(-0.1, 0, 0) : Vec3f(0.1, 0, 0);
363 float theta = sec * M_PI / 7;
364
365 capsule.rot = Quat1f(sec * M_PI / 7 + M_PI / 14, Vec3f(1, 0, 0));
366 capsule.center = offset;
367 this->bindCapsule(actor, capsule, 100000);
368
369 //row
370 float r = (up[1] - down[1]) / 3;
371 float theta2 = sec * M_PI / 7;
372 float y = r * sin(theta2);
373 float z = r * cos(theta2);
374
375 capsule.rot = Quat1f(M_PI / 2, Vec3f(0, 0, 1));
376 capsule.radius = 0.05f;
377 capsule.halfLength = (up[0] - down[0]) / 2;
378 capsule.center = Vec3f(0, y, z);
379 this->bindCapsule(actor, capsule, 100000);
380 }
381
382 this->bindShape(actor, Pair<uint, uint>(cid, i));
383
384
385 }
386
387 //Gun
388 int head = 18;
389 {
390 auto up = texMesh->shapes()[head]->boundingBox.v1;
391 auto down = texMesh->shapes()[head]->boundingBox.v0;
392
393 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[head]->boundingTransform.translation()) + instances[i].translation();
394 rigidbody.angle = Quat1f(instances[i].rotation());
395
396 rigidbody.motionType = BodyType::Dynamic;
397 auto actor = this->createRigidBody(rigidbody);
398 actors[head] = actor;
399
400 BoxInfo box;
401 box.halfLength = Vec3f(0.8, 0.4, 1.2);
402 box.center = Vec3f(0, -0.2, -1);
403
404 this->bindBox(actor, box);
405
406 CapsuleInfo capsule;
407 capsule.rot = Quat1f(M_PI / 2, Vec3f(1, 0, 0));
408 capsule.radius = 0.1f;
409 capsule.halfLength = (up[2] - down[2]) / 3;
410 capsule.center = Vec3f(0.3, -0.2, 0.5);
411 this->bindCapsule(actor, capsule);
412 capsule.center = Vec3f(-0.3, -0.2, 0.5);
413 this->bindCapsule(actor, capsule);
414
415 this->bindShape(actor, Pair<uint, uint>(head, i));
416 }
417 //Body
418 int body = 19;
419 {
420 auto up = texMesh->shapes()[body]->boundingBox.v1;
421 auto down = texMesh->shapes()[body]->boundingBox.v0;
422
423 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[body]->boundingTransform.translation()) + instances[i].translation();
424 rigidbody.angle = Quat1f(instances[i].rotation());
425
426 rigidbody.motionType = BodyType::Dynamic;
427 auto actor = this->createRigidBody(rigidbody);
428 actors[body] = actor;
429
430 BoxInfo box;
431 box.rot = Quat1f(0, Vec3f(0, 0, 1));
432 box.halfLength = (up - down) / Vec3f(3, 2, 3);
433
434 this->bindBox(actor, box);
435 this->bindShape(actor, Pair<uint, uint>(body, i));
436 }
437#endif // FULLBODY
438
439 std::vector<int> caterpillarTrack_L;
440 std::vector<int> caterpillarTrack_R;
441
442 for (int cid = 20; cid < 160; cid++)
443 {
444 auto up = texMesh->shapes()[cid]->boundingBox.v1;
445 auto down = texMesh->shapes()[cid]->boundingBox.v0;
446
447 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[cid]->boundingTransform.translation()) + instances[i].translation();
448 rigidbody.angle = Quat1f(instances[i].rotation());
449
450 rigidbody.motionType = BodyType::Dynamic;
451 rigidbody.bodyId = i * 2 + 1;
452 auto actor = this->createRigidBody(rigidbody);
453 actors[cid] = actor;
454 if (cid < 90)
455 caterpillarTrack_L.push_back(cid);
456 else
457 caterpillarTrack_R.push_back(cid);
458
459
460 SphereInfo capsule;
461 capsule.rot = Quat1f(M_PI / 2, Vec3f(0, 0, 1));
462 capsule.radius = 0.05;
463 if(cid < 90)
464 capsule.center = Vec3f((up[0] - down[0]) / 2 - 0.05, 0, 0);
465 else
466 capsule.center = Vec3f((up[0] - down[0]) / 2 + 0.05, 0, 0);
467 //capsule.halfLength = (up[0] - down[0]) / 2;
468
469 this->bindSphere(actor, capsule, 1000000);
470 capsule.center = Vec3f(-(up[0] - down[0]) / 2, 0, 0);
471 this->bindSphere(actor, capsule, 1000000);
472
473 this->bindShape(actor, Pair<uint, uint>(cid, i));
474 }
475
476#ifdef FULLBODY
477 Real wheel_velocity = 10;
478
479 //weel to Body Joint
480 for (auto it : Wheel_Id)
481 {
482 auto& joint = this->createHingeJoint(actors[it], actors[body]);
483 joint.setAnchorPoint(actors[it]->center);
484 //joint.setMoter(wheel_velocity);
485 joint.setAxis(Quat1f(instances[i].rotation()).rotate(Vec3f(1, 0, 0)));
486 }
487
488 //Gear to Body Joint
489 for (auto it : gear)
490 {
491 auto& joint = this->createHingeJoint(actors[it], actors[body]);
492 joint.setAnchorPoint(actors[it]->center);
493 joint.setMoter(wheel_velocity);
494 joint.setAxis(Quat1f(instances[i].rotation()).rotate(Vec3f(1, 0, 0)));
495 }
496
497 auto& jointGun_Body = this->createFixedJoint(actors[head], actors[body]);
498 jointGun_Body.setAnchorPoint(actors[body]->center);
499
500#endif // FULLBODY
501
502 //Caterpillar Track Joint
503 auto edgeset = this->statecaterpillarTrack()->getDataPtr();
504 std::vector<TopologyModule::Edge> edges;
505 std::vector<Vec3f> points;
506 for (int k = 0; k < caterpillarTrack_L.size(); k++)
507 {
508 auto start = caterpillarTrack_L[k];
509 auto end = k != caterpillarTrack_L.size() - 1 ? caterpillarTrack_L[k + 1] : caterpillarTrack_L[0];
510
511 auto& joint = this->createHingeJoint(actors[start], actors[end]);
512 joint.setAnchorPoint((actors[start]->center + actors[end]->center) / 2);
513 joint.setAxis(Quat1f(instances[i].rotation()).rotate(Vec3f(1, 0, 0)));
514
515 points.push_back(actors[start]->center);
516
517 if (k != caterpillarTrack_L.size() - 1)
518 edges.push_back(TopologyModule::Edge(k, k + 1));
519 else
520 edges.push_back(TopologyModule::Edge(k, 0));
521
522
523 }
524 auto edgeOffset = points.size();
525 for (int k = 0; k < caterpillarTrack_R.size(); k++)
526 {
527 auto start = caterpillarTrack_R[k];
528 auto end = k != caterpillarTrack_R.size() - 1 ? caterpillarTrack_R[k + 1] : caterpillarTrack_R[0];
529
530 auto& joint = this->createHingeJoint(actors[start], actors[end]);
531 joint.setAnchorPoint((actors[start]->center + actors[end]->center) / 2);
532 joint.setAxis(Quat1f(instances[i].rotation()).rotate(Vec3f(1, 0, 0)));
533
534 points.push_back(actors[start]->center);
535
536 if (k != caterpillarTrack_R.size() - 1)
537 edges.push_back(TopologyModule::Edge(k + edgeOffset, k + 1 + edgeOffset));
538 else
539 edges.push_back(TopologyModule::Edge(k + edgeOffset, 0 + edgeOffset));
540
541 }
542 edgeset->setPoints(points);
543 edgeset->setEdges(edges);
544 edgeset->update();
545
546
547 }
548
549 //**************************************************//
551
552 }
553
555
556 //UAV
557 IMPLEMENT_TCLASS(UAV, TDataType)
558
559 template<typename TDataType>
561 ArticulatedBody<TDataType>()
562 {
563 auto driver = std::make_shared<CarDriver<DataType3f>>();
564 this->stateTopology()->connect(driver->inTopology());
565 this->animationPipeline()->pushModule(driver);
566 }
567
568 template<typename TDataType>
570 {
571
572 }
573
574 template<typename TDataType>
576 {
577 this->clearRigidBodySystem();
578 this->clearVechicle();
579
580 std::string filename = getAssetPath() + "gltf/UAV/UAV.gltf";
581 if (this->varFilePath()->getValue() != filename)
582 {
583 this->varFilePath()->setValue(FilePath(filename));
584 }
585
586 auto instances = this->varVehiclesTransform()->getValue();
587 uint vehicleNum = instances.size();
588 for (size_t i = 0; i < vehicleNum; i++)
589 {
590 RigidBodyInfo rigidbody;
591 rigidbody.bodyId = i;
592 rigidbody.friction = this->varFrictionCoefficient()->getValue();
593
594 auto texMesh = this->stateTextureMesh()->constDataPtr();
595
596 //wheel
597 std::vector <int> Wheel_Id = { 0,1,2,3 };
598 std::map<int, CapsuleInfo> wheels;
599 std::map<int, std::shared_ptr<PdActor>> actors;
600 //Capsule
601 for (auto it : Wheel_Id)
602 {
603 auto up = texMesh->shapes()[it]->boundingBox.v1;
604 auto down = texMesh->shapes()[it]->boundingBox.v0;
605
606 wheels[it].center = Vec3f(0.0f);
607
608 auto rot = it == 0 || it == 3 ? Vec3f(90, 45, 0) : Vec3f(90, -45, 0);
609
610 Quat<Real> q =
611 Quat<Real>(Real(M_PI) * rot[2] / 180, Coord(0, 0, 1))
612 * Quat<Real>(Real(M_PI) * rot[1] / 180, Coord(0, 1, 0))
613 * Quat<Real>(Real(M_PI) * rot[0] / 180, Coord(1, 0, 0));
614 q.normalize();
615
616
617 wheels[it].rot = q;
618 wheels[it].halfLength = std::abs(up.x - down.x) / 1.5;
619 wheels[it].radius = std::abs(up.y - down.y) / 2;
620
621 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[it]->boundingTransform.translation()) + instances[i].translation();
622 rigidbody.angle = Quat1f(instances[i].rotation());
623
624 actors[it] = this->addCapsule(wheels[it], rigidbody, 100);
625 }
626
627
628 //body
629 int body = 4;
630
631 std::vector <int> box_Id = { body };
632 std::map<int, BoxInfo> boxs;
633 //box
634 for (auto it : box_Id)
635 {
636 auto up = texMesh->shapes()[it]->boundingBox.v1;
637 auto down = texMesh->shapes()[it]->boundingBox.v0;
638
639 boxs[it].center = Vec3f(0.0f);
640
641 boxs[it].halfLength = (up - down) / 2;
642
643 rigidbody.offset = Vec3f(0.0f, 0.0f, 0.0f);
644 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[it]->boundingTransform.translation()) + instances[i].translation();
645 rigidbody.angle = Quat1f(instances[i].rotation());
646
647 actors[it] = this->addBox(boxs[it], rigidbody, 100);
648 }
649
650 //bindShapetoActor
651 for (auto it : box_Id)
652 {
653 this->bindShape(actors[it], Pair<uint, uint>(it, i));
654 }
655 //bindShapetoActor
656 for (auto it : Wheel_Id)
657 {
658 this->bindShape(actors[it], Pair<uint, uint>(it, i));
659 }
660
661 rigidbody.offset = Vec3f(0);
662
663
664
665 //wheel to Body
666 for (auto it : Wheel_Id)
667 {
668 Real wheel_velocity = (it == 0 || it == 3) ? Real(20.0) : Real(-20.0);
669
670 auto& joint = this->createHingeJoint(actors[it], actors[body]);
671 joint.setAnchorPoint(actors[it]->center);
672 joint.setMoter(wheel_velocity);
673 joint.setAxis(Quat1f(instances[i].rotation()).rotate(Vec3f(0, 1, 0)));
674 }
675
676 }
677
678 //**************************************************//
680 }
681
683
684
685 //UUV
686 IMPLEMENT_TCLASS(UUV, TDataType)
687
688 template<typename TDataType>
690 ArticulatedBody<TDataType>()
691 {
692 auto driver = std::make_shared<CarDriver<DataType3f>>();
693 this->stateTopology()->connect(driver->inTopology());
694 this->animationPipeline()->pushModule(driver);
695 }
696
697 template<typename TDataType>
699 {
700
701 }
702
703 template<typename TDataType>
705 {
706 this->clearRigidBodySystem();
707 this->clearVechicle();
708
709 std::string filename = getAssetPath() + "gltf/UUV/UUV.gltf";
710 if (this->varFilePath()->getValue() != filename)
711 {
712 this->varFilePath()->setValue(FilePath(filename));
713 }
714
715 auto instances = this->varVehiclesTransform()->getValue();
716 uint vehicleNum = instances.size();
717 for (size_t i = 0; i < vehicleNum; i++)
718 {
719 RigidBodyInfo rigidbody;
720 rigidbody.bodyId = i;
721 rigidbody.friction = this->varFrictionCoefficient()->getValue();
722
723 auto texMesh = this->stateTextureMesh()->constDataPtr();
724
725 //wheel
726 std::vector <int> Wheel_Id = { 1,2,3,4 };
727 std::map<int, CapsuleInfo> wheels;
728 std::map<int, std::shared_ptr<PdActor>> actors;
729 //Capsule
730 for (auto it : Wheel_Id)
731 {
732 auto up = texMesh->shapes()[it]->boundingBox.v1;
733 auto down = texMesh->shapes()[it]->boundingBox.v0;
734
735 wheels[it].center = Vec3f(0.0f);
736
737 auto rot = it == 1 || it == 2 ? Vec3f(90, 30, 0) : Vec3f(0, 0, 30);
738
739 Quat<Real> q =
740 Quat<Real>(Real(M_PI) * rot[2] / 180, Coord(0, 0, 1))
741 * Quat<Real>(Real(M_PI) * rot[1] / 180, Coord(0, 1, 0))
742 * Quat<Real>(Real(M_PI) * rot[0] / 180, Coord(1, 0, 0));
743 q.normalize();
744
745
746 wheels[it].rot = q;
747 wheels[it].halfLength = 0.13;
748 wheels[it].radius = 0.03;
749
750 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[it]->boundingTransform.translation()) + instances[i].translation();
751 rigidbody.angle = Quat1f(instances[i].rotation());
752
753 actors[it] = this->addCapsule(wheels[it], rigidbody, 100);
754 }
755
756
757 //body
758 int body = 0;
759
760 std::vector <int> box_Id = { body };
761 std::map<int, BoxInfo> boxs;
762 //box
763 for (auto it : box_Id)
764 {
765 auto up = texMesh->shapes()[it]->boundingBox.v1;
766 auto down = texMesh->shapes()[it]->boundingBox.v0;
767
768 boxs[it].center = Vec3f(0.0f);
769
770 boxs[it].halfLength = (up - down) / 2;
771
772 rigidbody.offset = Vec3f(0.0f, 0.0f, 0.0f);
773 rigidbody.position = Quat1f(instances[i].rotation()).rotate(texMesh->shapes()[it]->boundingTransform.translation()) + instances[i].translation();
774 rigidbody.angle = Quat1f(instances[i].rotation());
775
776 actors[it] = this->addBox(boxs[it], rigidbody, 100);
777 }
778
779 //bindShapetoActor
780 for (auto it : box_Id)
781 {
782 this->bindShape(actors[it], Pair<uint, uint>(it, i));
783 }
784 //bindShapetoActor
785 for (auto it : Wheel_Id)
786 {
787 this->bindShape(actors[it], Pair<uint, uint>(it, i));
788 }
789
790 rigidbody.offset = Vec3f(0);
791
792
793
794 //wheel to Body
795 for (auto it : Wheel_Id)
796 {
797 Real wheel_velocity = 20;
798
799 auto& joint = this->createHingeJoint(actors[it], actors[body]);
800 joint.setAnchorPoint(actors[it]->center);
801 joint.setMoter(wheel_velocity);
802 Vec3f axis = it == 1 || it == 2 ? Vec3f(0, 1, 0) : Vec3f(0, 0, 1);
803 joint.setAxis(Quat1f(instances[i].rotation()).rotate(axis));
804 }
805
806 }
807
808 //**************************************************//
810 }
811
813
814}
#define DEFINE_CLASS(name)
Definition Object.h:140
#define IMPLEMENT_TCLASS(name, T1)
Definition Object.h:103
double Real
Definition Typedef.inl:23
#define M_PI
Definition Typedef.inl:36
void resetStates() override
void bindShape(std::shared_ptr< PdActor > actor, Pair< uint, uint > shapeId)
TDataType::Real Real
Definition Vehicle.h:33
void resetStates() override
Definition Vehicle.cpp:40
~Jeep() override
Definition Vehicle.cpp:34
std::shared_ptr< GraphicsPipeline > graphicsPipeline()
Definition Node.cpp:311
std::shared_ptr< AnimationPipeline > animationPipeline()
Definition Node.cpp:302
DYN_FUNC Quat< Real > & normalize()
Definition Quat.inl:183
DYN_FUNC Vector< Real, 3 > rotate(const Vector< Real, 3 > &v) const
Rotate a vector by the quaternion, guarantee the quaternion is normalized before rotating the vector.
Definition Quat.inl:259
std::shared_ptr< PdActor > addBox(const BoxInfo &box, const RigidBodyInfo &bodyDef, const Real density=Real(100))
HingeJoint & createHingeJoint(std::shared_ptr< PdActor > actor1, std::shared_ptr< PdActor > actor2)
std::shared_ptr< PdActor > createRigidBody(const Coord &p, const TQuat &q)
FixedJoint & createFixedJoint(std::shared_ptr< PdActor > actor1, std::shared_ptr< PdActor > actor2)
void bindBox(const std::shared_ptr< PdActor > actor, const BoxInfo &box, const Real density=Real(100))
std::shared_ptr< PdActor > addCapsule(const CapsuleInfo &capsule, const RigidBodyInfo &bodyDef, const Real density=Real(100))
void bindCapsule(const std::shared_ptr< PdActor > actor, const CapsuleInfo &capsule, const Real density=Real(100))
void bindSphere(const std::shared_ptr< PdActor > actor, const SphereInfo &sphere, const Real density=Real(100))
~Tank() override
Definition Vehicle.cpp:161
void resetStates() override
Definition Vehicle.cpp:167
TDataType::Real Real
Definition Vehicle.h:48
VectorND< PointType, 2 > Edge
void resetStates() override
Definition Vehicle.cpp:291
~TrackedTank() override
Definition Vehicle.cpp:284
TDataType::Real Real
Definition Vehicle.h:65
~UAV() override
Definition Vehicle.cpp:569
TDataType::Real Real
Definition Vehicle.h:86
TDataType::Coord Coord
Definition Vehicle.h:87
void resetStates() override
Definition Vehicle.cpp:575
TDataType::Real Real
Definition Vehicle.h:103
void resetStates() override
Definition Vehicle.cpp:704
~UUV() override
Definition Vehicle.cpp:698
TDataType::Coord Coord
Definition Vehicle.h:104
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
int joint
Definition GltfFunc.h:19
DYN_FUNC Complex< Real > sin(const Complex< Real > &)
Definition Complex.inl:564
Quat< float > Quat1f
Definition Quat.h:136
DYN_FUNC Complex< Real > cos(const Complex< Real > &)
Definition Complex.inl:573
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
unsigned int uint
Definition VkProgram.h:14
Quat< Real > rot
Vector< Real, 3 > halfLength
Vector< Real, 3 > center
Vector< Real, 3 > center
Vector< Real, 3 > offset
An offset from the barycenter to the geometric center.
Vector< Real, 3 > position
The barycenter of the body.
Quat< Real > rot
Vector< Real, 3 > center