00001 /* **-*-c++-*-************************************************************** 00002 Copyright (C)1996 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: ODESolidSystem 1031 2004-02-11 20:46:36Z jungd $ 00019 $Revision: 1.12 $ 00020 $Date: 2004-02-11 15:46:36 -0500 (Wed, 11 Feb 2004) $ 00021 $Author: jungd $ 00022 00023 ****************************************************************************/ 00024 00025 #ifndef _PHYSICS_ODESOLIDSYSTEM_ 00026 #define _PHYSICS_ODESOLIDSYSTEM_ 00027 00028 #include <base/reflist> 00029 #include <physics/physics> 00030 #include <physics/SolidSystem> 00031 #include <physics/CollidableGroup> 00032 #include <physics/PotentialCollisionListener> 00033 00034 #include <ode/ode.h> 00035 00036 00037 namespace physics { 00038 00039 00040 class ODESolidSystem : public SolidSystem, public PotentialCollisionListener 00041 { 00042 public: 00043 ODESolidSystem(); 00044 ODESolidSystem(const ODESolidSystem& ss); 00045 virtual ~ODESolidSystem(); 00046 00047 virtual String className() const { return String("ODESolidSystem"); } 00048 00049 virtual ref<Solid> createSolid(ref<const Shape> shape, ref<const Material> material); ///< create a new Solid 00050 virtual ref<ConstraintGroup> createConstraintGroup(); ///< new ConstraintGroup 00051 00052 virtual ref<BallJoint> createBallJoint(); ///< new Ball & Socket joint constraint 00053 virtual ref<HingeJoint> createHingeJoint(); ///< new Hinge joint constraint 00054 virtual ref<DoubleHingeJoint> createDoubleHingeJoint(); ///< new Double Hinge joint constraint 00055 virtual ref<SliderJoint> createSliderJoint(); ///< new Slider joint constraint 00056 virtual ref<UniversalJoint> createUniversalJoint(); ///< new Universal joint constraint 00057 00058 virtual ref<FixedConstraint> createFixedConstraint(); ///< new FixedConstraint 00059 virtual ref<ContactConstraint> createContactConstraint();///< new ContactConstraint 00060 00061 virtual ref<Motor> createMotor(); ///< new Joint Motor 00062 00063 virtual void setGround(ref<Solid> ground, const Point3& position); ///< Set the ground Solid (which is fixed to the world frame and doesn't move from position) 00064 virtual ref<Solid> getGround() const; ///< Get the ground Solid 00065 virtual void setGravity(const Vector3& v); ///< set acceleration due to gravity 00066 00067 virtual void addSolid(ref<Solid> solid); ///< add solid to the system (Solid must be from createSolid() 00068 virtual void removeSolid(ref<const Solid> solid); ///< remove solid from the system 00069 00070 virtual void addConstraintGroup(ref<ConstraintGroup> group); ///< add a constraint group to the system 00071 virtual void removeConstraintGroup(ref<const ConstraintGroup> group); ///< remove constraint group from the system 00072 00073 virtual void setCollidable(ref<Collidable> collidable) { this->collidable = collidable; } 00074 00075 virtual void setCollisionCuller(ref<CollisionCuller> collisionCuller); ///< called to cull Collidables from collision detection 00076 virtual ref<CollisionCuller> getCollisionCuller() const; ///< get the current CollisionCuller 00077 00078 virtual void setCollisionDetector(ref<CollisionDetector> collisionDetector); ///< called to detect collisions 00079 virtual ref<CollisionDetector> getCollisionDetector() const; ///< get the current CollisionDetector (or null if none) 00080 00081 virtual void setCollisionResponseHandler(ref<CollisionResponseHandler> collisionResponseHandler); ///< called to handle collisions 00082 virtual ref<CollisionResponseHandler> getCollisionResponseHandler() const; 00083 00084 virtual void setParameter(const String& name, Real value); 00085 00086 // Simulatable 00087 virtual void preSimulate(); 00088 virtual void simulateForSimTime(const base::Time& dt); 00089 00090 // Visual 00091 virtual bool visualTypeSupported(VisualType type) const { return (type==OSGVisual); } 00092 virtual osg::Node* createOSGVisual(Visual::Attributes visualAttributes=0) const; 00093 00094 // CollidableProvider 00095 virtual ref<Collidable> createCollidable(CollidableFlags flags = 0); 00096 00097 // PotentialCollisionListener 00098 virtual void reset(); 00099 virtual void potentialCollision(ref<const Collidable> collidable1, ref<const Collidable> collidable2); 00100 00101 protected: 00102 bool active; ///< if false, physics simulation or collision detection is performed 00103 00104 dWorldID worldID; ///< ODE's World ID 00105 00106 typedef base::reflist<Solid> Solids; 00107 Solids solids; 00108 00109 typedef base::reflist<ConstraintGroup> ConstraintGroups; 00110 ConstraintGroups constraintGroups; 00111 00112 enum CollisionListenMode { HandleCollisions, RecordInterpenetrations }; 00113 CollisionListenMode collisionListenMode; 00114 bool initialPenetrations; 00115 typedef std::pair< ref<const Collidable>, ref<const Collidable> > CollidablePair; 00116 typedef array<CollidablePair> PenetratingPairs; 00117 PenetratingPairs initiallyPenetratingPairs; 00118 00119 ref<CollisionCuller> collisionCuller; 00120 ref<CollisionDetector> collisionDetector; 00121 ref<CollisionResponseHandler> collisionHandler; 00122 00123 ref<Collidable> collidable; 00124 00125 ref<Solid> ground; ///< the Solid ground (or 0) 00126 dJointID groundJoint; ///< fixed joint that connects the ground Solid to the world frame. 00127 00128 bool preSimulateCalled; 00129 00130 // for Visual 00131 mutable Visual::Attributes attributes; 00132 mutable ref_ptr<osg::Group> node; // ==0 => no Visual has been created 00133 00134 void updateVisual() const; 00135 }; 00136 00137 } // physics 00138 00139 00140 #endif