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