Home | History | Annotate | Download | only in BulletSoftBody
      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 
    152