btDefaultSoftBodySolver.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h"
  14. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  15. #include "BulletCollision/CollisionShapes/btCollisionShape.h"
  16. #include "btDefaultSoftBodySolver.h"
  17. #include "BulletCollision/CollisionShapes/btCapsuleShape.h"
  18. #include "BulletSoftBody/btSoftBody.h"
  19. btDefaultSoftBodySolver::btDefaultSoftBodySolver()
  20. {
  21. // Initial we will clearly need to update solver constants
  22. // For now this is global for the cloths linked with this solver - we should probably make this body specific
  23. // for performance in future once we understand more clearly when constants need to be updated
  24. m_updateSolverConstants = true;
  25. }
  26. btDefaultSoftBodySolver::~btDefaultSoftBodySolver()
  27. {
  28. }
  29. // In this case the data is already in the soft bodies so there is no need for us to do anything
  30. void btDefaultSoftBodySolver::copyBackToSoftBodies(bool bMove)
  31. {
  32. }
  33. void btDefaultSoftBodySolver::optimize( btAlignedObjectArray< btSoftBody * > &softBodies , bool forceUpdate)
  34. {
  35. m_softBodySet.copyFromArray( softBodies );
  36. }
  37. void btDefaultSoftBodySolver::updateSoftBodies( )
  38. {
  39. for ( int i=0; i < m_softBodySet.size(); i++)
  40. {
  41. btSoftBody* psb=(btSoftBody*)m_softBodySet[i];
  42. if (psb->isActive())
  43. {
  44. psb->integrateMotion();
  45. }
  46. }
  47. } // updateSoftBodies
  48. bool btDefaultSoftBodySolver::checkInitialized()
  49. {
  50. return true;
  51. }
  52. void btDefaultSoftBodySolver::solveConstraints( float solverdt )
  53. {
  54. // Solve constraints for non-solver softbodies
  55. for(int i=0; i < m_softBodySet.size(); ++i)
  56. {
  57. btSoftBody* psb = static_cast<btSoftBody*>(m_softBodySet[i]);
  58. if (psb->isActive())
  59. {
  60. psb->solveConstraints();
  61. }
  62. }
  63. } // btDefaultSoftBodySolver::solveConstraints
  64. void btDefaultSoftBodySolver::copySoftBodyToVertexBuffer( const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer )
  65. {
  66. // Currently only support CPU output buffers
  67. // TODO: check for DX11 buffers. Take all offsets into the same DX11 buffer
  68. // and use them together on a single kernel call if possible by setting up a
  69. // per-cloth target buffer array for the copy kernel.
  70. if( vertexBuffer->getBufferType() == btVertexBufferDescriptor::CPU_BUFFER )
  71. {
  72. const btAlignedObjectArray<btSoftBody::Node> &clothVertices( softBody->m_nodes );
  73. int numVertices = clothVertices.size();
  74. const btCPUVertexBufferDescriptor *cpuVertexBuffer = static_cast< btCPUVertexBufferDescriptor* >(vertexBuffer);
  75. float *basePointer = cpuVertexBuffer->getBasePointer();
  76. if( vertexBuffer->hasVertexPositions() )
  77. {
  78. const int vertexOffset = cpuVertexBuffer->getVertexOffset();
  79. const int vertexStride = cpuVertexBuffer->getVertexStride();
  80. float *vertexPointer = basePointer + vertexOffset;
  81. for( int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex )
  82. {
  83. btVector3 position = clothVertices[vertexIndex].m_x;
  84. *(vertexPointer + 0) = (float)position.getX();
  85. *(vertexPointer + 1) = (float)position.getY();
  86. *(vertexPointer + 2) = (float)position.getZ();
  87. vertexPointer += vertexStride;
  88. }
  89. }
  90. if( vertexBuffer->hasNormals() )
  91. {
  92. const int normalOffset = cpuVertexBuffer->getNormalOffset();
  93. const int normalStride = cpuVertexBuffer->getNormalStride();
  94. float *normalPointer = basePointer + normalOffset;
  95. for( int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex )
  96. {
  97. btVector3 normal = clothVertices[vertexIndex].m_n;
  98. *(normalPointer + 0) = (float)normal.getX();
  99. *(normalPointer + 1) = (float)normal.getY();
  100. *(normalPointer + 2) = (float)normal.getZ();
  101. normalPointer += normalStride;
  102. }
  103. }
  104. }
  105. } // btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
  106. void btDefaultSoftBodySolver::processCollision( btSoftBody* softBody, btSoftBody* otherSoftBody)
  107. {
  108. softBody->defaultCollisionHandler( otherSoftBody);
  109. }
  110. // For the default solver just leave the soft body to do its collision processing
  111. void btDefaultSoftBodySolver::processCollision( btSoftBody *softBody, const btCollisionObjectWrapper* collisionObjectWrap )
  112. {
  113. softBody->defaultCollisionHandler( collisionObjectWrap );
  114. } // btDefaultSoftBodySolver::processCollision
  115. void btDefaultSoftBodySolver::predictMotion( float timeStep )
  116. {
  117. for ( int i=0; i < m_softBodySet.size(); ++i)
  118. {
  119. btSoftBody* psb = m_softBodySet[i];
  120. if (psb->isActive())
  121. {
  122. psb->predictMotion(timeStep);
  123. }
  124. }
  125. }