Bullet Collision Detection & Physics Library
btDeformableGravityForce.h
Go to the documentation of this file.
1 /*
2  Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
3 
4  Bullet Continuous Collision Detection and Physics Library
5  Copyright (c) 2019 Google Inc. http://bulletphysics.org
6  This software is provided 'as-is', without any express or implied warranty.
7  In no event will the authors be held liable for any damages arising from the use of this software.
8  Permission is granted to anyone to use this software for any purpose,
9  including commercial applications, and to alter it and redistribute it freely,
10  subject to the following restrictions:
11  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13  3. This notice may not be removed or altered from any source distribution.
14  */
15 
16 #ifndef BT_DEFORMABLE_GRAVITY_FORCE_H
17 #define BT_DEFORMABLE_GRAVITY_FORCE_H
18 
20 
22 {
23 public:
26 
28  {
29  }
30 
31  virtual void addScaledForces(btScalar scale, TVStack& force)
32  {
33  addScaledGravityForce(scale, force);
34  }
35 
36  virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
37  {
38  addScaledGravityForce(scale, force);
39  }
40 
41  virtual void addScaledDampingForce(btScalar scale, TVStack& force)
42  {
43  }
44 
45  virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
46  {
47  }
48 
49  virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
50  {
51  }
52 
53  virtual void addScaledGravityForce(btScalar scale, TVStack& force)
54  {
55  int numNodes = getNumNodes();
56  btAssert(numNodes <= force.size());
57  for (int i = 0; i < m_softBodies.size(); ++i)
58  {
59  btSoftBody* psb = m_softBodies[i];
60  if (!psb->isActive())
61  {
62  continue;
63  }
64  for (int j = 0; j < psb->m_nodes.size(); ++j)
65  {
66  btSoftBody::Node& n = psb->m_nodes[j];
67  size_t id = n.index;
68  btScalar mass = (n.m_im == 0) ? 0 : 1. / n.m_im;
69  btVector3 scaled_force = scale * m_gravity * mass;
70  force[id] += scaled_force;
71  }
72  }
73  }
74 
76  {
77  return BT_GRAVITY_FORCE;
78  }
79 
80  // the gravitational potential energy
81  virtual double totalEnergy(btScalar dt)
82  {
83  double e = 0;
84  for (int i = 0; i<m_softBodies.size();++i)
85  {
86  btSoftBody* psb = m_softBodies[i];
87  if (!psb->isActive())
88  {
89  continue;
90  }
91  for (int j = 0; j < psb->m_nodes.size(); ++j)
92  {
93  const btSoftBody::Node& node = psb->m_nodes[j];
94  if (node.m_im > 0)
95  {
96  e -= m_gravity.dot(node.m_q)/node.m_im;
97  }
98  }
99  }
100  return e;
101  }
102 
103 
104 };
105 #endif /* BT_DEFORMABLE_GRAVITY_FORCE_H */
btDeformableGravityForce::getForceType
virtual btDeformableLagrangianForceType getForceType()
Definition: btDeformableGravityForce.h:75
btDeformableGravityForce::TVStack
btAlignedObjectArray< btVector3 > TVStack
Definition: btDeformableGravityForce.h:24
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
BT_GRAVITY_FORCE
Definition: btDeformableLagrangianForce.h:25
btDeformableGravityForce::totalEnergy
virtual double totalEnergy(btScalar dt)
Definition: btDeformableGravityForce.h:81
btDeformableGravityForce::addScaledGravityForce
virtual void addScaledGravityForce(btScalar scale, TVStack &force)
Definition: btDeformableGravityForce.h:53
btDeformableGravityForce::m_gravity
btVector3 m_gravity
Definition: btDeformableGravityForce.h:25
btDeformableGravityForce::addScaledElasticForceDifferential
virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack &dx, TVStack &df)
Definition: btDeformableGravityForce.h:45
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:229
btSoftBody::Node
Definition: btSoftBody.h:255
btDeformableGravityForce::addScaledExplicitForce
virtual void addScaledExplicitForce(btScalar scale, TVStack &force)
Definition: btDeformableGravityForce.h:36
btSoftBody::Node::m_q
btVector3 m_q
Definition: btSoftBody.h:258
btAssert
#define btAssert(x)
Definition: btScalar.h:153
btDeformableGravityForce::addScaledDampingForce
virtual void addScaledDampingForce(btScalar scale, TVStack &force)
Definition: btDeformableGravityForce.h:41
btDeformableLagrangianForce::m_softBodies
btAlignedObjectArray< btSoftBody * > m_softBodies
Definition: btDeformableLagrangianForce.h:41
btDeformableLagrangianForce
Definition: btDeformableLagrangianForce.h:37
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:80
btSoftBody::Node::index
int index
Definition: btSoftBody.h:268
btDeformableGravityForce::btDeformableGravityForce
btDeformableGravityForce(const btVector3 &g)
Definition: btDeformableGravityForce.h:27
btAlignedObjectArray< btVector3 >
btDeformableGravityForce
Definition: btDeformableGravityForce.h:21
btSoftBody
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:72
btCollisionObject::isActive
bool isActive() const
Definition: btCollisionObject.h:294
btDeformableLagrangianForce::getNumNodes
virtual int getNumNodes()
Definition: btDeformableLagrangianForce.h:72
btDeformableLagrangianForceType
btDeformableLagrangianForceType
Definition: btDeformableLagrangianForce.h:23
btDeformableGravityForce::addScaledDampingForceDifferential
virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack &dv, TVStack &df)
Definition: btDeformableGravityForce.h:49
btSoftBody::Node::m_im
btScalar m_im
Definition: btSoftBody.h:264
btDeformableLagrangianForce.h
btDeformableGravityForce::addScaledForces
virtual void addScaledForces(btScalar scale, TVStack &force)
Definition: btDeformableGravityForce.h:31
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:142
btSoftBody::m_nodes
tNodeArray m_nodes
Definition: btSoftBody.h:777