NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgBulletUtil.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 
25 // This module
26 #include "tgBulletUtil.h"
27 // This application
28 #include "tgWorld.h"
30 // The Bullet Physics library
31 #include "BulletCollision/CollisionShapes/btCollisionShape.h"
32 #include "BulletDynamics/Dynamics/btDynamicsWorld.h"
33 #include "BulletDynamics/Dynamics/btRigidBody.h"
34 #include "LinearMath/btTransform.h"
35 #include "LinearMath/btDefaultMotionState.h"
36 
37 // @todo: Move this to the tgRigidInfo => tgModel step
38 // NOTE: this is a copy of localCreateRigidBody from the bullet DemoApplication.
39 btRigidBody* tgBulletUtil::createRigidBody(btDynamicsWorld* dynamicsWorld,
40  float mass,
41  const btTransform& startTransform,
42  btCollisionShape* shape)
43 {
44 
45  btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));
46 
47  //rigidbody is dynamic if and only if mass is non zero, otherwise static
48  bool isDynamic = (mass != 0.f);
49 
50  btVector3 localInertia(0,0,0);
51  if (isDynamic)
52  shape->calculateLocalInertia(mass,localInertia);
53 
54 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
55 
56 #define USE_MOTIONSTATE 1
57 #ifdef USE_MOTIONSTATE
58  btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
59 
60  btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);
61 
62  // This is defined in DemoApplication as BT_LARGE_FLOAT 1e30 if using
63  // double precision, 1e18.f if using single
64  double defaultContactProcessingThreshold = 1.0e30; // @TODO: What should this be?
65 
66  btRigidBody* body = new btRigidBody(cInfo);
67  body->setContactProcessingThreshold(defaultContactProcessingThreshold);
68 
69 #else
70  btRigidBody* body = new btRigidBody(mass,0,shape,localInertia);
71  body->setWorldTransform(startTransform);
72 #endif//
73 
74  dynamicsWorld->addRigidBody(body);
75 
76  return body;
77 }
78 
79 btDynamicsWorld& tgBulletUtil::worldToDynamicsWorld(const tgWorld& world)
80 {
81  // Fetch the world's implementation.
82  tgWorldImpl& impl = world.implementation();
83  // Downcast it for Bullet Physics.
84  // Avoid dynamic_cast because it is slow and this is called frequently.
86  tgWorldBulletPhysicsImpl& bulletPhysicsImpl =
87  static_cast<tgWorldBulletPhysicsImpl&>(impl);
88  btDynamicsWorld& result = bulletPhysicsImpl.dynamicsWorld();
89  return result;
90 }
Contains the definition of class tgWorldBulletPhysicsImpl.
static btDynamicsWorld & worldToDynamicsWorld(const tgWorld &world)
btDynamicsWorld & dynamicsWorld() const
tgWorldImpl & implementation() const
Definition: tgWorld.h:102
Contains the definition of class tgWorld $Id$.
Contains the definition of class tgBulletUtil.