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/ChParticlesClones.h"
#include "chrono/physics/ChLinkMotorRotationSpeed.h"
#include "chrono/assets/ChTexture.h"
#include "chrono_irrlicht/ChIrrApp.h"
// Use the namespaces of Chrono
using namespace chrono;
using namespace chrono::irrlicht;
// Use the main namespaces of Irrlicht
using namespace irr;
using namespace irr::core;
using namespace irr::scene;
using namespace irr::video;
using namespace irr::io;
using namespace irr::gui;
void create_some_falling_items(ChSystemNSC& mphysicalSystem, ISceneManager* msceneManager, IVideoDriver* driver) {
for (int bi = 0; bi < 29; bi++) {
// Create a bunch of ChronoENGINE rigid bodies (spheres and
// boxes etc.) which will fall..
auto msphereBody = chrono_types::make_shared<ChBodyEasySphere>(1.1, // radius size
1000, // density
true, // collide enable?
true); // visualization?
msphereBody->SetPos(ChVector<>(-5 + ChRandom() * 10, 4 + bi * 0.05, -5 + ChRandom() * 10));
msphereBody->GetMaterialSurfaceNSC()->SetFriction(0.2f);
mphysicalSystem.Add(msphereBody);
// optional, attach a texture for better visualization
auto mtexture = chrono_types::make_shared<ChTexture>();
mtexture->SetTextureFilename(GetChronoDataFile("bluwhite.png"));
msphereBody->AddAsset(mtexture);
auto mboxBody = chrono_types::make_shared<ChBodyEasyBox>(1.5, 1.5, 1.5, // x,y,z size
100, // density
true, // collide enable?
true); // visualization?
mboxBody->SetPos(ChVector<>(-5 + ChRandom() * 10, 4 + bi * 0.05, -5 + ChRandom() * 10));
mphysicalSystem.Add(mboxBody);
// optional, attach a texture for better visualization
auto mtexturebox = chrono_types::make_shared<ChTexture>();
mtexturebox->SetTextureFilename(GetChronoDataFile("cubetexture_bluwhite.png"));
mboxBody->AddAsset(mtexturebox);
auto mcylBody = chrono_types::make_shared<ChBodyEasyCylinder>(0.75, 0.5, // radius, height
100, // density
true, // collide enable?
true); // visualization?
mcylBody->SetPos(ChVector<>(-5 + ChRandom() * 10, 4 + bi * 0.05, -5 + ChRandom() * 10));
mphysicalSystem.Add(mcylBody);
// optional, attach a texture for better visualization
auto mtexturecyl = chrono_types::make_shared<ChTexture>();
mtexturecyl->SetTextureFilename(GetChronoDataFile("pinkwhite.png"));
mcylBody->AddAsset(mtexturecyl);
}
// 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, true, true);
floorBody->SetPos(ChVector<>(0, -5, 0));
floorBody->SetBodyFixed(true);
mphysicalSystem.Add(floorBody);
auto wallBody1 = chrono_types::make_shared<ChBodyEasyBox>(1, 10, 20.99, 1000, true, true);
wallBody1->SetPos(ChVector<>(-10, 0, 0));
wallBody1->SetBodyFixed(true);
mphysicalSystem.Add(wallBody1);
auto wallBody2 = chrono_types::make_shared<ChBodyEasyBox>(1, 10, 20.99, 1000, true, true);
wallBody2->SetPos(ChVector<>(10, 0, 0));
wallBody2->SetBodyFixed(true);
mphysicalSystem.Add(wallBody2);
auto wallBody3 = chrono_types::make_shared<ChBodyEasyBox>(20.99, 10, 1, 1000, true, true);
wallBody3->SetPos(ChVector<>(0, 0, -10));
wallBody3->SetBodyFixed(true);
mphysicalSystem.Add(wallBody3);
auto wallBody4 = chrono_types::make_shared<ChBodyEasyBox>(20.99, 10, 1, 1000, true, true);
wallBody4->SetPos(ChVector<>(0, 0, 10));
wallBody4->SetBodyFixed(true);
mphysicalSystem.Add(wallBody4);
// optional, attach textures for better visualization
auto mtexturewall = chrono_types::make_shared<ChTexture>();
mtexturewall->SetTextureFilename(GetChronoDataFile("concrete.jpg"));
wallBody1->AddAsset(mtexturewall); // note: most assets can be shared
wallBody2->AddAsset(mtexturewall);
wallBody3->AddAsset(mtexturewall);
wallBody4->AddAsset(mtexturewall);
floorBody->AddAsset(mtexturewall);
// Add the rotating mixer
auto rotatingBody = chrono_types::make_shared<ChBodyEasyBox>(10, 5, 1, // x,y,z size
4000, // density
true, // collide enable?
true); // visualization?
rotatingBody->SetPos(ChVector<>(0, -1.6, 0));
rotatingBody->GetMaterialSurfaceNSC()->SetFriction(0.4f);
mphysicalSystem.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 / 2.0); // speed w=90�/s
my_motor->SetSpeedFunction(mfun);
mphysicalSystem.AddLink(my_motor);
/*
// create a plain ChBody (no colliding shape nor visualization mesh is used yet)
auto mrigidBody = chrono_types::make_shared<ChBody>();
// set the ChBodySceneNode as fixed body, and turn collision ON, otherwise no collide by default
mrigidBody->SetBodyFixed(true);
mrigidBody->SetCollide(true);
// Clear model. The colliding shape description MUST be between ClearModel() .. BuildModel() pair.
mrigidBody->GetCollisionModel()->ClearModel();
// Describe the (invisible) colliding shape by adding five boxes (the walls and floor)
mrigidBody->GetCollisionModel()->AddBox(20,1,20, ChVector<>( 0,-10, 0));
mrigidBody->GetCollisionModel()->AddBox(1,40,20, ChVector<>(-11, 0, 0));
mrigidBody->GetCollisionModel()->AddBox(1,40,20, ChVector<>( 11, 0, 0));
mrigidBody->GetCollisionModel()->AddBox(20,40,1, ChVector<>( 0, 0,-11));
mrigidBody->GetCollisionModel()->AddBox(20,40,1, ChVector<>( 0, 0, 11));
// Complete the description of collision shape.
mrigidBody->GetCollisionModel()->BuildModel();
// Attach some visualization shapes if needed:
auto vshape = chrono_types::make_shared<ChBoxShape>();
vshape->GetBoxGeometry().SetLengths( ChVector<> (20,1,20) );
vshape->GetBoxGeometry().Pos = ChVector<> (0,-5,0);
this->AddAsset( vshape );
// etc. for other 4 box shapes..
*/
}
int main(int argc, char* argv[]) {
GetLog() << "Copyright (c) 2017 projectchrono.org\nChrono version: " << CHRONO_VERSION << "\n\n";
// Create a ChronoENGINE physical system
ChSystemNSC mphysicalSystem;
// Create the Irrlicht visualization (open the Irrlicht device,
// bind a simple user interface, etc. etc.)
ChIrrApp application(&mphysicalSystem, L"Collisions between objects", core::dimension2d<u32>(800, 600), false);
// Easy shortcuts to add camera, lights, logo and sky in Irrlicht scene:
ChIrrWizard::add_typical_Logo(application.GetDevice());
ChIrrWizard::add_typical_Sky(application.GetDevice());
ChIrrWizard::add_typical_Lights(application.GetDevice());
ChIrrWizard::add_typical_Camera(application.GetDevice(), core::vector3df(0, 14, -20));
// Create all the rigid bodies.
create_some_falling_items(mphysicalSystem, application.GetSceneManager(), application.GetVideoDriver());
// Use this function for adding a ChIrrNodeAsset to all items
// Otherwise use application.AssetBind(myitem); on a per-item basis.
application.AssetBindAll();
// Use this function for 'converting' assets into Irrlicht meshes
application.AssetUpdateAll();
// Modify some setting of the physical system for the simulation, if you want
mphysicalSystem.SetSolverMaxIterations(20);
// mphysicalSystem.SetUseSleeping(true);
application.SetStepManage(true);
application.SetTimestep(0.02);
//
// THE SOFT-REAL-TIME CYCLE
//
while (application.GetDevice()->run()) {
application.BeginScene(true, true, SColor(255, 140, 161, 192));
application.DrawAll();
application.DoStep();
application.EndScene();
}
return 0;
}
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
static void add_typical_Camera(irr::IrrlichtDevice *device, irr::core::vector3df mpos=irr::core::vector3df(0, 0, -8), irr::core::vector3df mtarg=irr::core::vector3df(0, 0, 0))
A very basic and simple function which is just a shortcut to avoid lot of typing when someone wants t...
Definition: ChIrrWizard.cpp:53
void AssetUpdateAll()
For all items in a ChSystem, this function sets up the Irrlicht nodes corresponding to the geometric ...
Definition: ChIrrApp.cpp:49
ChLog & GetLog()
Global function to get the current ChLog object.
Definition: ChLog.cpp:39
void SetTimestep(double val)
Set/Get the time step for time integration.
Definition: ChIrrAppInterface.cpp:532
virtual void AddLink(std::shared_ptr< ChLinkBase > link)
Attach a link to this assembly.
Definition: ChAssembly.cpp:125
static void add_typical_Logo(irr::IrrlichtDevice *device, const std::string &mlogofilename=GetChronoDataFile("logo_chronoengine_alpha.png"))
A very basic and simple function which is just a shortcut to avoid lot of typing when someone wants t...
Definition: ChIrrWizard.cpp:20
static void add_typical_Sky(irr::IrrlichtDevice *device, const std::string &mtexturedir=GetChronoDataFile("skybox/"))
A very basic and simple function which is just a shortcut to avoid lot of typing when someone wants t...
Definition: ChIrrWizard.cpp:40
void SetStepManage(bool val)
If set to true, you can use DoStep() in the simulation loop to advance the simulation by one timestep...
Definition: ChIrrAppInterface.h:84
static void add_typical_Lights(irr::IrrlichtDevice *device, irr::core::vector3df pos1=irr::core::vector3df(30.f, 100.f, 30.f), irr::core::vector3df pos2=irr::core::vector3df(30.f, 80.f, -30.f), double rad1=290, double rad2=190, irr::video::SColorf col1=irr::video::SColorf(0.7f, 0.7f, 0.7f, 1.0f), irr::video::SColorf col2=irr::video::SColorf(0.7f, 0.8f, 0.8f, 1.0f))
A very basic and simple function which is just a shortcut to avoid lot of typing when someone wants t...
Definition: ChIrrWizard.cpp:25
ChFrame: a class for coordinate systems in 3D space.
Definition: ChFrame.h:42
Class to add some GUI to Irrlicht+ChronoEngine applications.
Definition: ChIrrApp.h:29
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 EndScene()
Call this to end the scene draw at the end of each animation frame.
Definition: ChIrrAppInterface.cpp:578
Namespace with classes for the Irrlicht module.
Definition: ChApiIrr.h:48
Projected SOR (Successive Over-Relaxation)
Definition of general purpose 3d vector variables, such as points in 3D.
Definition: ChVector.h:35
virtual void DoStep()
Call this important function inside a cycle like while(application.GetDevice()->run()) {....
Definition: ChIrrAppInterface.cpp:590
virtual 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:149
void Add(std::shared_ptr< ChPhysicsItem > item)
Attach an arbitrary ChPhysicsItem (e.g.
Definition: ChAssembly.cpp:190
void SetSolverMaxIterations(int max_iters)
Set the maximum number of iterations, if using an iterative solver.
Definition: ChSystem.cpp:123
double ChRandom()
Returns random value in (0..1) interval with Park-Miller method.
Definition: ChMathematics.cpp:53
void AssetBindAll()
Shortcut to add and bind a ChIrrNodeAsset to all items in a ChSystem.
Definition: ChIrrApp.cpp:41
virtual void BeginScene(bool backBuffer=true, bool zBuffer=true, irr::video::SColor color=irr::video::SColor(255, 0, 0, 0))
Call this to clean the canvas at the beginning of each animation frame.
Definition: ChIrrAppInterface.cpp:559
Main namespace for the Chrono package.
Definition: ChAsset.cpp:18
virtual void DrawAll()
Call this important function inside a loop like while(application.GetDevice()->run()) {....
Definition: ChIrrAppInterface.cpp:699
Class for a physical system in which contact is modeled using a non-smooth (complementarity-based) me...
Definition: ChSystemNSC.h:29