NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgBasicContactCableInfo.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 
28 
30 
31 #include "core/tgBulletUtil.h"
33 
34 #include "tgcreator/tgNode.h"
35 
36 
37 // The Bullet Physics Library
38 #include "btBulletDynamicsCommon.h"
39 #include "BulletCollision/CollisionDispatch/btGhostObject.h"
40 
42 m_config(config),
44 {}
45 
47 m_config(config),
48 tgConnectorInfo(tags)
49 {}
50 
52 m_config(config),
53 tgConnectorInfo(pair)
54 {}
55 
56 
58 {
59  return new tgBasicContactCableInfo(m_config, pair);
60 }
61 
62 void tgBasicContactCableInfo::initConnector(tgWorld& world)
63 {
64  // Note: tgBulletContactSpringCable holds pointers to things in the world, but it doesn't actually have any in-world representation.
65  m_bulletContactSpringCable = createTgBulletContactSpringCable(world);
66 }
67 
68 tgModel* tgBasicContactCableInfo::createModel(tgWorld& world)
69 {
70  // Don't have to do anything in the world for a tgBulletContactSpringCable...
71  // @todo: set the name based on joined tags, or remove name from the model...
72  //std::cout << "tgBasicContactCableInfo::createModel" << std::endl;
73 
74  // ensure connector has been initialized
75  assert(m_bulletContactSpringCable);
76  return new tgBasicActuator(m_bulletContactSpringCable, getTags(), m_config);
77 }
78 
79 double tgBasicContactCableInfo::getMass()
80 {
81  // @todo: calculate a mass? tgBulletContactSpringCable doesn't have mass...
82  return 0;
83 }
84 
85 
86 tgBulletContactSpringCable* tgBasicContactCableInfo::createTgBulletContactSpringCable(tgWorld& world)
87 {
88  //std::cout << "tgBasicActuatorInfo::createtgBulletContactSpringCable()" << std::endl;
89 
90  //std::cout << " getFromRigidInfo(): " << getFromRigidInfo() << std::endl;
91  //std::cout << " getFromRigidInfo(): " << getFromRigidInfo()->getRigidInfoGroup() << std::endl;
92 
93  // @todo: need to check somewhere that the rigid bodies have been set...
94  btRigidBody* fromBody = getFromRigidBody();
95  btRigidBody* toBody = getToRigidBody();
96 
97  tgNode from = getFromRigidInfo()->getConnectionPoint(getFrom(), getTo(), m_config.rotation);
98  tgNode to = getToRigidInfo()->getConnectionPoint(getTo(), getFrom(), m_config.rotation);
99 
100  std::vector<tgBulletSpringCableAnchor*> anchorList;
101 
102  tgBulletSpringCableAnchor* anchor1 = new tgBulletSpringCableAnchor(fromBody, from);
103  anchorList.push_back(anchor1);
104 
105  tgBulletSpringCableAnchor* anchor2 = new tgBulletSpringCableAnchor(toBody, to);
106  anchorList.push_back(anchor2);
107 
109 
110  btTransform transform = tgUtil::getTransform(from, to);
111 
112  btCompoundShape* m_compoundShape = new btCompoundShape(&world);
113 
114  btTransform t = transform;
115  btVector3 origin(0.0, 0.0, 0.0);
116  t.setOrigin(origin);
117 
118  // @todo import this! Only the first two params matter
119  btScalar radius = 0.001;
120  btScalar length = (from - to).length() / 2.0;
121  btBoxShape* box = new btBoxShape(btVector3(radius, length, radius));
122 
123  m_compoundShape->addChildShape(t, box);
124 
125  btPairCachingGhostObject* m_ghostObject = new btPairCachingGhostObject();
126 
127  // Don't double rotate
128  transform.setRotation(btQuaternion::getIdentity());
129 
130  m_ghostObject->setCollisionShape (m_compoundShape);
131  m_ghostObject->setWorldTransform(transform);
132  m_ghostObject->setCollisionFlags (btCollisionObject::CF_NO_CONTACT_RESPONSE);
133 
134  // Add ghost object to world
135  // @todo tgBulletContactSpringCable handles deleting from world - should it handle adding too?
136  btDynamicsWorld& m_dynamicsWorld = tgBulletUtil::worldToDynamicsWorld(world);
137  m_dynamicsWorld.addCollisionObject(m_ghostObject,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
138 
139  return new tgBulletContactSpringCable(m_ghostObject, world, anchorList, m_config.stiffness, m_config.damping, m_config.pretension);
140 }
141 
virtual tgConnectorInfo * createConnectorInfo(const tgPair &pair)
tgBasicContactCableInfo(const tgBasicActuator::Config &config)
static btDynamicsWorld & worldToDynamicsWorld(const tgWorld &world)
Definition of class tgBasicContactCableInfo.
Definition of class tgNode.
static btTransform getTransform(const btVector3 &startOrientation, const btVector3 &start, const btVector3 &end)
Definition: tgUtil.h:90
Definition: tgPair.h:48
Contains the definition of class tgBulletUtil.
Definition: tgNode.h:45
Definitions of class tgBulletSpringCableAnchor, formerly muscleAnchor.
virtual btVector3 getConnectionPoint(const btVector3 &referencePoint, const btVector3 &destinationPoint) const =0
Definition of a massless cable with contact dynamics.
Definition: tgTags.h:44