aesop-physics.h

Go to the documentation of this file.
00001 /*
00002  * aesop-physics.h
00003  *
00004  * Copyright (C) 2008,2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  */
00031 
00032 #ifndef AESOP_PHYSICS_H__
00033 #define AESOP_PHYSICS_H__
00034 
00035 // includes --------------------------------------------------------------------
00036 #include "common/common.h"
00037 
00038 #include "typeinst/typeinst.h"
00039 #include "geometry/geometry_3d.h"
00040 #include "geometry/quaternion.h"
00041 
00042 
00043 // forward declarations
00044 class Datahash;
00045 namespace hfield {
00046         class Heightfield;
00047 };      // hfield namespace
00048 namespace trimesh {
00049         class Trimesh;
00050 };      // trimesh namespace
00051 
00052 
00053 namespace aesop {
00054 
00055 ////////////////////////////////////////////////////////////////////////////////
00056 ///
00057 /// \ingroup aesop_physics_mgmt
00058 /// \defgroup aesop_physics AESOP Physics Library
00059 ///
00060 /// \n
00061 /// This is the core physics engine used by the server.
00062 ///
00063 /// \n
00064 /// Note that this library is not intended to contain any actual physics code!
00065 /// That is hard.  This library is just to provide a simple interface on top
00066 /// of the very powerful Bullet Physics API.
00067 /// See http://www.bulletphysics.com/
00068 ///
00069 /// \n
00070 /// One intention of this layer is to insulate other code from knowing details
00071 /// of the physics engine implementation.
00072 ///
00073 /// Some concepts that are safe to expose:
00074 ///  - shapes
00075 ///  - objects (instances of shapes)
00076 ///  - axis-aligned bounding boxes (AABB), using basic geometric types
00077 ///  - 3D transforms
00078 ///
00079 /// Concepts that are NOT safe to expose:
00080 ///  - any bullet-specific types
00081 ///  - class hierarchies of shapes and objects (only expose base classes here)
00082 ///
00083 ////////////////////////////////////////////////////////////////////////////////
00084 
00085 /*@{*/
00086 
00087 
00088 /// Assuming a vector is initially pointing in the (0, 0, 1) direction,
00089 ///   return how it is rotated by the given Euler angles.
00090 void getVectorFromEulerAngles(IN const point3d_t& euler,
00091                                 OUT point3d_t& vector) throw();
00092 
00093 
00094 /// Assuming a vector is initially pointing in the (0, 0, 1) direction,
00095 ///   return how it is rotated by the given quaternion.
00096 void getVectorFromQuaternion(IN const quaternion_t& rotation,
00097                                 OUT point3d_t& vector) throw();
00098 
00099 
00100 /// a shape is a 3D template which can be used to create instances of objects
00101 class PhysicsShape {
00102 public:
00103         // virtual destructor --------------------------------------------------
00104         virtual ~PhysicsShape(void) throw();
00105 
00106         // aesop::PhysicsShape class interface methods -------------------------
00107         virtual void setContext(IN void * ctx) throw() = 0;
00108         virtual void * getContext(void) throw() = 0;
00109 };
00110 
00111 
00112 // primitives to create basic collision shapes
00113 smart_ptr<PhysicsShape> createBoxShape(IN const point3d_t& dimensions);
00114 smart_ptr<PhysicsShape> createCubeShape(IN float edge_length);
00115 smart_ptr<PhysicsShape> createCapsuleShape(IN float height, IN float radius);
00116 smart_ptr<PhysicsShape> createTrimeshShape(IN const trimesh::Trimesh * trimesh);
00117 smart_ptr<PhysicsShape> createHeightfieldShape(IN const hfield::Heightfield * field);
00118 
00119 
00120 
00121 /// forward declaration
00122 class PhysicsObject;
00123 
00124 /// callback definition
00125 typedef void (*object_iteration_fn)(IN smart_ptr<PhysicsObject>& obj,
00126                                 IN void * context);
00127 
00128 
00129 /// an instance of a shape (has position, velocity, etc)
00130 class PhysicsObject {
00131 public:
00132         // public enums --------------------------------------------------------
00133         enum eFlags {
00134                 eFlag_Ghost             = 0x0001, ///< ghost object
00135                 eFlag_Moveable          = 0x0002, ///< client can requestMove()
00136                 eFlag_Events            = 0x0004, ///< receive collision events
00137                 eFlag_NoCollisions      = 0x0008, ///< does not collide
00138                 eFlag_Remove            = 0x0010, ///< will soon be removed!
00139                 eFlag_Static            = 0x0020, ///< this is a static object
00140 
00141                 // keep this last!
00142                 eFlag_Invalid           = 0x8000
00143         };
00144 
00145         // virtual destructor --------------------------------------------------
00146         virtual ~PhysicsObject(void) throw();
00147 
00148         // aesop::PhysicsObject class interface methods ------------------------
00149 
00150         /// every physics object has a unique ID
00151         virtual dword_t getId(void) const throw() = 0;
00152 
00153         /// object flags
00154         virtual int getFlags(void) const throw() = 0;
00155 
00156         /// debug information (writes a chunk of information to stderr)
00157         virtual void dump(IN const char * txt) const throw() = 0;
00158 
00159         /// object's current position in world (== map)
00160         virtual point3d_t getPosition(void) const throw() = 0;
00161 
00162         /// object's axis-aligned bounding box
00163         virtual rect3d_t getBoundingBox(void) const throw() = 0;
00164 
00165         /// object's current orientation (in Euler angles)
00166         virtual quaternion_t getOrientation(void) const throw() = 0;
00167 
00168         /// object's current linear velocity
00169         virtual point3d_t getLinearVelocity(void) const throw() = 0;
00170 
00171         /// object's current angular velocity
00172         virtual point3d_t getAngularVelocity(void) const throw() = 0;
00173 
00174         virtual void setOrientation(IN const quaternion_t& orient) = 0;
00175         virtual void setLinearVelocity(IN const point3d_t& linear) = 0;
00176         virtual void setAngularVelocity(IN const point3d_t& angular) = 0;
00177 
00178         /// can request motion in a particular direction over a time interval
00179         virtual void requestMove(IN const point3d_t& delta,
00180                                 IN float dt) = 0;
00181 
00182         /// jump to a new location, regardless of collisions
00183         virtual void setPosition(IN const point3d_t& pos) = 0;
00184 
00185         /// get mass (zero means infinite--immobile)
00186         virtual float getMass(void) const throw() = 0;
00187 
00188         /// iterate over all other overlapping objects
00189         virtual void iterateOverlappingObjects(IN object_iteration_fn fn,
00190                                 IN void * context) = 0;
00191 
00192         /// set a context pointer
00193         virtual void setUserPointer(void * userPointer) throw() = 0;
00194 
00195         /// retrieve user pointer
00196         virtual void * getUserPointer(void) const throw() = 0;
00197 };
00198 
00199 
00200 /// access to static registry of physics objects
00201 smart_ptr<PhysicsObject> getPhysicsObjectById(IN dword_t id);
00202 
00203 
00204 /// basic information to create an instance of a shape (== object)
00205 struct physics_meta_t {
00206         // constructor, manipulator
00207         physics_meta_t(void) throw() { this->clear(); }
00208         void clear(void) throw() {
00209                         shape = NULL;
00210                         mass = 0;
00211                         objectFlags = 0;
00212                 }
00213         void dump(IN const char * msg) const throw() {
00214                         DPRINTF("%s: physics_meta_t", msg);
00215                         DPRINTF("      shape: %p", (const PhysicsShape *) shape);
00216                         DPRINTF("      mass: %f", mass);
00217                         DPRINTF("      flags: 0x%08x", objectFlags);
00218                 }
00219 
00220         // data fields
00221         smart_ptr<PhysicsShape>         shape;
00222         float                           mass;
00223         int                             objectFlags;
00224 };
00225 
00226 
00227 
00228 /// create an object from a physics meta and instance information
00229 smart_ptr<PhysicsObject> createObject(IN const physics_meta_t& meta,
00230                                 IN const placement_t& placement);
00231 
00232 
00233 
00234 /// create one of these to support physics simulations etc.
00235 /// This is a threadsafe object with two supported iterators: one for
00236 /// objects in the world, and one for collisions.
00237 class PhysicsWorld {
00238 public:
00239         // public typedefs -----------------------------------------------------
00240         struct object_iterator_t {
00241         private:
00242                 byte_t          opaque[6 * sizeof(long)];
00243         };
00244 
00245         struct collision_iterator_t {
00246         private:
00247                 byte_t          opaque[4 * sizeof(long)];
00248         };
00249 
00250         struct collision_record_t {
00251                 collision_record_t(void) throw() { this->clear(); }
00252                 void clear(void) throw() {
00253                                 obj1 = NULL;
00254                                 obj2 = NULL;
00255                         }
00256 
00257                 // data fields
00258                 smart_ptr<PhysicsObject>        obj1;
00259                 smart_ptr<PhysicsObject>        obj2;
00260         };
00261 
00262         // virtual destructor --------------------------------------------------
00263         virtual ~PhysicsWorld(void) throw();
00264 
00265         // aesop::PhysicsWorld class interface methods -------------------------
00266         virtual void addObject(IN smart_ptr<PhysicsObject>& o) = 0;
00267         virtual void removeObject(IN PhysicsObject * obj) = 0;
00268         virtual long getObjectCount(void) throw() = 0;
00269         virtual void tick(IN float seconds) = 0;
00270 
00271         virtual void getObjectIterator(OUT object_iterator_t& i) = 0;
00272         virtual bool getNextObject(IO object_iterator_t& i,
00273                                 OUT smart_ptr<PhysicsObject>& obj) = 0;
00274 
00275         virtual void getCollisionIterator(OUT collision_iterator_t& i) = 0;
00276         virtual bool getNextCollision(IO collision_iterator_t& i,
00277                                 OUT collision_record_t& cr) = 0;
00278 
00279         virtual smart_ptr<PhysicsObject> rayTest(IN const point3d_t& from,
00280                                 IN const point3d_t& to) = 0;
00281 
00282         // static factory methods ----------------------------------------------
00283         static smart_ptr<PhysicsWorld> create(IN const Datahash * params);
00284 };
00285 
00286 
00287 
00288 // primitives + debugging
00289 
00290 /// given an object at the given location and view direction (orientation), what
00291 ///     is it looking at?  (First object hit by a ray cast)  Can return null!
00292 smart_ptr<PhysicsObject> getObjectHitFromPlacement(IN PhysicsWorld * world,
00293                                 IN const placement_t& placement,
00294                                 IN float half_extent);
00295 
00296 /// expensive debugging function
00297 void dumpWorld(IN PhysicsWorld * world) throw();
00298 
00299 
00300 };      // aesop namespace
00301 
00302 #endif  // AESOP_PHYSICS_H__
00303