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
16 #include "BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h"
17 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
18 #include "BulletCollision/CollisionShapes/btCollisionShape.h"
19
20 #include "btDefaultSoftBodySolver.h"
21 #include "BulletCollision/CollisionShapes/btCapsuleShape.h"
22 #include "BulletSoftBody/btSoftBody.h"
23
btDefaultSoftBodySolver()24 btDefaultSoftBodySolver::btDefaultSoftBodySolver()
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
29 m_updateSolverConstants = true;
30 }
31
~btDefaultSoftBodySolver()32 btDefaultSoftBodySolver::~btDefaultSoftBodySolver()
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
copyBackToSoftBodies(bool bMove)37 void btDefaultSoftBodySolver::copyBackToSoftBodies(bool bMove)
38 {
39 }
40
optimize(btAlignedObjectArray<btSoftBody * > & softBodies,bool forceUpdate)41 void btDefaultSoftBodySolver::optimize(btAlignedObjectArray<btSoftBody *> &softBodies, bool forceUpdate)
42 {
43 m_softBodySet.copyFromArray(softBodies);
44 }
45
updateSoftBodies()46 void btDefaultSoftBodySolver::updateSoftBodies()
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
checkInitialized()58 bool btDefaultSoftBodySolver::checkInitialized()
59 {
60 return true;
61 }
62
solveConstraints(btScalar solverdt)63 void btDefaultSoftBodySolver::solveConstraints(btScalar solverdt)
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
copySoftBodyToVertexBuffer(const btSoftBody * const softBody,btVertexBufferDescriptor * vertexBuffer)76 void btDefaultSoftBodySolver::copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
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
83 if (vertexBuffer->getBufferType() == btVertexBufferDescriptor::CPU_BUFFER)
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
processCollision(btSoftBody * softBody,btSoftBody * otherSoftBody)124 void btDefaultSoftBodySolver::processCollision(btSoftBody *softBody, btSoftBody *otherSoftBody)
125 {
126 softBody->defaultCollisionHandler(otherSoftBody);
127 }
128
129 // For the default solver just leave the soft body to do its collision processing
processCollision(btSoftBody * softBody,const btCollisionObjectWrapper * collisionObjectWrap)130 void btDefaultSoftBodySolver::processCollision(btSoftBody *softBody, const btCollisionObjectWrapper *collisionObjectWrap)
131 {
132 softBody->defaultCollisionHandler(collisionObjectWrap);
133 } // btDefaultSoftBodySolver::processCollision
134
predictMotion(btScalar timeStep)135 void btDefaultSoftBodySolver::predictMotion(btScalar timeStep)
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 }
147