[1971] | 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 (Use it Freely, even Statically, but have to contribute any changes) |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | This program is free software; you can redistribute it and/or modify it under |
---|
| 12 | the terms of the GPL General Public License with runtime exception 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 GPL General Public License with runtime exception for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GPL General Public License with runtime exception 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/licenses/old-licenses/gpl-2.0.html |
---|
| 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 "OgreBulletDynamicsObjectState.h" |
---|
| 37 | |
---|
| 38 | using namespace Ogre; |
---|
| 39 | using namespace OgreBulletCollisions; |
---|
| 40 | |
---|
| 41 | namespace OgreBulletDynamics |
---|
| 42 | { |
---|
| 43 | |
---|
| 44 | // ------------------------------------------------------------------------- |
---|
| 45 | RigidBody::RigidBody(const String &name, DynamicsWorld *world, const short collisionGroup, const short collisionMask) |
---|
| 46 | : |
---|
| 47 | Object(name, world, false) |
---|
| 48 | { |
---|
| 49 | mCollisionGroup = collisionGroup; |
---|
| 50 | mCollisionMask = collisionMask; |
---|
| 51 | } |
---|
| 52 | // ------------------------------------------------------------------------- |
---|
| 53 | RigidBody::~RigidBody() |
---|
| 54 | { |
---|
| 55 | } |
---|
| 56 | // ------------------------------------------------------------------------- |
---|
| 57 | void RigidBody::setShape(Ogre::SceneNode *node, |
---|
| 58 | OgreBulletCollisions::CollisionShape *shape, |
---|
| 59 | const float bodyRestitution, |
---|
| 60 | const float bodyFriction, |
---|
| 61 | const float bodyMass, |
---|
| 62 | const Vector3 &pos, |
---|
| 63 | const Quaternion &quat) |
---|
| 64 | { |
---|
| 65 | |
---|
| 66 | mState = new ObjectState(this); |
---|
| 67 | |
---|
| 68 | mRootNode = node; |
---|
| 69 | mShapeNode = mRootNode->createChildSceneNode(mName + "Node"); |
---|
| 70 | mShapeNode->attachObject(this); |
---|
| 71 | |
---|
| 72 | node->setPosition (pos); |
---|
| 73 | node->setOrientation (quat); |
---|
| 74 | |
---|
| 75 | mShape = shape; |
---|
| 76 | showDebugShape(mWorld->getShowDebugShapes()); |
---|
| 77 | |
---|
| 78 | btVector3 localInertiaTensor = btVector3(0,0,0); |
---|
| 79 | if (bodyMass > 0.0) |
---|
| 80 | mShape->getBulletShape ()->calculateLocalInertia(bodyMass, localInertiaTensor); |
---|
| 81 | |
---|
| 82 | btRigidBody *body = new btRigidBody(bodyMass, mState, mShape->getBulletShape (), localInertiaTensor); |
---|
| 83 | body->setRestitution(bodyRestitution); |
---|
| 84 | body->setFriction(bodyFriction); |
---|
| 85 | |
---|
| 86 | mObject = body; |
---|
| 87 | |
---|
| 88 | getDynamicsWorld()->addRigidBody(this, mCollisionGroup, mCollisionMask); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | // ------------------------------------------------------------------------- |
---|
| 92 | void RigidBody::setStaticShape(Ogre::SceneNode *node, |
---|
| 93 | OgreBulletCollisions::CollisionShape *shape, |
---|
| 94 | const float bodyRestitution, |
---|
| 95 | const float bodyFriction, |
---|
| 96 | const Vector3 &pos, |
---|
| 97 | const Quaternion &quat) |
---|
| 98 | { |
---|
| 99 | mState = new ObjectState(this); |
---|
| 100 | |
---|
| 101 | mRootNode = node; |
---|
| 102 | |
---|
| 103 | mShapeNode = mRootNode->createChildSceneNode(mName + "Node"); |
---|
| 104 | mShapeNode->attachObject(this); |
---|
| 105 | |
---|
| 106 | node->setPosition (pos); |
---|
| 107 | node->setOrientation (quat); |
---|
| 108 | |
---|
| 109 | mShape = shape; |
---|
| 110 | showDebugShape(mWorld->getShowDebugShapes()); |
---|
| 111 | |
---|
| 112 | btRigidBody *body = new btRigidBody(0.0, mState, mShape->getBulletShape ()); |
---|
| 113 | |
---|
| 114 | body->setRestitution(bodyRestitution); |
---|
| 115 | body->setFriction(bodyFriction); |
---|
| 116 | |
---|
| 117 | body->getWorldTransform().setOrigin(btVector3(pos.x, pos.y, pos.z)); |
---|
| 118 | body->getWorldTransform().setRotation(btQuaternion(quat.x, quat.y, quat.z, quat.w)); |
---|
| 119 | |
---|
| 120 | mObject = body; |
---|
| 121 | getDynamicsWorld()->addRigidBody(this, mCollisionGroup, mCollisionMask); |
---|
| 122 | } |
---|
| 123 | // ------------------------------------------------------------------------- |
---|
| 124 | void RigidBody::setStaticShape(OgreBulletCollisions::CollisionShape *shape, |
---|
| 125 | const float bodyRestitution, |
---|
| 126 | const float bodyFriction, |
---|
| 127 | const Vector3 &pos, |
---|
| 128 | const Quaternion &quat) |
---|
| 129 | { |
---|
| 130 | //mState = new ObjectState(this); |
---|
| 131 | |
---|
| 132 | mShape = shape; |
---|
| 133 | btRigidBody *body = new btRigidBody(0.0, 0, mShape->getBulletShape ()); |
---|
| 134 | |
---|
| 135 | body->setRestitution(bodyRestitution); |
---|
| 136 | body->setFriction(bodyFriction); |
---|
| 137 | |
---|
| 138 | body->getWorldTransform().setOrigin(btVector3(pos.x, pos.y, pos.z)); |
---|
| 139 | body->getWorldTransform().setRotation(btQuaternion(quat.x, quat.y, quat.z, quat.w)); |
---|
| 140 | |
---|
| 141 | mObject = body; |
---|
| 142 | getDynamicsWorld()->addRigidBody(this, mCollisionGroup, mCollisionMask); |
---|
| 143 | } |
---|
| 144 | // ------------------------------------------------------------------------- |
---|
| 145 | void RigidBody::setKinematicObject(bool isKinematic) { |
---|
| 146 | if (this->isKinematicObject() != isKinematic) { |
---|
| 147 | //flip kinematic state |
---|
| 148 | getBulletRigidBody()->setCollisionFlags( |
---|
| 149 | getBulletRigidBody()->getCollisionFlags() ^ btCollisionObject::CF_KINEMATIC_OBJECT); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | // ------------------------------------------------------------------------- |
---|
| 153 | void RigidBody::setLinearVelocity( const Ogre::Vector3 &vel ) |
---|
| 154 | { |
---|
| 155 | getBulletRigidBody()->setLinearVelocity (btVector3(vel.x, vel.y, vel.z)); |
---|
| 156 | } |
---|
| 157 | // ------------------------------------------------------------------------- |
---|
| 158 | void RigidBody::setLinearVelocity( const Ogre::Real x, const Ogre::Real y, const Ogre::Real z ) |
---|
| 159 | { |
---|
| 160 | getBulletRigidBody()->setLinearVelocity (btVector3(x, y, z)); |
---|
| 161 | } |
---|
| 162 | // ------------------------------------------------------------------------- |
---|
| 163 | Ogre::Vector3 RigidBody::getLinearVelocity() |
---|
| 164 | { |
---|
| 165 | const btVector3 lv = getBulletRigidBody()->getLinearVelocity(); |
---|
| 166 | return BtOgreConverter::to(lv); |
---|
| 167 | } |
---|
| 168 | // ------------------------------------------------------------------------- |
---|
| 169 | void RigidBody::applyImpulse( const Ogre::Vector3 &impulse, const Ogre::Vector3 &position ) |
---|
| 170 | { |
---|
| 171 | getBulletRigidBody()->applyImpulse (OgreBtConverter::to(impulse), OgreBtConverter::to(position)); |
---|
| 172 | } |
---|
| 173 | // ------------------------------------------------------------------------- |
---|
| 174 | void RigidBody::applyForce( const Ogre::Vector3 &impulse, const Ogre::Vector3 &position ) |
---|
| 175 | { |
---|
| 176 | getBulletRigidBody()->applyForce(OgreBtConverter::to(impulse), OgreBtConverter::to(position)); |
---|
| 177 | } |
---|
| 178 | // ------------------------------------------------------------------------- |
---|
| 179 | Ogre::Vector3 RigidBody::getCenterOfMassPivot( const Ogre::Vector3 &pivotPosition ) const |
---|
| 180 | { |
---|
| 181 | const btVector3 centerOfMassPivot(getCenterOfMassTransform().inverse()* OgreBtConverter::to(pivotPosition)); |
---|
| 182 | return BtOgreConverter::to(centerOfMassPivot); |
---|
| 183 | } |
---|
| 184 | // ------------------------------------------------------------------------- |
---|
| 185 | void RigidBody::setDeactivationTime( const float ftime ) |
---|
| 186 | { |
---|
| 187 | getBulletRigidBody()->setDeactivationTime( ftime ); |
---|
| 188 | } |
---|
| 189 | // ------------------------------------------------------------------------- |
---|
| 190 | void RigidBody::setDamping( const Ogre::Real linearDamping, const Ogre::Real angularDamping ) |
---|
| 191 | { |
---|
| 192 | getBulletRigidBody()->setDamping( linearDamping, angularDamping); |
---|
| 193 | } |
---|
| 194 | // ------------------------------------------------------------------------- |
---|
| 195 | // ------------------------------------------------------------------------- |
---|
| 196 | // ------------------------------------------------------------------------- |
---|
| 197 | void WheeledRigidBody::setPosition(const btVector3 &pos) |
---|
| 198 | { |
---|
| 199 | //should update wheels as well ? |
---|
| 200 | mRootNode->setPosition(pos[0], pos[1], pos[2]); |
---|
| 201 | }; |
---|
| 202 | // ------------------------------------------------------------------------- |
---|
| 203 | void WheeledRigidBody::setOrientation(const btQuaternion &quat) |
---|
| 204 | { |
---|
| 205 | mRootNode->setOrientation(quat.getW(),quat.getX(), quat.getY(), quat.getZ()); |
---|
| 206 | }; |
---|
| 207 | // ------------------------------------------------------------------------- |
---|
| 208 | void WheeledRigidBody::setTransform(const btVector3 &pos, const btQuaternion &quat) |
---|
| 209 | { |
---|
| 210 | mRootNode->setPosition(pos[0], pos[1], pos[2]); |
---|
| 211 | mRootNode->setOrientation(quat.getW(),quat.getX(), quat.getY(), quat.getZ()); |
---|
| 212 | |
---|
| 213 | mVehicle->setTransform(); |
---|
| 214 | } |
---|
| 215 | // ------------------------------------------------------------------------- |
---|
| 216 | void WheeledRigidBody::setTransform(const btTransform& worldTrans) |
---|
| 217 | { |
---|
| 218 | mRootNode->setPosition(worldTrans.getOrigin()[0], worldTrans.getOrigin()[1],worldTrans.getOrigin()[2]); |
---|
| 219 | mRootNode->setOrientation(worldTrans.getRotation().getW(),worldTrans.getRotation().getX(), worldTrans.getRotation().getY(), worldTrans.getRotation().getZ()); |
---|
| 220 | |
---|
| 221 | mVehicle->setTransform(); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|