Collision example (demo_IRR_collisionNSC.cpp)

An introductory tutorial about collisions using Non-Smooth Contacts (NSC).

A bunch of random shapes will fall into a box, stacking in random order, while a mixer blade rotates.

Learn about:

  • how to enable collisions
  • how to change the settings of the stepper in sake of higher precision or higher computing speed.
  • create a motor between two parts.
// =============================================================================
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2014 projectchrono.org
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file at the top level of the distribution and at
// http://projectchrono.org/license-chrono.txt.
//
// =============================================================================
// Authors: Alessandro Tasora
// =============================================================================
//
// Demo code about
// - collisions and contacts
// - using Irrlicht to display objects.
//
// =============================================================================
#include "chrono/physics/ChSystemNSC.h"
#include "chrono/physics/ChBodyEasy.h"
#include "chrono/physics/ChLinkMotorRotationSpeed.h"
#include "chrono/assets/ChTexture.h"
#include "chrono/core/ChRealtimeStep.h"
#ifdef CHRONO_COLLISION
#include "chrono/collision/ChCollisionSystemChrono.h"
#endif
#include "chrono_irrlicht/ChVisualSystemIrrlicht.h"
// Use the namespaces of Chrono
using namespace chrono;
using namespace chrono::irrlicht;
void AddFallingItems(ChSystemNSC& sys) {
// Shared contact materials for falling objects
auto sph_mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
sph_mat->SetFriction(0.2f);
auto box_mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
auto cyl_mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
// Create falling rigid bodies (spheres and boxes etc.)
for (int bi = 0; bi < 29; bi++) {
auto sphereBody = chrono_types::make_shared<ChBodyEasySphere>(1.1, // radius size
1000, // density
sph_mat, // contact material
collision_type);
sphereBody->SetPos(ChVector<>(-5 + ChRandom() * 10, 4 + bi * 0.05, -5 + ChRandom() * 10));
sphereBody->GetVisualShape(0)->SetTexture(GetChronoDataFile("textures/bluewhite.png"));
sys.Add(sphereBody);
auto boxBody = chrono_types::make_shared<ChBodyEasyBox>(1.5, 1.5, 1.5, // x,y,z size
100, // density
box_mat, // contact material
collision_type);
boxBody->SetPos(ChVector<>(-5 + ChRandom() * 10, 4 + bi * 0.05, -5 + ChRandom() * 10));
boxBody->GetVisualShape(0)->SetTexture(GetChronoDataFile("textures/cubetexture_bluewhite.png"));
sys.Add(boxBody);
auto cylBody = chrono_types::make_shared<ChBodyEasyCylinder>(0.75, 0.5, // radius, height
100, // density
cyl_mat, // contact material
collision_type);
cylBody->SetPos(ChVector<>(-5 + ChRandom() * 10, 4 + bi * 0.05, -5 + ChRandom() * 10));
cylBody->GetVisualShape(0)->SetTexture(GetChronoDataFile("textures/pinkwhite.png"));
sys.Add(cylBody);
}
}
std::shared_ptr<ChBody> AddContainer(ChSystemNSC& sys) {
// Contact and visualization materials for container
auto ground_mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
auto ground_mat_vis = chrono_types::make_shared<ChVisualMaterial>(*ChVisualMaterial::Default());
ground_mat_vis->SetKdTexture(GetChronoDataFile("textures/concrete.jpg"));
// Create the five walls of the rectangular container, using fixed rigid bodies of 'box' type
auto floorBody = chrono_types::make_shared<ChBodyEasyBox>(20, 1, 20, 1000, ground_mat, collision_type);
floorBody->SetPos(ChVector<>(0, -5, 0));
floorBody->SetBodyFixed(true);
floorBody->GetVisualShape(0)->SetMaterial(0, ground_mat_vis);
sys.Add(floorBody);
auto wallBody1 = chrono_types::make_shared<ChBodyEasyBox>(1, 10, 20.99, 1000, ground_mat, collision_type);
wallBody1->SetPos(ChVector<>(-10, 0, 0));
wallBody1->SetBodyFixed(true);
wallBody1->GetVisualShape(0)->SetMaterial(0, ground_mat_vis);
sys.Add(wallBody1);
auto wallBody2 = chrono_types::make_shared<ChBodyEasyBox>(1, 10, 20.99, 1000, ground_mat, collision_type);
wallBody2->SetPos(ChVector<>(10, 0, 0));
wallBody2->SetBodyFixed(true);
wallBody2->GetVisualShape(0)->SetMaterial(0, ground_mat_vis);
sys.Add(wallBody2);
auto wallBody3 = chrono_types::make_shared<ChBodyEasyBox>(20.99, 10, 1, 1000, ground_mat, collision_type);
wallBody3->SetPos(ChVector<>(0, 0, -10));
wallBody3->SetBodyFixed(true);
wallBody3->GetVisualShape(0)->SetMaterial(0, ground_mat_vis);
sys.Add(wallBody3);
auto wallBody4 = chrono_types::make_shared<ChBodyEasyBox>(20.99, 10, 1, 1000, ground_mat, collision_type);
wallBody4->SetPos(ChVector<>(0, 0, 10));
wallBody4->SetBodyFixed(true);
wallBody4->GetVisualShape(0)->SetMaterial(0, ground_mat_vis);
sys.Add(wallBody4);
// Add the rotating mixer
auto mixer_mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
mixer_mat->SetFriction(0.4f);
auto rotatingBody = chrono_types::make_shared<ChBodyEasyBox>(10, 5, 1, // x,y,z size
4000, // density
mixer_mat, // contact material
collision_type);
rotatingBody->SetPos(ChVector<>(0, -1.6, 0));
rotatingBody->GetVisualShape(0)->SetTexture(GetChronoDataFile("textures/blue.png"));
sys.Add(rotatingBody);
// .. a motor between mixer and truss
auto my_motor = chrono_types::make_shared<ChLinkMotorRotationSpeed>();
my_motor->Initialize(rotatingBody, floorBody, ChFrame<>(ChVector<>(0, 0, 0), Q_from_AngAxis(CH_C_PI_2, VECT_X)));
auto mfun = chrono_types::make_shared<ChFunction_Const>(CH_C_PI / 4.0); // speed 45 deg/s
my_motor->SetSpeedFunction(mfun);
sys.AddLink(my_motor);
/*
// create a plain ChBody (no colliding shape nor visualization mesh is used yet)
auto rigidBody = chrono_types::make_shared<ChBody>(collision_type);
// set as fixed body, and turn collision ON, otherwise no collide by default
rigidBody->SetBodyFixed(true);
rigidBody->SetCollide(true);
// Clear model. The colliding shape description MUST be between ClearModel() .. BuildModel() pair.
rigidBody->GetCollisionModel()->ClearModel();
// Describe the (invisible) colliding shape by adding five boxes (the walls and floor)
rigidBody->GetCollisionModel()->AddBox(ground_mat, 20, 1, 20, ChVector<>(0, -10, 0));
rigidBody->GetCollisionModel()->AddBox(ground_mat, 1, 40, 20, ChVector<>(-11, 0, 0));
rigidBody->GetCollisionModel()->AddBox(ground_mat, 1, 40, 20, ChVector<>(11, 0, 0));
rigidBody->GetCollisionModel()->AddBox(ground_mat, 20, 40, 1, ChVector<>(0, 0, -11));
rigidBody->GetCollisionModel()->AddBox(ground_mat, 20, 40, 1, ChVector<>(0, 0, 11));
// Complete the description of collision shape.
rigidBody->GetCollisionModel()->BuildModel();
*/
return rotatingBody;
}
int main(int argc, char* argv[]) {
GetLog() << "Copyright (c) 2017 projectchrono.org\nChrono version: " << CHRONO_VERSION << "\n\n";
// Create the physical system
sys.SetCollisionSystemType(collision_type);
// Settings specific to Chrono multicore collision system
#ifdef CHRONO_COLLISION
auto collsys = std::static_pointer_cast<collision::ChCollisionSystemChrono>(sys.GetCollisionSystem());
// Change the default number of broadphase bins
collsys->SetBroadphaseGridResolution(ChVector<int>(10, 10, 2));
// Change default narrowphase algorithm
collsys->SetEnvelope(0.005);
// Enable active bounding box
collsys->EnableActiveBoundingBox(ChVector<>(-10, -10, -20), ChVector<>(+10, +10, +10));
// Set number of threads used by the collision detection system
collsys->SetNumThreads(4);
#endif
}
// Add fixed and moving bodies
auto mixer = AddContainer(sys);
AddFallingItems(sys);
// Create the Irrlicht visualization system
auto vis = chrono_types::make_shared<ChVisualSystemIrrlicht>();
vis->AttachSystem(&sys);
vis->SetWindowSize(800, 600);
vis->SetWindowTitle("NSC collision demo");
vis->Initialize();
vis->AddLogo();
vis->AddSkyBox();
vis->AddCamera(ChVector<>(0, 14, -20));
// Modify some setting of the physical system for the simulation, if you want
// Simulation loop
double step_size = 0.003;
while (vis->Run()) {
vis->BeginScene();
vis->Render();
vis->EndScene();
sys.DoStepDynamics(step_size);
rt.Spin(step_size);
}
return 0;
}
void AddTypicalLights()
Simple shortcut to set two point lights in the scene.
Definition: ChVisualSystemIrrlicht.cpp:274
std::string GetChronoDataFile(const std::string &filename)
Obtain the complete path to the specified filename, given relative to the Chrono data directory (thre...
Definition: ChGlobal.cpp:95
void Add(std::shared_ptr< ChPhysicsItem > item)
Attach an arbitrary ChPhysicsItem (e.g.
Definition: ChSystem.cpp:183
void AddSkyBox(const std::string &texture_dir=GetChronoDataFile("skybox/"))
Add a sky box in a 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:287
virtual void Initialize()
Initialize the visualization system.
Definition: ChVisualSystemIrrlicht.cpp:151
ChLog & GetLog()
Global function to get the current ChLog object.
Definition: ChLog.cpp:39
std::shared_ptr< collision::ChCollisionSystem > GetCollisionSystem() const
Access the underlying collision system.
Definition: ChSystem.h:711
virtual void SetCollisionSystemType(collision::ChCollisionSystemType type)
Change the underlying collision detection system to the specified type.
Definition: ChSystem.cpp:344
bool Run()
Run the Irrlicht device.
Definition: ChVisualSystemIrrlicht.cpp:205
virtual void AddLink(std::shared_ptr< ChLinkBase > link)
Attach a link to the underlying assembly.
Definition: ChSystem.cpp:168
void AddCamera(const ChVector<> &pos, ChVector<> targ=VNULL)
Add a camera in an Irrlicht 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:256
Representation of a 3D transform.
Definition: ChFrame.h:34
ChQuaternion< double > Q_from_AngAxis(double angle, const ChVector< double > &axis)
Get the quaternion from an angle of rotation and an axis, defined in abs coords.
Definition: ChQuaternion.cpp:100
virtual void BeginScene(bool backBuffer=true, bool zBuffer=true, ChColor color=ChColor(0, 0, 0))
Clean the canvas at the beginning of each animation frame.
Definition: ChVisualSystemIrrlicht.cpp:489
Namespace with classes for the Irrlicht module.
Definition: ChApiIrr.h:48
Class for a timer which attempts to enforce soft real-time.
Definition: ChRealtimeStep.h:25
Projected SOR (Successive Over-Relaxation)
Chrono multicore collision detection system.
void Spin(double step)
Call this function INSIDE the simulation loop, just ONCE per loop (preferably as the last call in the...
Definition: ChRealtimeStep.h:34
Definition of general purpose 3d vector variables, such as points in 3D.
Definition: ChVector.h:35
Bullet-based collision detection system.
void SetSolverType(ChSolver::Type type)
Choose the solver type, to be used for the simultaneous solution of the constraints in dynamical simu...
Definition: ChSystem.cpp:259
int DoStepDynamics(double step_size)
Advances the dynamical simulation for a single step, of length step_size.
Definition: ChSystem.cpp:1426
virtual void EndScene()
End the scene draw at the end of each animation frame.
Definition: ChVisualSystemIrrlicht.cpp:534
void SetSolverMaxIterations(int max_iters)
Set the maximum number of iterations, if using an iterative solver.
Definition: ChSystem.cpp:233
void SetWindowTitle(const std::string &win_title)
Set the windoiw title (default "").
Definition: ChVisualSystemIrrlicht.cpp:118
double ChRandom()
Returns random value in (0..1) interval with Park-Miller method.
Definition: ChMathematics.cpp:53
virtual void AttachSystem(ChSystem *sys) override
Attach another Chrono system to the run-time visualization system.
Definition: ChVisualSystemIrrlicht.cpp:135
ChCollisionSystemType
Collision engine type.
Definition: ChCollisionModel.h:44
virtual void Render()
Draw all 3D shapes and GUI elements at the current frame.
Definition: ChVisualSystemIrrlicht.cpp:544
Main namespace for the Chrono package.
Definition: ChBarrelShape.cpp:17
void AddLogo(const std::string &logo_filename=GetChronoDataFile("logo_chronoengine_alpha.png"))
Add a logo in a 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:248
Class for a physical system in which contact is modeled using a non-smooth (complementarity-based) me...
Definition: ChSystemNSC.h:29
void SetWindowSize(unsigned int width, unsigned int height)
Set the window size (default 640x480).
Definition: ChVisualSystemIrrlicht.cpp:115