Bullet Collision Detection & Physics Library: btDefaultSoftBodySolver.cpp Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
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
24
25 btDefaultSoftBodySolver::btDefaultSoftBodySolver()
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
30 m_updateSolverConstants = true;
31 }
32
33 btDefaultSoftBodySolver::~btDefaultSoftBodySolver()
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
38 void btDefaultSoftBodySolver::copyBackToSoftBodies(bool bMove)
39 {
40
41 }
42
43 void btDefaultSoftBodySolver::optimize( btAlignedObjectArray< btSoftBody * > &softBodies , bool forceUpdate)
44 {
45 m_softBodySet.copyFromArray( softBodies );
46 }
47
48 void btDefaultSoftBodySolver::updateSoftBodies( )
49 {
50 for ( int i=0; i < m_softBodySet.size(); i++)
51 {
52 btSoftBody* psb=(btSoftBody*)m_softBodySet[i];
53 if (psb->isActive())
54 {
55 psb->integrateMotion();
56 }
57 }
58 } // updateSoftBodies
59
60 bool btDefaultSoftBodySolver::checkInitialized()
61 {
62 return true;
63 }
64
65 void btDefaultSoftBodySolver::solveConstraints( float solverdt )
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
79 void btDefaultSoftBodySolver::copySoftBodyToVertexBuffer( const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer )
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
86 if( vertexBuffer->getBufferType() == btVertexBufferDescriptor::CPU_BUFFER )
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) = position.getX();
104 *(vertexPointer + 1) = position.getY();
105 *(vertexPointer + 2) = 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) = normal.getX();
119 *(normalPointer + 1) = normal.getY();
120 *(normalPointer + 2) = normal.getZ();
121 normalPointer += normalStride;
122 }
123 }
124 }
125 } // btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
126
127 void btDefaultSoftBodySolver::processCollision( btSoftBody* softBody, btSoftBody* otherSoftBody)
128 {
129 softBody->defaultCollisionHandler( otherSoftBody);
130 }
131
132 // For the default solver just leave the soft body to do its collision processing
133 void btDefaultSoftBodySolver::processCollision( btSoftBody *softBody, const btCollisionObjectWrapper* collisionObjectWrap )
134 {
135 softBody->defaultCollisionHandler( collisionObjectWrap );
136 } // btDefaultSoftBodySolver::processCollision
137
138
139 void btDefaultSoftBodySolver::predictMotion( float timeStep )
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::solveConstraints
virtual void solveConstraints(float solverdt)
Solve constraints for a set of soft bodies.
Definition: btDefaultSoftBodySolver.cpp:65
btDefaultSoftBodySolver::predictMotion
virtual void predictMotion(float solverdt)
Predict motion of soft bodies into next timestep.
Definition: btDefaultSoftBodySolver.cpp:139
btCapsuleShape.h
btVertexBufferDescriptor::hasVertexPositions
virtual bool hasVertexPositions() const
Definition: btSoftBodySolverVertexBuffer.h:57
btSoftBody::defaultCollisionHandler
void defaultCollisionHandler(const btCollisionObjectWrapper *pcoWrap)
Definition: btSoftBody.cpp:3153
btAlignedObjectArray< btSoftBody * >
btDefaultSoftBodySolver::btDefaultSoftBodySolver
btDefaultSoftBodySolver()
Definition: btDefaultSoftBodySolver.cpp:25
btSoftBody::integrateMotion
void integrateMotion()
Definition: btSoftBody.cpp:1988
btVertexBufferDescriptor::getBufferType
virtual BufferTypes getBufferType() const =0
Return the type of the vertex buffer descriptor.
btDefaultSoftBodySolver::m_updateSolverConstants
bool m_updateSolverConstants
Variable to define whether we need to update solver constants on the next iteration.
Definition: btDefaultSoftBodySolver.h:28
btDefaultSoftBodySolver.h
btTriangleIndexVertexArray.h
btAlignedObjectArray::copyFromArray
void copyFromArray(const btAlignedObjectArray &otherArray)
Definition: btAlignedObjectArray.h:494
btSoftBody::m_nodes
tNodeArray m_nodes
Definition: btSoftBody.h:656
btVector3::getZ
const btScalar & getZ() const
Return the z value.
Definition: btVector3.h:565
btDefaultSoftBodySolver::copyBackToSoftBodies
virtual void copyBackToSoftBodies(bool bMove=true)
Copy necessary data back to the original soft body source objects.
Definition: btDefaultSoftBodySolver.cpp:38
btVertexBufferDescriptor::CPU_BUFFER
Definition: btSoftBodySolverVertexBuffer.h:25
btSoftBody.h
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:149
btDefaultSoftBodySolver::optimize
virtual void optimize(btAlignedObjectArray< btSoftBody * > &softBodies, bool forceUpdate=false)
Optimize soft bodies in this solver.
Definition: btDefaultSoftBodySolver.cpp:43
btVector3::getY
const btScalar & getY() const
Return the y value.
Definition: btVector3.h:563
btSoftBody::solveConstraints
void solveConstraints()
Definition: btSoftBody.cpp:1847
btSoftBody::predictMotion
void predictMotion(btScalar dt)
Definition: btSoftBody.cpp:1739
btVector3::getX
const btScalar & getX() const
Return the x value.
Definition: btVector3.h:561
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
btCollisionShape.h
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::getVertexOffset
virtual int getVertexOffset() const
Return the vertex offset in floats from the base pointer.
Definition: btSoftBodySolverVertexBuffer.h:75
btDefaultSoftBodySolver::checkInitialized
virtual bool checkInitialized()
Ensure that this solver is initialized.
Definition: btDefaultSoftBodySolver.cpp:60
btDefaultSoftBodySolver::processCollision
virtual void processCollision(btSoftBody *, const btCollisionObjectWrapper *)
Definition: btDefaultSoftBodySolver.cpp:133
btVertexBufferDescriptor::getNormalOffset
virtual int getNormalOffset() const
Return the vertex offset in floats from the base pointer.
Definition: btSoftBodySolverVertexBuffer.h:91
btCPUVertexBufferDescriptor::getBasePointer
virtual float * getBasePointer() const
Return the base pointer in memory to the first vertex.
Definition: btSoftBodySolverVertexBuffer.h:159
btCollisionObject.h
btSoftBody
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition: btSoftBody.h:71
btCollisionObjectWrapper
Definition: btCollisionObjectWrapper.h:17
btCollisionObject::isActive
bool isActive() const
Definition: btCollisionObject.h:286
btVertexBufferDescriptor::hasNormals
virtual bool hasNormals() const
Definition: btSoftBodySolverVertexBuffer.h:62
btVertexBufferDescriptor::getNormalStride
virtual int getNormalStride() const
Return the vertex stride in number of floats between vertices.
Definition: btSoftBodySolverVertexBuffer.h:99
btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
virtual void copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
Definition: btDefaultSoftBodySolver.cpp:79
btDefaultSoftBodySolver::~btDefaultSoftBodySolver
virtual ~btDefaultSoftBodySolver()
Definition: btDefaultSoftBodySolver.cpp:33
btCPUVertexBufferDescriptor
Definition: btSoftBodySolverVertexBuffer.h:106
btVertexBufferDescriptor
Definition: btSoftBodySolverVertexBuffer.h:20