[1985] | 1 | /*************************************************************************** |
---|
| 2 | |
---|
| 3 | This source file is part of OGREBULLET |
---|
| 4 | (Object-oriented Graphics Rendering Engine Bullet Wrapper) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/phpBB2addons/viewforum.php?f=10 |
---|
| 6 | |
---|
| 7 | Copyright (c) 2007 tuan.kuranes@gmail.com |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | This program is free software; you can redistribute it and/or modify it under |
---|
| 12 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 13 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 14 | version. |
---|
| 15 | |
---|
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 18 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU Lesser General Public License along with |
---|
| 21 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 22 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 23 | http://www.gnu.org/copyleft/lesser.txt. |
---|
| 24 | ----------------------------------------------------------------------------- |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | #include "OgreBulletDynamics.h" |
---|
| 28 | |
---|
| 29 | #include "OgreBulletCollisionsShape.h" |
---|
| 30 | #include "OgreBulletCollisionsObject.h" |
---|
| 31 | #include "OgreBulletCollisionsWorld.h" |
---|
| 32 | #include "OgreBulletCollisionsObjectState.h" |
---|
| 33 | |
---|
| 34 | #include "OgreBulletDynamicsWorld.h" |
---|
| 35 | #include "OgreBulletDynamicsRigidBody.h" |
---|
| 36 | #include "Prefab/OgreBulletDynamicsRagDoll.h" |
---|
| 37 | |
---|
| 38 | using namespace Ogre; |
---|
| 39 | using namespace OgreBulletCollisions; |
---|
| 40 | |
---|
| 41 | namespace OgreBulletDynamics |
---|
| 42 | { |
---|
| 43 | |
---|
| 44 | // ------------------------------------------------------------------------- |
---|
| 45 | RagDoll::RagDoll (btDynamicsWorld* ownerWorld, const btVector3& positionOffset) |
---|
| 46 | : |
---|
| 47 | m_ownerWorld (ownerWorld) |
---|
| 48 | { |
---|
| 49 | // Setup all the rigid bodies |
---|
| 50 | btTransform offset; offset.setIdentity(); |
---|
| 51 | offset.setOrigin(positionOffset); |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | btTransform transform; |
---|
| 55 | |
---|
| 56 | // for |
---|
| 57 | { |
---|
| 58 | transform.setIdentity(); |
---|
| 59 | transform.setOrigin(btVector3(btScalar(0.), btScalar(1.), btScalar(0.))); |
---|
| 60 | m_bodies.push_back( |
---|
| 61 | localCreateRigidBody(btScalar(1.), |
---|
| 62 | offset*transform, |
---|
| 63 | new btCapsuleShape(btScalar(0.15), btScalar(0.20)) |
---|
| 64 | ) |
---|
| 65 | ); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | // Setup some damping on the m_bodies |
---|
| 69 | for(std::vector<btRigidBody* >::iterator i=m_bodies.begin(); |
---|
| 70 | i!=m_bodies.end(); |
---|
| 71 | ++i) |
---|
| 72 | { |
---|
| 73 | (*i)->setDamping(0.05, 0.85); |
---|
| 74 | (*i)->setDeactivationTime(0.8); |
---|
| 75 | (*i)->setSleepingThresholds(1.6, 2.5); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | // Now setup the constraints |
---|
| 79 | btHingeConstraint* hingeC; |
---|
| 80 | btConeTwistConstraint* coneC; |
---|
| 81 | btTransform localA, localB; |
---|
| 82 | |
---|
| 83 | // for |
---|
| 84 | { |
---|
| 85 | localA.setIdentity(); localB.setIdentity(); |
---|
| 86 | localA.getBasis().setEulerZYX(0, Ogre::Math::TWO_PI,0); |
---|
| 87 | localA.setOrigin(btVector3(btScalar(0.), btScalar(0.15), btScalar(0.))); |
---|
| 88 | localB.getBasis().setEulerZYX(0,Ogre::Math::TWO_PI,0); |
---|
| 89 | localB.setOrigin(btVector3(btScalar(0.), btScalar(-0.15), btScalar(0.))); |
---|
| 90 | hingeC = new btHingeConstraint(*m_bodies[0], *m_bodies[1], |
---|
| 91 | localA, localB); |
---|
| 92 | hingeC->setLimit(btScalar(-Ogre::Math::TWO_PI*2), btScalar(Ogre::Math::TWO_PI)); |
---|
| 93 | m_joints.push_back(hingeC); |
---|
| 94 | m_ownerWorld->addConstraint(hingeC, true); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | } |
---|
| 98 | // ------------------------------------------------------------------------- |
---|
| 99 | RagDoll::~RagDoll () |
---|
| 100 | { |
---|
| 101 | std::vector<btCollisionShape* > m_shapes; |
---|
| 102 | std::vector<btRigidBody* > m_bodies; |
---|
| 103 | std::vector<btTypedConstraint* > m_joints; |
---|
| 104 | // Remove all constraints |
---|
| 105 | for(std::vector<btTypedConstraint* >::iterator i=m_joints.begin(); |
---|
| 106 | i!=m_joints.end(); |
---|
| 107 | ++i) |
---|
| 108 | { |
---|
| 109 | m_ownerWorld->removeConstraint(*i); |
---|
| 110 | delete *i; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | // Remove all bodies and shapes |
---|
| 114 | for(std::vector<btRigidBody* >::iterator i=m_bodies.begin(); |
---|
| 115 | i!=m_bodies.end(); |
---|
| 116 | ++i) |
---|
| 117 | { |
---|
| 118 | m_ownerWorld->removeRigidBody(*i); |
---|
| 119 | |
---|
| 120 | delete (*i)->getMotionState(); |
---|
| 121 | delete *i; |
---|
| 122 | } |
---|
| 123 | for(std::vector<btCollisionShape* >::iterator i=m_shapes.begin(); |
---|
| 124 | i!=m_shapes.end(); |
---|
| 125 | ++i) |
---|
| 126 | { |
---|
| 127 | delete *i; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | // ------------------------------------------------------------------------- |
---|
| 131 | btRigidBody* RagDoll::localCreateRigidBody (btScalar mass, |
---|
| 132 | const btTransform& startTransform, |
---|
| 133 | btCollisionShape* shape) |
---|
| 134 | { |
---|
| 135 | bool isDynamic = (mass != 0.f); |
---|
| 136 | |
---|
| 137 | btVector3 localInertia(0,0,0); |
---|
| 138 | if (isDynamic) |
---|
| 139 | shape->calculateLocalInertia(mass,localInertia); |
---|
| 140 | |
---|
| 141 | btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); |
---|
| 142 | |
---|
| 143 | btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,shape,localInertia); |
---|
| 144 | btRigidBody* body = new btRigidBody(rbInfo); |
---|
| 145 | |
---|
| 146 | m_ownerWorld->addRigidBody(body); |
---|
| 147 | |
---|
| 148 | return body; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|