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 
24 
26 {
27  // Initial we will clearly need to update solver constants
28  // For now this is global for the cloths linked with this solver - we should probably make this body specific
29  // for performance in future once we understand more clearly when constants need to be updated
31 }
32 
34 {
35 }
36 
37 // In this case the data is already in the soft bodies so there is no need for us to do anything
39 {
40 
41 }
42 
44 {
45  m_softBodySet.copyFromArray( softBodies );
46 }
47 
49 {
50  for ( int i=0; i < m_softBodySet.size(); i++)
51  {
53  if (psb->isActive())
54  {
55  psb->integrateMotion();
56  }
57  }
58 } // updateSoftBodies
59 
61 {
62  return true;
63 }
64 
66 {
67  // Solve constraints for non-solver softbodies
68  for(int i=0; i < m_softBodySet.size(); ++i)
69  {
70  btSoftBody* psb = static_cast<btSoftBody*>(m_softBodySet[i]);
71  if (psb->isActive())
72  {
73  psb->solveConstraints();
74  }
75  }
76 } // btDefaultSoftBodySolver::solveConstraints
77 
78 
80 {
81  // Currently only support CPU output buffers
82  // TODO: check for DX11 buffers. Take all offsets into the same DX11 buffer
83  // and use them together on a single kernel call if possible by setting up a
84  // per-cloth target buffer array for the copy kernel.
85 
87  {
88  const btAlignedObjectArray<btSoftBody::Node> &clothVertices( softBody->m_nodes );
89  int numVertices = clothVertices.size();
90 
91  const btCPUVertexBufferDescriptor *cpuVertexBuffer = static_cast< btCPUVertexBufferDescriptor* >(vertexBuffer);
92  float *basePointer = cpuVertexBuffer->getBasePointer();
93 
94  if( vertexBuffer->hasVertexPositions() )
95  {
96  const int vertexOffset = cpuVertexBuffer->getVertexOffset();
97  const int vertexStride = cpuVertexBuffer->getVertexStride();
98  float *vertexPointer = basePointer + vertexOffset;
99 
100  for( int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex )
101  {
102  btVector3 position = clothVertices[vertexIndex].m_x;
103  *(vertexPointer + 0) = (float)position.getX();
104  *(vertexPointer + 1) = (float)position.getY();
105  *(vertexPointer + 2) = (float)position.getZ();
106  vertexPointer += vertexStride;
107  }
108  }
109  if( vertexBuffer->hasNormals() )
110  {
111  const int normalOffset = cpuVertexBuffer->getNormalOffset();
112  const int normalStride = cpuVertexBuffer->getNormalStride();
113  float *normalPointer = basePointer + normalOffset;
114 
115  for( int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex )
116  {
117  btVector3 normal = clothVertices[vertexIndex].m_n;
118  *(normalPointer + 0) = (float)normal.getX();
119  *(normalPointer + 1) = (float)normal.getY();
120  *(normalPointer + 2) = (float)normal.getZ();
121  normalPointer += normalStride;
122  }
123  }
124  }
125 } // btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
126 
128 {
129  softBody->defaultCollisionHandler( otherSoftBody);
130 }
131 
132 // For the default solver just leave the soft body to do its collision processing
134 {
135  softBody->defaultCollisionHandler( collisionObjectWrap );
136 } // btDefaultSoftBodySolver::processCollision
137 
138 
140 {
141  for ( int i=0; i < m_softBodySet.size(); ++i)
142  {
143  btSoftBody* psb = m_softBodySet[i];
144 
145  if (psb->isActive())
146  {
147  psb->predictMotion(timeStep);
148  }
149  }
150 }
151 
btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
virtual void copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
Definition: btDefaultSoftBodySolver.cpp:79
btCollisionShape.h
btDefaultSoftBodySolver::solveConstraints
virtual void solveConstraints(float solverdt)
Solve constraints for a set of soft bodies.
Definition: btDefaultSoftBodySolver.cpp:65
btSoftBody::defaultCollisionHandler
void defaultCollisionHandler(const btCollisionObjectWrapper *pcoWrap)
Definition: btSoftBody.cpp:3205
btCollisionObjectWrapper
Definition: btCollisionObjectWrapper.h:17
btAlignedObjectArray::copyFromArray
void copyFromArray(const btAlignedObjectArray &otherArray)
Definition: btAlignedObjectArray.h:521
btDefaultSoftBodySolver::btDefaultSoftBodySolver
btDefaultSoftBodySolver()
Definition: btDefaultSoftBodySolver.cpp:25
btDefaultSoftBodySolver::m_updateSolverConstants
bool m_updateSolverConstants
Variable to define whether we need to update solver constants on the next iteration.
Definition: btDefaultSoftBodySolver.h:28
btVertexBufferDescriptor::hasVertexPositions
virtual bool hasVertexPositions() const
Definition: btSoftBodySolverVertexBuffer.h:57
btSoftBody::integrateMotion
void integrateMotion()
Definition: btSoftBody.cpp:1990
btCPUVertexBufferDescriptor::getBasePointer
virtual float * getBasePointer() const
Return the base pointer in memory to the first vertex.
Definition: btSoftBodySolverVertexBuffer.h:159
btDefaultSoftBodySolver.h
btVertexBufferDescriptor::hasNormals
virtual bool hasNormals() const
Definition: btSoftBodySolverVertexBuffer.h:62
btVector3::getX
const btScalar & getX() const
Return the x value.
Definition: btVector3.h:573
btDefaultSoftBodySolver::optimize
virtual void optimize(btAlignedObjectArray< btSoftBody * > &softBodies, bool forceUpdate=false)
Optimize soft bodies in this solver.
Definition: btDefaultSoftBodySolver.cpp:43
btTriangleIndexVertexArray.h
btDefaultSoftBodySolver::copyBackToSoftBodies
virtual void copyBackToSoftBodies(bool bMove=true)
Copy necessary data back to the original soft body source objects.
Definition: btDefaultSoftBodySolver.cpp:38
btVertexBufferDescriptor
Definition: btSoftBodySolverVertexBuffer.h:20
btVertexBufferDescriptor::getVertexStride
virtual int getVertexStride() const
Return the vertex stride in number of floats between vertices.
Definition: btSoftBodySolverVertexBuffer.h:83
btDefaultSoftBodySolver::updateSoftBodies
virtual void updateSoftBodies()
Perform necessary per-step updates of soft bodies such as recomputing normals and bounding boxes.
Definition: btDefaultSoftBodySolver.cpp:48
btSoftBody::predictMotion
void predictMotion(btScalar dt)
Definition: btSoftBody.cpp:1741
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btDefaultSoftBodySolver::m_softBodySet
btAlignedObjectArray< btSoftBody * > m_softBodySet
Definition: btDefaultSoftBodySolver.h:30
btVertexBufferDescriptor::getNormalOffset
virtual int getNormalOffset() const
Return the vertex offset in floats from the base pointer.
Definition: btSoftBodySolverVertexBuffer.h:91
btSoftBody::solveConstraints
void solveConstraints()
Definition: btSoftBody.cpp:1849
btVector3::getZ
const btScalar & getZ() const
Return the z value.
Definition: btVector3.h:577
btAlignedObjectArray< btSoftBody * >
btVector3::getY
const btScalar & getY() const
Return the y value.
Definition: btVector3.h:575
btDefaultSoftBodySolver::checkInitialized
virtual bool checkInitialized()
Ensure that this solver is initialized.
Definition: btDefaultSoftBodySolver.cpp:60
btSoftBody
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:71
btDefaultSoftBodySolver::processCollision
virtual void processCollision(btSoftBody *, const btCollisionObjectWrapper *)
Definition: btDefaultSoftBodySolver.cpp:133
btCPUVertexBufferDescriptor
Definition: btSoftBodySolverVertexBuffer.h:106
btVertexBufferDescriptor::getVertexOffset
virtual int getVertexOffset() const
Return the vertex offset in floats from the base pointer.
Definition: btSoftBodySolverVertexBuffer.h:75
btCollisionObject::isActive
bool isActive() const
Definition: btCollisionObject.h:299
btCollisionObject.h
btVertexBufferDescriptor::CPU_BUFFER
@ CPU_BUFFER
Definition: btSoftBodySolverVertexBuffer.h:25
btDefaultSoftBodySolver::~btDefaultSoftBodySolver
virtual ~btDefaultSoftBodySolver()
Definition: btDefaultSoftBodySolver.cpp:33
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:99
btCapsuleShape.h
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:155
btSoftBody::m_nodes
tNodeArray m_nodes
Definition: btSoftBody.h:659
btDefaultSoftBodySolver::predictMotion
virtual void predictMotion(float solverdt)
Predict motion of soft bodies into next timestep.
Definition: btDefaultSoftBodySolver.cpp:139