NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgWorldBulletPhysicsImpl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17 */
18 
19 
27 // This module
29 // This application
30 #include "tgWorld.h"
31 #include "terrain/tgBulletGround.h"
32 // The Bullet Physics library
33 #include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h"
34 #include "BulletCollision/BroadphaseCollision/btDbvtBroadphase.h"
35 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
36 #include "BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h"
37 #include "BulletCollision/CollisionShapes/btBoxShape.h"
38 #include "BulletDynamics/Dynamics/btRigidBody.h"
39 #include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
40 #include "BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h"
41 #include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
42 #include "LinearMath/btDefaultMotionState.h"
43 #include "LinearMath/btScalar.h"
44 #include "LinearMath/btTransform.h"
45 #include "LinearMath/btVector3.h"
46 
52 {
53 public:
54  IntermediateBuildProducts() : dispatcher(&collisionConfiguration)
55  {
56  }
57  btSoftBodyRigidBodyCollisionConfiguration collisionConfiguration;
58  btCollisionDispatcher dispatcher;
59  btDbvtBroadphase broadphase;
60  btSequentialImpulseConstraintSolver solver;
61 };
62 
64  tgBulletGround* ground) :
65  tgWorldImpl(config, ground),
66  m_pIntermediateBuildProducts(new IntermediateBuildProducts),
67  m_pDynamicsWorld(createDynamicsWorld())
68 {
69  // Gravitational acceleration is down on the Y axis
70  const btVector3 gravityVector(0, -config.gravity, 0);
71  m_pDynamicsWorld->setGravity(gravityVector);
72 
73  // Create and add the ground rigid body
74  #if (0)
75  btRigidBody * const pGroundRigidBody = createGroundRigidBody();
76  m_pDynamicsWorld->addRigidBody(pGroundRigidBody);
77  #else
78  m_pDynamicsWorld->addRigidBody(ground->getGroundRigidBody());
79  #endif
80 
81  // Postcondition
82  assert(invariant());
83 }
84 
86 {
87  // Delete all the collision objects. The dynamics world must exist.
88  // Delete in reverse order of creation.
89  const size_t nco = m_pDynamicsWorld->getNumCollisionObjects();
90  btCollisionObjectArray& oa = m_pDynamicsWorld->getCollisionObjectArray();
91  for (int i = nco - 1; i >= 0; --i)
92  {
93  btCollisionObject * const pCollisionObject = oa[i];
94 
95  // If the collision object is a rigid body, delete its motion state
96  const btRigidBody* const pRigidBody =
97  btRigidBody::upcast(pCollisionObject);
98  if (pRigidBody)
99  {
100  delete pRigidBody->getMotionState();
101  }
102 
103  // Remove the collision object from the dynamics world
104  m_pDynamicsWorld->removeCollisionObject(pCollisionObject);
105  // Delete the collision object
106  delete pCollisionObject;
107  }
108  // All collision objects have been removed and deleted
109  assert(m_pDynamicsWorld->getNumCollisionObjects() == 0);
110 
111  // Delete all the collision shapes. This can be done at any time.
112  const size_t ncs = m_collisionShapes.size();
113  for (size_t i = 0; i < ncs; ++i) { delete m_collisionShapes[i]; }
114 
115  delete m_pDynamicsWorld;
116 
117  // Delete the intermediate build products, which are now orphaned
118  delete m_pIntermediateBuildProducts;
119 }
120 
125 btSoftRigidDynamicsWorld* tgWorldBulletPhysicsImpl::createDynamicsWorld() const
126 {
127  btSoftRigidDynamicsWorld * const result =
128  new btSoftRigidDynamicsWorld(&m_pIntermediateBuildProducts->dispatcher,
129  &m_pIntermediateBuildProducts->broadphase,
130  &m_pIntermediateBuildProducts->solver,
131  &m_pIntermediateBuildProducts->collisionConfiguration);
132 
133  return result;
134 }
135 
140 btRigidBody* tgWorldBulletPhysicsImpl::createGroundRigidBody()
141 {
142 const btScalar mass = 0.0;
143 
144  btTransform groundTransform;
145  groundTransform.setIdentity();
146  groundTransform.setOrigin(btVector3(0, -0.5, 0));
147 
148  // Using motionstate is recommended
149  // It provides interpolation capabilities, and only synchronizes 'active' objects
150  btDefaultMotionState* const pMotionState =
151  new btDefaultMotionState(groundTransform);
152 
153  const btVector3 groundDimensions(btScalar(500.0), btScalar(0.5), btScalar(500.0));
154  btBoxShape* const pGroundShape = new btBoxShape(groundDimensions);
155 
156  addCollisionShape(pGroundShape);
157 
158  const btVector3 localInertia(0, 0, 0);
159 
160  btRigidBody::btRigidBodyConstructionInfo const rbInfo(mass, pMotionState, pGroundShape, localInertia);
161 
162  btRigidBody* const pGroundRigidBody = new btRigidBody(rbInfo);
163 
164  return pGroundRigidBody;
165 }
166 
167 
169 {
170  // Precondition
171  assert(dt > 0.0);
172 
173  const btScalar timeStep = dt;
174  const int maxSubSteps = 1;
175  const btScalar fixedTimeStep = dt;
176  m_pDynamicsWorld->stepSimulation(timeStep, maxSubSteps, fixedTimeStep);
177 
178  // Postcondition
179  assert(invariant());
180 }
181 
182 void tgWorldBulletPhysicsImpl::addCollisionShape(btCollisionShape* pShape)
183 {
184  if (pShape)
185  {
186  m_collisionShapes.push_back(pShape);
187  }
188 
189  // Postcondition
190  assert(invariant());
191 }
192 
193 bool tgWorldBulletPhysicsImpl::invariant() const
194 {
195  return (m_pDynamicsWorld != 0);
196 }
Contains the definition of class tgBulletGround.
Contains the definition of class tgWorldBulletPhysicsImpl.
tgWorldBulletPhysicsImpl(const tgWorld::Config &config, tgBulletGround *ground)
double gravity
Definition: tgWorld.h:52
virtual btRigidBody * getGroundRigidBody() const =0
Contains the definition of class tgWorld $Id$.
void addCollisionShape(btCollisionShape *pShape)