00001 /* **-*-c++-*-************************************************************** 00002 Copyright (C)2003 David Jung <opensim@pobox.com> 00003 00004 This program/file is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. (http://www.gnu.org) 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 $Id: ODECollidableBody 1031 2004-02-11 20:46:36Z jungd $ 00019 $Revision: 1.1 $ 00020 $Date: 2004-02-11 15:46:36 -0500 (Wed, 11 Feb 2004) $ 00021 $Author: jungd $ 00022 00023 ****************************************************************************/ 00024 00025 #ifndef _PHYSICS_ODECOLLIDABLEBODY_ 00026 #define _PHYSICS_ODECOLLIDABLEBODY_ 00027 00028 #include <physics/physics> 00029 00030 #include <physics/CollidableBody> 00031 00032 #include <ode/ode.h> 00033 00034 00035 namespace physics { 00036 00037 00038 class ODECollidableBody : virtual public CollidableBody 00039 { 00040 public: 00041 ODECollidableBody(ref<const Shape> shape); 00042 virtual ~ODECollidableBody(); 00043 00044 virtual String className() const { return String("ODECollidableBody"); } 00045 00046 // DynamicSpatial 00047 virtual void setPosition(const Point3& x); 00048 virtual void setOrientation(const Orient& orient); 00049 virtual void setVelocity(const Vector3& v) { state->vel = v; } 00050 virtual void setAngVelocity(const Vector3& w) { state->angVel = w; } 00051 00052 virtual Point3 getPosition() const; 00053 virtual Orient getOrientation() const; 00054 virtual Vector3 getVelocity() const { return state->vel; } 00055 virtual Vector3 getAngVelocity() const { return state->angVel; } 00056 00057 virtual void saveState(); 00058 virtual void restoreState(); 00059 00060 virtual Point3 getSavedPosition() const { return state->savedPos; } 00061 virtual Orient getSavedOrientation() const { return state->savedOrient; } 00062 virtual Vector3 getSavedVelocity() const { return state->savedVel; } 00063 virtual Vector3 getSavedAngVelocity() const { return state->savedAngVel; } 00064 00065 virtual dGeomID getGeomID() const { return geomID; } 00066 00067 protected: 00068 ODECollidableBody() {} 00069 ODECollidableBody(ref<const Shape> shape, bool holdState) : CollidableBody(shape) { init(holdState); } 00070 ODECollidableBody(const ODECollidableBody& cb) 00071 : CollidableBody(cb) 00072 { 00073 init(false); 00074 if (cb.state) 00075 state = ref<BodyState>( dynamic_cast<BodyState*>(&cb.state->clone()) ); 00076 } 00077 00078 00079 dGeomID geomID; ///< ODE's geom ID (corresponding to shape - has pos/orient) 00080 void createGeom(); ///< create ODE geom from Shape 00081 00082 class BodyState : public base::ReferencedObject, public base::Cloneable 00083 { 00084 public: 00085 BodyState() {} 00086 BodyState(const BodyState& bs) 00087 : vel(bs.vel), angVel(bs.angVel), 00088 savedPos(bs.savedPos), savedOrient(bs.savedOrient), savedVel(bs.savedVel), savedAngVel(bs.savedAngVel) {} 00089 00090 virtual String className() const { return String("BodyState"); } 00091 virtual Object& clone() const { return *NewObj BodyState(*this); } 00092 00093 Vector3 vel; 00094 Vector3 angVel; 00095 00096 // saved state 00097 Point3 savedPos; 00098 Orient savedOrient; 00099 Vector3 savedVel; 00100 Vector3 savedAngVel; 00101 }; 00102 00103 ref<BodyState> state; 00104 00105 void init(bool holdState) 00106 { 00107 createGeom(); 00108 if (holdState) 00109 state = ref<BodyState>(NewObj BodyState()); 00110 } 00111 00112 }; 00113 00114 00115 00116 } // physics 00117 00118 #endif