Bullet Collision Detection & Physics Library
btDefaultSoftBodySolver.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. 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 {
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}
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
int size() const
return the number of elements in the array
void copyFromArray(const btAlignedObjectArray &otherArray)
virtual float * getBasePointer() const
Return the base pointer in memory to the first vertex.
virtual void updateSoftBodies()
Perform necessary per-step updates of soft bodies such as recomputing normals and bounding boxes.
virtual void predictMotion(btScalar solverdt)
Predict motion of soft bodies into next timestep.
btAlignedObjectArray< btSoftBody * > m_softBodySet
virtual void processCollision(btSoftBody *, const btCollisionObjectWrapper *)
virtual void optimize(btAlignedObjectArray< btSoftBody * > &softBodies, bool forceUpdate=false)
Optimize soft bodies in this solver.
virtual void copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
virtual void solveConstraints(btScalar solverdt)
Solve constraints for a set of soft bodies.
bool m_updateSolverConstants
Variable to define whether we need to update solver constants on the next iteration.
virtual void copyBackToSoftBodies(bool bMove=true)
Copy necessary data back to the original soft body source objects.
virtual bool checkInitialized()
Ensure that this solver is initialized.
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:75
void integrateMotion()
void defaultCollisionHandler(const btCollisionObjectWrapper *pcoWrap)
void predictMotion(btScalar dt)
void solveConstraints()
tNodeArray m_nodes
Definition: btSoftBody.h:814
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
const btScalar & getZ() const
Return the z value.
Definition: btVector3.h:565
const btScalar & getY() const
Return the y value.
Definition: btVector3.h:563
const btScalar & getX() const
Return the x value.
Definition: btVector3.h:561
virtual BufferTypes getBufferType() const =0
Return the type of the vertex buffer descriptor.
virtual int getNormalOffset() const
Return the vertex offset in floats from the base pointer.
virtual int getVertexOffset() const
Return the vertex offset in floats from the base pointer.
virtual int getNormalStride() const
Return the vertex stride in number of floats between vertices.
virtual int getVertexStride() const
Return the vertex stride in number of floats between vertices.