svn merge -r 16396:16411 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / extern / bullet2 / src / BulletSoftBody / btSoftRigidDynamicsWorld.cpp
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
17 #include "btSoftRigidDynamicsWorld.h"
18 #include "LinearMath/btQuickprof.h"
19
20 //softbody & helpers
21 #include "btSoftBody.h"
22 #include "btSoftBodyHelpers.h"
23
24 btSoftRigidDynamicsWorld::btSoftRigidDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration)
25 :btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration)
26 {
27 m_drawFlags                     =       fDrawFlags::Std;
28 m_drawNodeTree          =       true;
29 m_drawFaceTree          =       false;
30 m_drawClusterTree       =       false;
31 }
32                 
33 btSoftRigidDynamicsWorld::~btSoftRigidDynamicsWorld()
34 {
35
36 }
37
38 void    btSoftRigidDynamicsWorld::predictUnconstraintMotion(btScalar timeStep)
39 {
40         btDiscreteDynamicsWorld::predictUnconstraintMotion( timeStep);
41
42         for ( int i=0;i<m_softBodies.size();++i)
43         {
44                 btSoftBody*     psb= m_softBodies[i];
45
46                 psb->predictMotion(timeStep);           
47         }
48 }
49                 
50 void    btSoftRigidDynamicsWorld::internalSingleStepSimulation( btScalar timeStep)
51 {
52         btDiscreteDynamicsWorld::internalSingleStepSimulation( timeStep );
53
54         ///solve soft bodies constraints
55         solveSoftBodiesConstraints();
56
57         ///update soft bodies
58         updateSoftBodies();
59
60 }
61
62 void    btSoftRigidDynamicsWorld::updateSoftBodies()
63 {
64         BT_PROFILE("updateSoftBodies");
65         
66         for ( int i=0;i<m_softBodies.size();i++)
67         {
68                 btSoftBody*     psb=(btSoftBody*)m_softBodies[i];
69                 psb->integrateMotion(); 
70         }
71 }
72
73 void    btSoftRigidDynamicsWorld::solveSoftBodiesConstraints()
74 {
75         BT_PROFILE("solveSoftConstraints");
76         
77         if(m_softBodies.size())
78                 {
79                 btSoftBody::solveClusters(m_softBodies);
80                 }
81         
82         for(int i=0;i<m_softBodies.size();++i)
83         {
84                 btSoftBody*     psb=(btSoftBody*)m_softBodies[i];
85                 psb->solveConstraints();
86         }       
87 }
88
89 void    btSoftRigidDynamicsWorld::addSoftBody(btSoftBody* body)
90 {
91         m_softBodies.push_back(body);
92
93         btCollisionWorld::addCollisionObject(body,
94                                         btBroadphaseProxy::DefaultFilter,
95                                         btBroadphaseProxy::AllFilter);
96
97 }
98
99 void    btSoftRigidDynamicsWorld::removeSoftBody(btSoftBody* body)
100 {
101         m_softBodies.remove(body);
102
103         btCollisionWorld::removeCollisionObject(body);
104 }
105
106 void    btSoftRigidDynamicsWorld::debugDrawWorld()
107 {
108         btDiscreteDynamicsWorld::debugDrawWorld();
109
110         if (getDebugDrawer())
111         {
112                 int i;
113                 for (  i=0;i<this->m_softBodies.size();i++)
114                 {
115                         btSoftBody*     psb=(btSoftBody*)this->m_softBodies[i];
116                         btSoftBodyHelpers::DrawFrame(psb,m_debugDrawer);
117                         btSoftBodyHelpers::Draw(psb,m_debugDrawer,m_drawFlags);
118                         if (m_debugDrawer && (m_debugDrawer->getDebugMode() & btIDebugDraw::DBG_DrawAabb))
119                         {
120                                 if(m_drawNodeTree)              btSoftBodyHelpers::DrawNodeTree(psb,m_debugDrawer);
121                                 if(m_drawFaceTree)              btSoftBodyHelpers::DrawFaceTree(psb,m_debugDrawer);
122                                 if(m_drawClusterTree)   btSoftBodyHelpers::DrawClusterTree(psb,m_debugDrawer);
123                         }
124                 }               
125         }       
126 }