btDefaultSoftBodySolver.cpp

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #include "BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h"
00017 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
00018 #include "BulletCollision/CollisionShapes/btCollisionShape.h"
00019 
00020 #include "btDefaultSoftBodySolver.h"
00021 #include "BulletCollision/CollisionShapes/btCapsuleShape.h"
00022 #include "BulletSoftBody/btSoftBody.h"
00023 
00024 
00025 btDefaultSoftBodySolver::btDefaultSoftBodySolver()
00026 {
00027         // Initial we will clearly need to update solver constants
00028         // For now this is global for the cloths linked with this solver - we should probably make this body specific 
00029         // for performance in future once we understand more clearly when constants need to be updated
00030         m_updateSolverConstants = true;
00031 }
00032 
00033 btDefaultSoftBodySolver::~btDefaultSoftBodySolver()
00034 {
00035 }
00036 
00037 // In this case the data is already in the soft bodies so there is no need for us to do anything
00038 void btDefaultSoftBodySolver::copyBackToSoftBodies(bool bMove)
00039 {
00040 
00041 }
00042 
00043 void btDefaultSoftBodySolver::optimize( btAlignedObjectArray< btSoftBody * > &softBodies , bool forceUpdate)
00044 {
00045         m_softBodySet.copyFromArray( softBodies );
00046 }
00047 
00048 void btDefaultSoftBodySolver::updateSoftBodies( )
00049 {
00050         for ( int i=0; i < m_softBodySet.size(); i++)
00051         {
00052                 btSoftBody*     psb=(btSoftBody*)m_softBodySet[i];
00053                 if (psb->isActive())
00054                 {
00055                         psb->integrateMotion(); 
00056                 }
00057         }
00058 } // updateSoftBodies
00059 
00060 bool btDefaultSoftBodySolver::checkInitialized()
00061 {
00062         return true;
00063 }
00064 
00065 void btDefaultSoftBodySolver::solveConstraints( float solverdt )
00066 {
00067         // Solve constraints for non-solver softbodies
00068         for(int i=0; i < m_softBodySet.size(); ++i)
00069         {
00070                 btSoftBody*     psb = static_cast<btSoftBody*>(m_softBodySet[i]);
00071                 if (psb->isActive())
00072                 {
00073                         psb->solveConstraints();
00074                 }
00075         }       
00076 } // btDefaultSoftBodySolver::solveConstraints
00077 
00078 
00079 void btDefaultSoftBodySolver::copySoftBodyToVertexBuffer( const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer )
00080 {
00081         // Currently only support CPU output buffers
00082         // TODO: check for DX11 buffers. Take all offsets into the same DX11 buffer
00083         // and use them together on a single kernel call if possible by setting up a
00084         // per-cloth target buffer array for the copy kernel.
00085 
00086         if( vertexBuffer->getBufferType() == btVertexBufferDescriptor::CPU_BUFFER )
00087         {
00088                 const btAlignedObjectArray<btSoftBody::Node> &clothVertices( softBody->m_nodes );
00089                 int numVertices = clothVertices.size();
00090 
00091                 const btCPUVertexBufferDescriptor *cpuVertexBuffer = static_cast< btCPUVertexBufferDescriptor* >(vertexBuffer);                                         
00092                 float *basePointer = cpuVertexBuffer->getBasePointer();                                         
00093 
00094                 if( vertexBuffer->hasVertexPositions() )
00095                 {
00096                         const int vertexOffset = cpuVertexBuffer->getVertexOffset();
00097                         const int vertexStride = cpuVertexBuffer->getVertexStride();
00098                         float *vertexPointer = basePointer + vertexOffset;
00099 
00100                         for( int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex )
00101                         {
00102                                 btVector3 position = clothVertices[vertexIndex].m_x;
00103                                 *(vertexPointer + 0) = position.getX();
00104                                 *(vertexPointer + 1) = position.getY();
00105                                 *(vertexPointer + 2) = position.getZ();
00106                                 vertexPointer += vertexStride;
00107                         }
00108                 }
00109                 if( vertexBuffer->hasNormals() )
00110                 {
00111                         const int normalOffset = cpuVertexBuffer->getNormalOffset();
00112                         const int normalStride = cpuVertexBuffer->getNormalStride();
00113                         float *normalPointer = basePointer + normalOffset;
00114 
00115                         for( int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex )
00116                         {
00117                                 btVector3 normal = clothVertices[vertexIndex].m_n;
00118                                 *(normalPointer + 0) = normal.getX();
00119                                 *(normalPointer + 1) = normal.getY();
00120                                 *(normalPointer + 2) = normal.getZ();
00121                                 normalPointer += normalStride;
00122                         }
00123                 }
00124         }
00125 } // btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
00126 
00127 void btDefaultSoftBodySolver::processCollision( btSoftBody* softBody, btSoftBody* otherSoftBody)
00128 {
00129         softBody->defaultCollisionHandler( otherSoftBody);
00130 }
00131 
00132 // For the default solver just leave the soft body to do its collision processing
00133 void btDefaultSoftBodySolver::processCollision( btSoftBody *softBody, const btCollisionObjectWrapper* collisionObjectWrap )
00134 {
00135         softBody->defaultCollisionHandler( collisionObjectWrap );
00136 } // btDefaultSoftBodySolver::processCollision
00137 
00138 
00139 void btDefaultSoftBodySolver::predictMotion( float timeStep )
00140 {
00141         for ( int i=0; i < m_softBodySet.size(); ++i)
00142         {
00143                 btSoftBody*     psb = m_softBodySet[i];
00144 
00145                 if (psb->isActive())
00146                 {
00147                         psb->predictMotion(timeStep);           
00148                 }
00149         }
00150 }
00151