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 
00084 
00088 
00089 
00090 void getVectorFromEulerAngles(IN const point3d_t& euler,
00091                                 OUT point3d_t& vector) throw();
00092 
00093 
00096 void getVectorFromQuaternion(IN const quaternion_t& rotation,
00097                                 OUT point3d_t& vector) throw();
00098 
00099 
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 
00122 class PhysicsObject;
00123 
00125 typedef void (*object_iteration_fn)(IN smart_ptr<PhysicsObject>& obj,
00126                                 IN void * context);
00127 
00128 
00130 class PhysicsObject {
00131 public:
00132         // public enums --------------------------------------------------------
00133         enum eFlags {
00134                 eFlag_Ghost             = 0x0001, 
00135                 eFlag_Moveable          = 0x0002, 
00136                 eFlag_Events            = 0x0004, 
00137                 eFlag_NoCollisions      = 0x0008, 
00138                 eFlag_Remove            = 0x0010, 
00139                 eFlag_Static            = 0x0020, 
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 
00151         virtual dword_t getId(void) const throw() = 0;
00152 
00154         virtual int getFlags(void) const throw() = 0;
00155 
00157         virtual void dump(IN const char * txt) const throw() = 0;
00158 
00160         virtual point3d_t getPosition(void) const throw() = 0;
00161 
00163         virtual rect3d_t getBoundingBox(void) const throw() = 0;
00164 
00166         virtual quaternion_t getOrientation(void) const throw() = 0;
00167 
00169         virtual point3d_t getLinearVelocity(void) const throw() = 0;
00170 
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 
00179         virtual void requestMove(IN const point3d_t& delta,
00180                                 IN float dt) = 0;
00181 
00183         virtual void setPosition(IN const point3d_t& pos) = 0;
00184 
00186         virtual float getMass(void) const throw() = 0;
00187 
00189         virtual void iterateOverlappingObjects(IN object_iteration_fn fn,
00190                                 IN void * context) = 0;
00191 
00193         virtual void setUserPointer(void * userPointer) throw() = 0;
00194 
00196         virtual void * getUserPointer(void) const throw() = 0;
00197 };
00198 
00199 
00201 smart_ptr<PhysicsObject> getPhysicsObjectById(IN dword_t id);
00202 
00203 
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 
00229 smart_ptr<PhysicsObject> createObject(IN const physics_meta_t& meta,
00230                                 IN const placement_t& placement);
00231 
00232 
00233 
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 
00292 smart_ptr<PhysicsObject> getObjectHitFromPlacement(IN PhysicsWorld * world,
00293                                 IN const placement_t& placement,
00294                                 IN float half_extent);
00295 
00297 void dumpWorld(IN PhysicsWorld * world) throw();
00298 
00299 
00300 };      // aesop namespace
00301 
00302 #endif  // AESOP_PHYSICS_H__
00303