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.
// =============================================================================
// Copyright (c) 2014
// 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
// =============================================================================
// 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"
#include "chrono/collision/ChCollisionSystemChrono.h"
#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>();
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
sphereBody->SetPos(ChVector<>(-5 + ChRandom() * 10, 4 + bi * 0.05, -5 + ChRandom() * 10));
auto boxBody = chrono_types::make_shared<ChBodyEasyBox>(1.5, 1.5, 1.5, // x,y,z size
100, // density
box_mat, // contact material
boxBody->SetPos(ChVector<>(-5 + ChRandom() * 10, 4 + bi * 0.05, -5 + ChRandom() * 10));
auto cylBody = chrono_types::make_shared<ChBodyEasyCylinder>(0.75, 0.5, // radius, height
100, // density
cyl_mat, // contact material
cylBody->SetPos(ChVector<>(-5 + ChRandom() * 10, 4 + bi * 0.05, -5 + ChRandom() * 10));
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());
// 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->GetVisualShape(0)->SetMaterial(0, ground_mat_vis);
auto wallBody1 = chrono_types::make_shared<ChBodyEasyBox>(1, 10, 20.99, 1000, ground_mat, collision_type);
wallBody1->SetPos(ChVector<>(-10, 0, 0));
wallBody1->GetVisualShape(0)->SetMaterial(0, ground_mat_vis);
auto wallBody2 = chrono_types::make_shared<ChBodyEasyBox>(1, 10, 20.99, 1000, ground_mat, collision_type);
wallBody2->SetPos(ChVector<>(10, 0, 0));
wallBody2->GetVisualShape(0)->SetMaterial(0, ground_mat_vis);
auto wallBody3 = chrono_types::make_shared<ChBodyEasyBox>(20.99, 10, 1, 1000, ground_mat, collision_type);
wallBody3->SetPos(ChVector<>(0, 0, -10));
wallBody3->GetVisualShape(0)->SetMaterial(0, ground_mat_vis);
auto wallBody4 = chrono_types::make_shared<ChBodyEasyBox>(20.99, 10, 1, 1000, ground_mat, collision_type);
wallBody4->SetPos(ChVector<>(0, 0, 10));
wallBody4->GetVisualShape(0)->SetMaterial(0, ground_mat_vis);
// Add the rotating mixer
auto mixer_mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
auto rotatingBody = chrono_types::make_shared<ChBodyEasyBox>(10, 5, 1, // x,y,z size
4000, // density
mixer_mat, // contact material
rotatingBody->SetPos(ChVector<>(0, -1.6, 0));
// .. 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
// 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
// Clear model. The colliding shape description MUST be between ClearModel() .. BuildModel() pair.
// 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.
return rotatingBody;
int main(int argc, char* argv[]) {
GetLog() << "Copyright (c) 2017\nChrono version: " << CHRONO_VERSION << "\n\n";
// Create the physical system
// Settings specific to Chrono multicore collision system
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
// Enable active bounding box
collsys->EnableActiveBoundingBox(ChVector<>(-10, -10, -20), ChVector<>(+10, +10, +10));
// Set number of threads used by the collision detection system
// Add fixed and moving bodies
auto mixer = AddContainer(sys);
// Create the Irrlicht visualization system
auto vis = chrono_types::make_shared<ChVisualSystemIrrlicht>();
vis->SetWindowSize(800, 600);
vis->SetWindowTitle("NSC collision demo");
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()) {
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
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