diff options
author | Benoit Bolsee <benoit.bolsee@online.be> | 2009-11-25 01:44:29 +0300 |
---|---|---|
committer | Benoit Bolsee <benoit.bolsee@online.be> | 2009-11-25 01:44:29 +0300 |
commit | 5b722b1e8755c2d3b0c981087f3016f16a2b891a (patch) | |
tree | ece1d250a7e8cb72b9f393d394a1edb684dbf577 /source/gameengine/Converter/KX_SoftBodyDeformer.cpp | |
parent | 6c55047b4019a3491934bae38bbf229e6b0e6a48 (diff) |
BGE: replace mesh works for Soft Body (including reinstantiation of physics soft body mesh).
Even a static mesh can be used as replacement: the mesh
will be instantiated with the soft body settings of the
object. The position and orientation of the soft body
is preserved after the replacement.
Known limitation: the velocity of the soft body is reset
aftet the replacement. This is because soft body don't
have a well defined velocity.
Diffstat (limited to 'source/gameengine/Converter/KX_SoftBodyDeformer.cpp')
-rw-r--r-- | source/gameengine/Converter/KX_SoftBodyDeformer.cpp | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/source/gameengine/Converter/KX_SoftBodyDeformer.cpp b/source/gameengine/Converter/KX_SoftBodyDeformer.cpp new file mode 100644 index 00000000000..bee8751548a --- /dev/null +++ b/source/gameengine/Converter/KX_SoftBodyDeformer.cpp @@ -0,0 +1,120 @@ +/** + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifdef WIN32 +#pragma warning (disable : 4786) +#endif //WIN32 + +#include "MT_assert.h" + +#include "KX_ConvertPhysicsObject.h" +#include "KX_SoftBodyDeformer.h" +#include "RAS_MeshObject.h" +#include "GEN_Map.h" +#include "GEN_HashedPtr.h" + +#ifdef USE_BULLET + +#include "CcdPhysicsEnvironment.h" +#include "CcdPhysicsController.h" +#include "BulletSoftBody/btSoftBody.h" + +#include "KX_BulletPhysicsController.h" +#include "btBulletDynamicsCommon.h" + +void KX_SoftBodyDeformer::Relink(GEN_Map<class GEN_HashedPtr, void*>*map) +{ + void **h_obj = (*map)[m_gameobj]; + + if (h_obj) { + m_gameobj = (BL_DeformableGameObject*)(*h_obj); + m_pMeshObject = m_gameobj->GetMesh(0); + } else { + m_gameobj = NULL; + m_pMeshObject = NULL; + } +} + +bool KX_SoftBodyDeformer::Apply(class RAS_IPolyMaterial *polymat) +{ + KX_BulletPhysicsController* ctrl = (KX_BulletPhysicsController*) m_gameobj->GetPhysicsController(); + if (!ctrl) + return false; + + btSoftBody* softBody= ctrl->GetSoftBody(); + if (!softBody) + return false; + + //printf("apply\n"); + RAS_MeshSlot::iterator it; + RAS_MeshMaterial *mmat; + RAS_MeshSlot *slot; + size_t i; + + // update the vertex in m_transverts + Update(); + + // The vertex cache can only be updated for this deformer: + // Duplicated objects with more than one ploymaterial (=multiple mesh slot per object) + // share the same mesh (=the same cache). As the rendering is done per polymaterial + // cycling through the objects, the entire mesh cache cannot be updated in one shot. + mmat = m_pMeshObject->GetMeshMaterial(polymat); + if(!mmat->m_slots[(void*)m_gameobj]) + return true; + + slot = *mmat->m_slots[(void*)m_gameobj]; + + // for each array + for(slot->begin(it); !slot->end(it); slot->next(it)) + { + btSoftBody::tNodeArray& nodes(softBody->m_nodes); + + int index = 0; + for(i=it.startvertex; i<it.endvertex; i++,index++) { + RAS_TexVert& v = it.vertex[i]; + btAssert(v.getSoftBodyIndex() >= 0); + + MT_Point3 pt ( + nodes[v.getSoftBodyIndex()].m_x.getX(), + nodes[v.getSoftBodyIndex()].m_x.getY(), + nodes[v.getSoftBodyIndex()].m_x.getZ()); + v.SetXYZ(pt); + + MT_Vector3 normal ( + nodes[v.getSoftBodyIndex()].m_n.getX(), + nodes[v.getSoftBodyIndex()].m_n.getY(), + nodes[v.getSoftBodyIndex()].m_n.getZ()); + v.SetNormal(normal); + + } + } + return true; +} + +#endif |