Falling bricks example (demo_MBS_bricks.cpp)

A tutorial about collisions.

Create a stack of bricks and a wrecking ball that collides with them.

Learn about:

  • how to enable collisions
  • how to use the surface material
  • how to impose an initial speed to a body
// =============================================================================
// 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
// - sharing a ChMaterialSurfaceNSC property between bodies
//
// =============================================================================
#include "chrono/physics/ChSystemNSC.h"
#include "chrono/physics/ChBodyEasy.h"
#include "chrono/solver/ChSolverPSOR.h"
#include "chrono/assets/ChTexture.h"
#include "chrono_irrlicht/ChVisualSystemIrrlicht.h"
// Use the namespaces of Chrono
using namespace chrono;
using namespace chrono::irrlicht;
// Create a bunch of ChronoENGINE rigid bodies that
// represent bricks in a large wall.
void create_wall_bodies(ChSystemNSC& sys) {
// Create a material that will be shared among all collision shapes
auto mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
mat->SetFriction(0.4f);
mat->SetCompliance(0.0);
mat->SetComplianceT(0.0);
mat->SetDampingF(0.2f);
// Create bricks
for (int ai = 0; ai < 1; ai++) { // N. of walls
for (int bi = 0; bi < 10; bi++) { // N. of vert. bricks
for (int ui = 0; ui < 15; ui++) { // N. of hor. bricks
auto mrigidBody = chrono_types::make_shared<ChBodyEasyBox>(3.96, 2, 4, // x,y,z size
100, // density
true, // visualization?
true, // collision?
mat); // contact material
mrigidBody->SetPos(ChVector<>(-8 + ui * 4.0 + 2 * (bi % 2), 1.0 + bi * 2.0, ai * 9));
mrigidBody->GetVisualShape(0)->SetTexture(GetChronoDataFile("textures/cubetexture_borders.png"));
sys.Add(mrigidBody);
}
}
}
// Create the floor using
// fixed rigid body of 'box' type:
auto mrigidFloor = chrono_types::make_shared<ChBodyEasyBox>(250, 4, 250, // x,y,z size
1000, // density
true, // visulization?
true, // collision?
mat); // contact material
mrigidFloor->SetPos(ChVector<>(0, -2, 0));
mrigidFloor->SetBodyFixed(true);
sys.Add(mrigidFloor);
// Create a ball that will collide with wall
auto mrigidBall = chrono_types::make_shared<ChBodyEasySphere>(4, // radius
8000, // density
true, // visualization?
true, // collision?
mat); // contact material
mrigidBall->SetPos(ChVector<>(0, -2, 0));
mrigidBall->SetPos(ChVector<>(0, 3, -8));
mrigidBall->SetPos_dt(ChVector<>(0, 0, 16)); // set initial speed
mrigidBall->GetVisualShape(0)->SetTexture(GetChronoDataFile("textures/bluewhite.png"));
sys.Add(mrigidBall);
}
// Create a bunch of ChronoENGINE rigid bodies that
// represent bricks in a Jenga tower
void create_jengatower_bodies(ChSystemNSC& sys) {
// Create a material that will be shared among all collision shapes
auto mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
mat->SetFriction(0.4f);
mat->SetCompliance(0.0);
mat->SetComplianceT(0.0);
mat->SetDampingF(0.2f);
// Create bricks
for (int bi = 0; bi < 12; bi += 2) {
auto mrigidBody1 = chrono_types::make_shared<ChBodyEasyBox>(2, 2, 14, // x,y,z size
100, // density
true, // visualization?
true, // collision?
mat); // contact material
mrigidBody1->SetPos(ChVector<>(-5, 1.0 + bi * 2.0, 0));
sys.Add(mrigidBody1);
auto mrigidBody2 = chrono_types::make_shared<ChBodyEasyBox>(2, 2, 14, // x,y,z size
100, // density
true, // visualization?
true, // collision?
mat); // contact material
mrigidBody2->SetPos(ChVector<>(5, 1.0 + bi * 2.0, 0));
sys.Add(mrigidBody2);
auto mrigidBody3 = chrono_types::make_shared<ChBodyEasyBox>(14, 2, 2, // x,y,z size
100, // density
true, // visualization?
true, // collision?
mat); // contact material
mrigidBody3->SetPos(ChVector<>(0, 3.0 + bi * 2.0, 5));
sys.Add(mrigidBody3);
auto mrigidBody4 = chrono_types::make_shared<ChBodyEasyBox>(14, 2, 2, // x,y,z size
100, // density
true, // visualization?
true, // collision?
mat); // contact material
mrigidBody4->SetPos(ChVector<>(0, 3.0 + bi * 2.0, -5));
sys.Add(mrigidBody4);
}
// Create the floor using
// fixed rigid body of 'box' type:
auto mrigidFloor = chrono_types::make_shared<ChBodyEasyBox>(250, 4, 250, // x,y,z size
1000, // density
true, // visualization?
true, // collision?
mat); // contact material
mrigidFloor->SetPos(ChVector<>(0, -2, 0));
mrigidFloor->SetBodyFixed(true);
sys.Add(mrigidFloor);
// Create a ball that will collide with tower
auto mrigidBall = chrono_types::make_shared<ChBodyEasySphere>(4, // radius
1000, // density
true, // visualization?
true, // collision?
mat); // contact material
mrigidBall->SetPos(ChVector<>(0, 3, -8));
mrigidBall->SetPos_dt(ChVector<>(0, 0, 2)); // set initial speed
mrigidBall->GetVisualShape(0)->SetTexture(GetChronoDataFile("textures/bluewhite.png"));
sys.Add(mrigidBall);
}
int main(int argc, char* argv[]) {
GetLog() << "Copyright (c) 2017 projectchrono.org\nChrono version: " << CHRONO_VERSION << "\n\n";
// Create a ChronoENGINE physical system
// Create all the rigid bodies.
create_wall_bodies(sys);
// create_jengatower_bodies (sys);
// Create the Irrlicht visualization system
auto vis = chrono_types::make_shared<ChVisualSystemIrrlicht>();
vis->AttachSystem(&sys);
vis->SetWindowSize(800, 600);
vis->SetWindowTitle("Bricks test");
vis->Initialize();
vis->AddLogo();
vis->AddSkyBox();
vis->AddLight(ChVector<>(70, 120, -90), 290, ChColor(0.7f, 0.7f, 0.7f));
vis->AddLight(ChVector<>(30, 80, 60), 190, ChColor(0.7f, 0.8f, 0.8f));
vis->AddCamera(ChVector<>(-15, 14, -30), ChVector<>(0, 5, 0));
// Prepare the physical system for the simulation
auto solver = chrono_types::make_shared<ChSolverPSOR>();
solver->SetMaxIterations(40);
solver->EnableWarmStart(true);
sys.SetSolver(solver);
// sys.SetUseSleeping(true);
// Simulation loop
while (vis->Run()) {
vis->BeginScene();
tools::drawGrid(vis.get(), 5, 5, 20, 20,
ChCoordsys<>(ChVector<>(0, 0.04, 0), Q_from_AngAxis(CH_C_PI / 2, VECT_X)),
ChColor(0.35f, 0.35f, 0.59f), true);
vis->Render();
vis->EndScene();
sys.DoStepDynamics(0.02);
}
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
void Add(std::shared_ptr< ChPhysicsItem > item)
Attach an arbitrary ChPhysicsItem (e.g.
Definition: ChSystem.cpp:183
COORDSYS:
Definition: ChCoordsys.h:38
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
bool Run()
Run the Irrlicht device.
Definition: ChVisualSystemIrrlicht.cpp:205
irr::scene::ILightSceneNode * AddLight(const ChVector<> &pos, double radius, ChColor color=ChColor(0.7f, 0.7f, 0.7f))
Add a point light to the scene.
Definition: ChVisualSystemIrrlicht.cpp:332
void AddCamera(const ChVector<> &pos, ChVector<> targ=VNULL)
Add a camera in an Irrlicht 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:256
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
void SetMaxPenetrationRecoverySpeed(double mval)
For the default stepper, you can limit the speed of exiting from penetration situations.
Definition: ChSystem.h:166
Definition of a visual color.
Definition: ChColor.h:26
Definition of general purpose 3d vector variables, such as points in 3D.
Definition: ChVector.h:35
void drawGrid(ChVisualSystemIrrlicht *vis, double ustep, double vstep, int nu, int nv, ChCoordsys<> pos, chrono::ChColor col, bool use_Zbuffer)
Draw grids in 3D space with given orientation, color, and spacing.
Definition: ChIrrTools.cpp:794
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 SetWindowTitle(const std::string &win_title)
Set the windoiw title (default "").
Definition: ChVisualSystemIrrlicht.cpp:118
virtual void AttachSystem(ChSystem *sys) override
Attach another Chrono system to the run-time visualization system.
Definition: ChVisualSystemIrrlicht.cpp:135
virtual void SetSolver(std::shared_ptr< ChSolver > newsolver)
Attach a solver (derived from ChSolver) for use by this system.
Definition: ChSystem.cpp:339
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