Falling bricks example (demo_IRR_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/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;
// Create a bunch of ChronoENGINE rigid bodies that
// represent bricks in a large wall.
void create_wall_bodies(ChSystemNSC& mphysicalSystem) {
// 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));
mphysicalSystem.Add(mrigidBody);
// optional, attach a texture for better visualization
auto mtexture = chrono_types::make_shared<ChTexture>();
mtexture->SetTextureFilename(GetChronoDataFile("textures/cubetexture_borders.png"));
mrigidBody->AddAsset(mtexture);
}
}
}
// 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);
mphysicalSystem.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
mphysicalSystem.Add(mrigidBall);
// optional, attach a texture for better visualization
auto mtextureball = chrono_types::make_shared<ChTexture>();
mtextureball->SetTextureFilename(GetChronoDataFile("textures/bluewhite.png"));
mrigidBall->AddAsset(mtextureball);
}
// Create a bunch of ChronoENGINE rigid bodies that
// represent bricks in a Jenga tower
void create_jengatower_bodies(ChSystemNSC& mphysicalSystem) {
// 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));
mphysicalSystem.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));
mphysicalSystem.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));
mphysicalSystem.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));
mphysicalSystem.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);
mphysicalSystem.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
mphysicalSystem.Add(mrigidBall);
// optional, attach a texture for better visualization
auto mtextureball = chrono_types::make_shared<ChTexture>();
mtextureball->SetTextureFilename(GetChronoDataFile("textures/bluewhite.png"));
mrigidBall->AddAsset(mtextureball);
}
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"Bricks test", core::dimension2d<u32>(800, 600));
application.AddTypicalLogo();
application.AddTypicalSky();
application.AddTypicalLights(core::vector3df(70.f, 120.f, -90.f), core::vector3df(30.f, 80.f, 60.f), 290, 190);
application.AddTypicalCamera(core::vector3df(-15, 14, -30), core::vector3df(0, 5, 0));
//
// HERE YOU POPULATE THE MECHANICAL SYSTEM OF CHRONO...
//
// Create all the rigid bodies.
create_wall_bodies(mphysicalSystem);
// create_jengatower_bodies (mphysicalSystem);
// Use this function for adding a ChIrrNodeAsset to all items
// If you need a finer control on which item really needs a visualization proxy in
// Irrlicht, just use application.AssetBind(myitem); on a per-item basis.
application.AssetBindAll();
// Use this function for 'converting' into Irrlicht meshes the assets
// into Irrlicht-visualizable meshes
application.AssetUpdateAll();
// Prepare the physical system for the simulation
auto solver = chrono_types::make_shared<ChSolverPSOR>();
solver->SetMaxIterations(40);
solver->EnableWarmStart(true);
mphysicalSystem.SetSolver(solver);
// mphysicalSystem.SetUseSleeping(true);
mphysicalSystem.SetMaxPenetrationRecoverySpeed(1.0);
// Simulation loop
application.SetTimestep(0.02);
application.SetTryRealtime(true);
while (application.GetDevice()->run()) {
application.BeginScene(true, true, SColor(255, 140, 161, 192));
tools::drawGrid(application.GetVideoDriver(), 5, 5, 20, 20,
ChCoordsys<>(ChVector<>(0, 0.04, 0), Q_from_AngAxis(CH_C_PI / 2, VECT_X)),
video::SColor(50, 90, 90, 150), true);
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
void Add(std::shared_ptr< ChPhysicsItem > item)
Attach an arbitrary ChPhysicsItem (e.g.
Definition: ChSystem.cpp:170
COORDSYS:
Definition: ChCoordsys.h:38
void AssetUpdateAll()
For all items in a ChSystem, this function sets up the Irrlicht nodes corresponding to the geometric ...
Definition: ChIrrApp.cpp:50
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:552
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:627
void SetMaxPenetrationRecoverySpeed(double mval)
For the default stepper, you can limit the speed of exiting from penetration situations.
Definition: ChSystem.h:163
Definition of general purpose 3d vector variables, such as points in 3D.
Definition: ChVector.h:35
virtual void DoStep()
Call this function inside a loop such as.
Definition: ChIrrAppInterface.cpp:637
virtual void SetSolver(std::shared_ptr< ChSolver > newsolver)
Attach a solver (derived from ChSolver) for use by this system.
Definition: ChSystem.cpp:307
void AssetBindAll()
Shortcut to add and bind a ChIrrNodeAsset to all items in a ChSystem.
Definition: ChIrrApp.cpp:42
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:610
Main namespace for the Chrono package.
Definition: ChAsset.cpp:18
virtual void DrawAll()
Call this function inside a loop such as.
Definition: ChIrrAppInterface.cpp:750
Class for a physical system in which contact is modeled using a non-smooth (complementarity-based) me...
Definition: ChSystemNSC.h:29
void SetTryRealtime(bool val)
If enabled, the function DoStep() will enforce soft real-time, by spinning in place until simulation ...
Definition: ChIrrAppInterface.h:98
void drawGrid(irr::video::IVideoDriver *driver, double ustep, double vstep, int nu, int nv, ChCoordsys<> mpos, irr::video::SColor mcol, bool use_Zbuffer)
Easy-to-use function to draw grids in 3D space, with given orientation, color and spacing.
Definition: ChIrrTools.cpp:693