Panda3D

collisionSolid.h

00001 // Filename: collisionSolid.h
00002 // Created by:  drose (24Apr00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef COLLISIONSOLID_H
00016 #define COLLISIONSOLID_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "config_collide.h"
00021 #include "copyOnWriteObject.h"
00022 #include "luse.h"
00023 #include "pointerTo.h"
00024 #include "renderState.h"
00025 #include "geomNode.h"
00026 #include "lightMutex.h"
00027 #include "lightMutexHolder.h"
00028 #include "pStatCollector.h"
00029 
00030 class CollisionHandler;
00031 class CollisionEntry;
00032 class CollisionSphere;
00033 class GeomNode;
00034 class CollisionNode;
00035 class CullTraverserData;
00036 
00037 ////////////////////////////////////////////////////////////////////
00038 //       Class : CollisionSolid
00039 // Description : The abstract base class for all things that can
00040 //               collide with other things in the world, and all the
00041 //               things they can collide with (except geometry).
00042 //
00043 //               This class and its derivatives really work very
00044 //               similarly to the way BoundingVolume and all of its
00045 //               derivatives work.  There's a different subclass for
00046 //               each basic shape of solid, and double-dispatch
00047 //               function calls handle the subset of the N*N
00048 //               intersection tests that we care about.
00049 ////////////////////////////////////////////////////////////////////
00050 class EXPCL_PANDA_COLLIDE CollisionSolid : public CopyOnWriteObject {
00051 public:
00052   CollisionSolid();
00053   CollisionSolid(const CollisionSolid &copy);
00054   virtual ~CollisionSolid();
00055 
00056   virtual CollisionSolid *make_copy()=0;
00057 protected:
00058   virtual PT(CopyOnWriteObject) make_cow_copy();
00059 
00060 PUBLISHED:
00061   virtual LPoint3f get_collision_origin() const=0;
00062 
00063   INLINE void set_tangible(bool tangible);
00064   INLINE bool is_tangible() const;
00065 
00066   INLINE void set_effective_normal(const LVector3f &effective_normal);
00067   INLINE void clear_effective_normal();
00068   INLINE bool has_effective_normal() const;
00069   INLINE const LVector3f &get_effective_normal() const;
00070 
00071   INLINE void set_respect_effective_normal(bool respect_effective_normal);
00072   INLINE bool get_respect_effective_normal() const;
00073 
00074   CPT(BoundingVolume) get_bounds() const;
00075   void set_bounds(const BoundingVolume &bounding_volume);
00076   
00077 public:
00078   virtual PT(CollisionEntry)
00079   test_intersection(const CollisionEntry &entry) const;
00080 
00081   virtual void xform(const LMatrix4f &mat);
00082 
00083   virtual PT(PandaNode) get_viz(const CullTraverser *trav,
00084                                 const CullTraverserData &data,
00085                                 bool bounds_only) const;
00086 
00087   virtual PStatCollector &get_volume_pcollector();
00088   virtual PStatCollector &get_test_pcollector();
00089 
00090 PUBLISHED:
00091   virtual void output(ostream &out) const;
00092   virtual void write(ostream &out, int indent_level = 0) const;
00093 
00094 protected:
00095   INLINE bool do_is_tangible() const;
00096   INLINE bool do_has_effective_normal() const;
00097 
00098   INLINE void mark_internal_bounds_stale();
00099   virtual PT(BoundingVolume) compute_internal_bounds() const;
00100 
00101   virtual PT(CollisionEntry)
00102   test_intersection_from_ds_solid(const CollisionEntry &entry) const;
00103   virtual PT(CollisionEntry)
00104   test_intersection_from_sphere(const CollisionEntry &entry) const;
00105   virtual PT(CollisionEntry)
00106   test_intersection_from_line(const CollisionEntry &entry) const;
00107   virtual PT(CollisionEntry)
00108   test_intersection_from_ray(const CollisionEntry &entry) const;
00109   virtual PT(CollisionEntry)
00110   test_intersection_from_segment(const CollisionEntry &entry) const;
00111   virtual PT(CollisionEntry)
00112   test_intersection_from_parabola(const CollisionEntry &entry) const;
00113   virtual PT(CollisionEntry)
00114   test_intersection_from_box(const CollisionEntry &entry) const;
00115 
00116   static void report_undefined_intersection_test(TypeHandle from_type,
00117                                                  TypeHandle into_type);
00118   static void report_undefined_from_intersection(TypeHandle from_type);
00119 
00120   INLINE void mark_viz_stale();
00121   virtual void fill_viz_geom();
00122 
00123   CPT(RenderState) get_solid_viz_state();
00124   CPT(RenderState) get_wireframe_viz_state();
00125   CPT(RenderState) get_other_viz_state();
00126   CPT(RenderState) get_solid_bounds_viz_state();
00127   CPT(RenderState) get_wireframe_bounds_viz_state();
00128   CPT(RenderState) get_other_bounds_viz_state();
00129 
00130   PT(GeomNode) _viz_geom;
00131   PT(GeomNode) _bounds_viz_geom;
00132 
00133 private:
00134   LVector3f _effective_normal;
00135   PT(BoundingVolume) _internal_bounds;
00136 
00137   // Be careful reordering these bits, since they are written to a bam
00138   // file.
00139   enum Flags {
00140     F_tangible                  = 0x01,
00141     F_effective_normal          = 0x02,
00142     F_viz_geom_stale            = 0x04,
00143     F_ignore_effective_normal   = 0x08,
00144     F_internal_bounds_stale     = 0x10,
00145   };
00146   int _flags;
00147 
00148   LightMutex _lock;
00149 
00150   static PStatCollector _volume_pcollector;
00151   static PStatCollector _test_pcollector;
00152 
00153 public:
00154   virtual void write_datagram(BamWriter* manager, Datagram &me);
00155 
00156 protected:
00157   void fillin(DatagramIterator& scan, BamReader* manager);
00158 
00159 public:
00160   static TypeHandle get_class_type() {
00161     return _type_handle;
00162   }
00163   static void init_type() {
00164     CopyOnWriteObject::init_type();
00165     register_type(_type_handle, "CollisionSolid",
00166                   CopyOnWriteObject::get_class_type());
00167   }
00168   virtual TypeHandle get_type() const {
00169     return get_class_type();
00170   }
00171   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00172 
00173 private:
00174   static TypeHandle _type_handle;
00175 
00176   friend class CollisionDSSolid;
00177   friend class CollisionSphere;
00178   friend class CollisionLine;
00179   friend class CollisionRay;
00180   friend class CollisionSegment;
00181   friend class CollisionParabola;
00182   friend class CollisionHandlerFluidPusher;
00183   friend class CollisionBox;
00184 };
00185 
00186 INLINE ostream &operator << (ostream &out, const CollisionSolid &cs) {
00187   cs.output(out);
00188   return out;
00189 }
00190 
00191 #include "collisionSolid.I"
00192 
00193 #endif
00194 
 All Classes Functions Variables Enumerations