Guest User

Untitled

a guest
Dec 14th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1. // =============================================================================
  2. // PROJECT CHRONO - http://projectchrono.org
  3. //
  4. // Copyright (c) 2018 projectchrono.org
  5. // All rights reserved.
  6. //
  7. // Use of this source code is governed by a BSD-style license that can be found
  8. // in the LICENSE file at the top level of the distribution and at
  9. // http://projectchrono.org/license-chrono.tx
  10. // =============================================================================
  11. // Authors: Kishor Bhalerao, Radu Serban
  12. // =============================================================================
  13. //
  14. // Demonstrate the use of the debug collision visualization callback with the
  15. // Bullet or Chrono collision system.
  16. // =============================================================================
  17.  
  18. #include "chrono/collision/bullet/ChCollisionSystemBullet.h"
  19. #include "chrono/physics/ChSystemNSC.h"
  20. #include "chrono/physics/ChSystemSMC.h"
  21. #include "chrono/physics/ChLoadContainer.h"
  22. #include "chrono/physics/ChBodyEasy.h"
  23. #include "chrono/core/ChTimer.h"
  24. #include "chrono/core/ChRealtimeStep.h"
  25.  
  26. #include "chrono_irrlicht/ChVisualSystemIrrlicht.h"
  27.  
  28. using namespace chrono;
  29. using namespace chrono::irrlicht;
  30.  
  31. auto csys_type = ChCollisionSystem::Type::BULLET;
  32. ////auto csys_type = ChCollisionSystem::Type::MULTICORE;
  33.  
  34. class DebugDrawer : public ChCollisionSystem::VisualizationCallback {
  35.   public:
  36.     explicit DebugDrawer(ChVisualSystemIrrlicht* vis) : m_vis(vis) {}
  37.     ~DebugDrawer() {}
  38.  
  39.     virtual void DrawLine(const ChVector3d& from, const ChVector3d& to, const ChColor& color) override {
  40.         m_vis->GetVideoDriver()->draw3DLine(irr::core::vector3dfCH(from), irr::core::vector3dfCH(to),
  41.                                             irr::video::SColor(255, color.R * 255, color.G * 255, color.B * 255));
  42.     }
  43.  
  44.     virtual double GetNormalScale() const override { return 1.0; }
  45.  
  46.     void Draw(int flags, bool use_zbuffer = true) {
  47.         m_vis->GetVideoDriver()->setTransform(irr::video::ETS_WORLD, irr::core::matrix4());
  48.         irr::video::SMaterial mattransp;
  49.         mattransp.ZBuffer = use_zbuffer;
  50.         mattransp.Lighting = false;
  51.         m_vis->GetVideoDriver()->setMaterial(mattransp);
  52.  
  53.         m_vis->GetSystem(0).GetCollisionSystem()->Visualize(flags);
  54.     }
  55.  
  56.   private:
  57.     ChVisualSystemIrrlicht* m_vis;
  58. };
  59.  
  60. int main(int argc, char* argv[]) {
  61.     std::cout << "Copyright (c) 2017 projectchrono.org\nChrono version: " << CHRONO_VERSION << std::endl;
  62.  
  63.     // Create the Chrono system, bodies, and collison shapes
  64.     ChSystemSMC sys;
  65.     //ChSystemNSC sys;
  66.     sys.SetCollisionSystemType(csys_type);
  67.     sys.SetGravitationalAcceleration(ChVector3d(0, 0, 0));
  68.  
  69.     auto mat = chrono_types::make_shared<ChContactMaterialSMC>();
  70.     //auto mat = chrono_types::make_shared<ChContactMaterialNSC>();
  71.  
  72.     auto ground = chrono_types::make_shared<ChBodyEasyBox>(10, 3, 10, 100, mat);
  73.     ground->SetFixed(true);
  74.     ground->SetPos(ChVector3d(0.0, 0.0, 0.0));
  75.     ground->GetVisualShape(0)->SetColor(ChColor(0.2f, 0.3f, 0.6f));
  76.     sys.AddBody(ground);
  77.  
  78.     auto cyl = chrono_types::make_shared<ChBodyEasyCylinder>(ChAxis::Y, 0.5, 1.0, 100, mat);
  79.     cyl->SetPos(ChVector3d(0.0, 3.0, 0.0));
  80.     cyl->GetVisualShape(0)->SetColor(ChColor(0.2f, 0.3f, 0.6f));
  81.     sys.AddBody(cyl);
  82.  
  83.     auto box = chrono_types::make_shared<ChBodyEasyBox>(0.5, 0.5, 0.5, 100, mat);
  84.     box->SetPos(ChVector3d(0.2, 2.0, 0.0));
  85.     box->GetVisualShape(0)->SetColor(ChColor(0.2f, 0.3f, 0.6f));
  86.     sys.AddBody(box);
  87.  
  88.     auto sphere = chrono_types::make_shared<ChBodyEasySphere>(0.25, 100.0, mat);
  89.     sphere->SetPos(ChVector3d(-0.2, 2.0, 0.75));
  90.     sphere->GetVisualShape(0)->SetColor(ChColor(0.2f, 0.3f, 0.6f));
  91.     sys.AddBody(sphere);
  92.  
  93.     auto ellipse = chrono_types::make_shared<ChBodyEasyEllipsoid>(ChVector3d(0.4, 0.8, 1.2), 100.0, mat);
  94.     ellipse->SetPos(ChVector3d(0.2, 2.0, -1.0));
  95.     ellipse->GetVisualShape(0)->SetColor(ChColor(0.2f, 0.3f, 0.6f));
  96.     sys.AddBody(ellipse);
  97.  
  98.     auto mesh = chrono_types::make_shared<ChBodyEasyMesh>(GetChronoDataFile("models/cube.obj"), 100.0, mat, 0.05);
  99.     mesh->SetPos(ChVector3d(2.0, 3.5, -2.0));
  100.     mesh->GetVisualShape(0)->SetColor(ChColor(0.2f, 0.3f, 0.6f));
  101.     sys.AddBody(mesh);
  102.  
  103.     auto load_container = chrono_types::make_shared<ChLoadContainer>();
  104.     for (auto obj : sys.GetBodies()) {
  105.         auto custom_loader = chrono_types::make_shared<ChLoaderGravity>(obj);
  106.         auto custom_load = chrono_types::make_shared<ChLoad>(custom_loader);
  107.         load_container->Add(custom_load);
  108.     }
  109.     sys.Add(load_container);
  110.  
  111.     // Create the Irrlicht visualization system
  112.     auto vis = chrono_types::make_shared<ChVisualSystemIrrlicht>();
  113.     vis->AttachSystem(&sys);
  114.     vis->SetWindowSize(800, 600);
  115.     vis->SetWindowTitle("Collision visualization demo");
  116.     vis->Initialize();
  117.     vis->AddLogo();
  118.     vis->AddSkyBox();
  119.     vis->AddCamera(ChVector3d(0, 8, 6));
  120.     vis->AddTypicalLights();
  121.  
  122.     // Set the debug drawer for collision visualization
  123.     auto drawer = chrono_types::make_shared<DebugDrawer>(vis.get());
  124.     sys.GetCollisionSystem()->RegisterVisualizationCallback(drawer);
  125.  
  126.     // Specify what information is visualized
  127.     int mode = ChCollisionSystem::VIS_Shapes | ChCollisionSystem::VIS_Aabb;
  128.     //////int mode = ChCollisionSystem::VIS_Shapes;
  129.     ////int mode = ChCollisionSystem::VIS_Contacts;
  130.  
  131.     bool use_zbuffer = true;
  132.  
  133.     // Simulation loop
  134.     double timestep = 0.005;
  135.     ChRealtimeStepTimer realtime_timer;
  136.     while (vis->Run()) {
  137.         vis->BeginScene();
  138.         vis->Render();
  139.         drawer->Draw(mode, use_zbuffer);
  140.         vis->EndScene();
  141.         sys.DoStepDynamics(timestep);
  142.         realtime_timer.Spin(timestep);
  143.     }
  144.  
  145.     return 0;
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment