Bullet Collision Detection & Physics Library
btDefaultSoftBodySolver.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
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 
19 
23 
25 {
26  // Initial we will clearly need to update solver constants
27  // For now this is global for the cloths linked with this solver - we should probably make this body specific
28  // for performance in future once we understand more clearly when constants need to be updated
30 }
31 
33 {
34 }
35 
36 // In this case the data is already in the soft bodies so there is no need for us to do anything
38 {
39 }
40 
42 {
43  m_softBodySet.copyFromArray(softBodies);
44 }
45 
47 {
48  for (int i = 0; i < m_softBodySet.size(); i++)
49  {
50  btSoftBody *psb = (btSoftBody *)m_softBodySet[i];
51  if (psb->isActive())
52  {
53  psb->integrateMotion();
54  }
55  }
56 } // updateSoftBodies
57 
59 {
60  return true;
61 }
62 
64 {
65  // Solve constraints for non-solver softbodies
66  for (int i = 0; i < m_softBodySet.size(); ++i)
67  {
68  btSoftBody *psb = static_cast<btSoftBody *>(m_softBodySet[i]);
69  if (psb->isActive())
70  {
71  psb->solveConstraints();
72  }
73  }
74 } // btDefaultSoftBodySolver::solveConstraints
75 
77 {
78  // Currently only support CPU output buffers
79  // TODO: check for DX11 buffers. Take all offsets into the same DX11 buffer
80  // and use them together on a single kernel call if possible by setting up a
81  // per-cloth target buffer array for the copy kernel.
82 
84  {
85  const btAlignedObjectArray<btSoftBody::Node> &clothVertices(softBody->m_nodes);
86  int numVertices = clothVertices.size();
87 
88  const btCPUVertexBufferDescriptor *cpuVertexBuffer = static_cast<btCPUVertexBufferDescriptor *>(vertexBuffer);
89  float *basePointer = cpuVertexBuffer->getBasePointer();
90 
91  if (vertexBuffer->hasVertexPositions())
92  {
93  const int vertexOffset = cpuVertexBuffer->getVertexOffset();
94  const int vertexStride = cpuVertexBuffer->getVertexStride();
95  float *vertexPointer = basePointer + vertexOffset;
96 
97  for (int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex)
98  {
99  btVector3 position = clothVertices[vertexIndex].m_x;
100  *(vertexPointer + 0) = (float)position.getX();
101  *(vertexPointer + 1) = (float)position.getY();
102  *(vertexPointer + 2) = (float)position.getZ();
103  vertexPointer += vertexStride;
104  }
105  }
106  if (vertexBuffer->hasNormals())
107  {
108  const int normalOffset = cpuVertexBuffer->getNormalOffset();
109  const int normalStride = cpuVertexBuffer->getNormalStride();
110  float *normalPointer = basePointer + normalOffset;
111 
112  for (int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex)
113  {
114  btVector3 normal = clothVertices[vertexIndex].m_n;
115  *(normalPointer + 0) = (float)normal.getX();
116  *(normalPointer + 1) = (float)normal.getY();
117  *(normalPointer + 2) = (float)normal.getZ();
118  normalPointer += normalStride;
119  }
120  }
121  }
122 } // btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
123 
125 {
126  softBody->defaultCollisionHandler(otherSoftBody);
127 }
128 
129 // For the default solver just leave the soft body to do its collision processing
131 {
132  softBody->defaultCollisionHandler(collisionObjectWrap);
133 } // btDefaultSoftBodySolver::processCollision
134 
136 {
137  for (int i = 0; i < m_softBodySet.size(); ++i)
138  {
139  btSoftBody *psb = m_softBodySet[i];
140 
141  if (psb->isActive())
142  {
143  psb->predictMotion(timeStep);
144  }
145  }
146 }
btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
virtual void copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
Definition: btDefaultSoftBodySolver.cpp:76
btCollisionShape.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btSoftBody::defaultCollisionHandler
void defaultCollisionHandler(const btCollisionObjectWrapper *pcoWrap)
Definition: btSoftBody.cpp:3613
btCollisionObjectWrapper
Definition: btCollisionObjectWrapper.h:17
btAlignedObjectArray::copyFromArray
void copyFromArray(const btAlignedObjectArray &otherArray)
Definition: btAlignedObjectArray.h:496
btDefaultSoftBodySolver::btDefaultSoftBodySolver
btDefaultSoftBodySolver()
Definition: btDefaultSoftBodySolver.cpp:24
btDefaultSoftBodySolver::m_updateSolverConstants
bool m_updateSolverConstants
Variable to define whether we need to update solver constants on the next iteration.
Definition: btDefaultSoftBodySolver.h:27
btVertexBufferDescriptor::hasVertexPositions
virtual bool hasVertexPositions() const
Definition: btSoftBodySolverVertexBuffer.h:54
btSoftBody::integrateMotion
void integrateMotion()
Definition: btSoftBody.cpp:2113
btCPUVertexBufferDescriptor::getBasePointer
virtual float * getBasePointer() const
Return the base pointer in memory to the first vertex.
Definition: btSoftBodySolverVertexBuffer.h:154
btDefaultSoftBodySolver.h
btVertexBufferDescriptor::hasNormals
virtual bool hasNormals() const
Definition: btSoftBodySolverVertexBuffer.h:59
btVector3::getX
const btScalar & getX() const
Return the x value.
Definition: btVector3.h:561
btDefaultSoftBodySolver::optimize
virtual void optimize(btAlignedObjectArray< btSoftBody * > &softBodies, bool forceUpdate=false)
Optimize soft bodies in this solver.
Definition: btDefaultSoftBodySolver.cpp:41
btTriangleIndexVertexArray.h
btDefaultSoftBodySolver::copyBackToSoftBodies
virtual void copyBackToSoftBodies(bool bMove=true)
Copy necessary data back to the original soft body source objects.
Definition: btDefaultSoftBodySolver.cpp:37
btDefaultSoftBodySolver::predictMotion
virtual void predictMotion(btScalar solverdt)
Predict motion of soft bodies into next timestep.
Definition: btDefaultSoftBodySolver.cpp:135
btVertexBufferDescriptor
Definition: btSoftBodySolverVertexBuffer.h:19
btVertexBufferDescriptor::getVertexStride
virtual int getVertexStride() const
Return the vertex stride in number of floats between vertices.
Definition: btSoftBodySolverVertexBuffer.h:80
btDefaultSoftBodySolver::updateSoftBodies
virtual void updateSoftBodies()
Perform necessary per-step updates of soft bodies such as recomputing normals and bounding boxes.
Definition: btDefaultSoftBodySolver.cpp:46
btSoftBody::predictMotion
void predictMotion(btScalar dt)
Definition: btSoftBody.cpp:1863
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:80
btVertexBufferDescriptor::getNormalOffset
virtual int getNormalOffset() const
Return the vertex offset in floats from the base pointer.
Definition: btSoftBodySolverVertexBuffer.h:88
btSoftBody::solveConstraints
void solveConstraints()
Definition: btSoftBody.cpp:1973
btVector3::getZ
const btScalar & getZ() const
Return the z value.
Definition: btVector3.h:565
btDefaultSoftBodySolver::m_softBodySet
btAlignedObjectArray< btSoftBody * > m_softBodySet
Definition: btDefaultSoftBodySolver.h:29
btAlignedObjectArray< btSoftBody * >
btVector3::getY
const btScalar & getY() const
Return the y value.
Definition: btVector3.h:563
btDefaultSoftBodySolver::checkInitialized
virtual bool checkInitialized()
Ensure that this solver is initialized.
Definition: btDefaultSoftBodySolver.cpp:58
btDefaultSoftBodySolver::solveConstraints
virtual void solveConstraints(btScalar solverdt)
Solve constraints for a set of soft bodies.
Definition: btDefaultSoftBodySolver.cpp:63
btSoftBody
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:72
btDefaultSoftBodySolver::processCollision
virtual void processCollision(btSoftBody *, const btCollisionObjectWrapper *)
Definition: btDefaultSoftBodySolver.cpp:130
btCPUVertexBufferDescriptor
Definition: btSoftBodySolverVertexBuffer.h:102
btVertexBufferDescriptor::getVertexOffset
virtual int getVertexOffset() const
Return the vertex offset in floats from the base pointer.
Definition: btSoftBodySolverVertexBuffer.h:72
btCollisionObject::isActive
bool isActive() const
Definition: btCollisionObject.h:294
btCollisionObject.h
btVertexBufferDescriptor::CPU_BUFFER
Definition: btSoftBodySolverVertexBuffer.h:24
btDefaultSoftBodySolver::~btDefaultSoftBodySolver
virtual ~btDefaultSoftBodySolver()
Definition: btDefaultSoftBodySolver.cpp:32
btVertexBufferDescriptor::getBufferType
virtual BufferTypes getBufferType() const =0
Return the type of the vertex buffer descriptor.
btSoftBody.h
btVertexBufferDescriptor::getNormalStride
virtual int getNormalStride() const
Return the vertex stride in number of floats between vertices.
Definition: btSoftBodySolverVertexBuffer.h:96
btCapsuleShape.h
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