Panda3D
 All Classes Functions Variables Enumerations
collisionBox.h
00001 
00002 // Filename: collisionBox.h
00003 // Created by:  amith tudur( 31Jul09 )
00004 //
00005 ////////////////////////////////////////////////////////////////////
00006 //
00007 // PANDA 3D SOFTWARE
00008 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00009 //
00010 // All use of this software is subject to the terms of the revised BSD
00011 // license.  You should have received a copy of this license along
00012 // with this source code in a file named "LICENSE."
00013 //
00014 ////////////////////////////////////////////////////////////////////
00015 
00016 #ifndef COLLISIONBOX_H
00017 #define COLLISIONBOX_H
00018 
00019 #include "pandabase.h"
00020 #include "collisionSolid.h"
00021 #include "parabola.h"
00022 #include "plane.h"
00023 #include "look_at.h"
00024 #include "clipPlaneAttrib.h"
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //       Class : CollisionBox
00028 // Description : A cuboid collision volume or object.
00029 ////////////////////////////////////////////////////////////////////
00030 class EXPCL_PANDA_COLLIDE CollisionBox : public CollisionSolid {
00031 PUBLISHED:
00032   INLINE CollisionBox(const LPoint3 &center, 
00033                       PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
00034   INLINE CollisionBox(const LPoint3 &min, const LPoint3 &max);
00035 
00036   virtual LPoint3 get_collision_origin() const;
00037 
00038 protected:
00039   INLINE CollisionBox();
00040 
00041 public:
00042   INLINE CollisionBox(const CollisionBox &copy);
00043   virtual CollisionSolid *make_copy();
00044 
00045   virtual PT(CollisionEntry)
00046     test_intersection(const CollisionEntry &entry) const;
00047   virtual void xform(const LMatrix4 &mat);
00048 
00049   virtual PStatCollector &get_volume_pcollector();
00050   virtual PStatCollector &get_test_pcollector();
00051 
00052   virtual void output(ostream &out) const;
00053   
00054   virtual LPoint3 get_approx_center() const;
00055   virtual LPoint3 get_min() const;
00056   virtual LPoint3 get_max() const;
00057 
00058   INLINE static void flush_level();
00059   void setup_box();
00060 
00061 PUBLISHED:
00062   INLINE_MATHUTIL int get_num_points() const;
00063   INLINE_MATHUTIL LPoint3 get_point_aabb(int n) const;
00064   INLINE_MATHUTIL LPoint3 get_point(int n) const;
00065   INLINE_MATHUTIL int get_num_planes() const;
00066   INLINE_MATHUTIL LPlane set_plane(int n) const;
00067   INLINE_MATHUTIL LPlane get_plane(int n) const;
00068   INLINE void set_center(const LPoint3 &center);
00069   INLINE void set_center(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
00070   INLINE const LPoint3 &get_center() const;
00071   INLINE PN_stdfloat get_radius() const;
00072 
00073 protected:
00074   virtual PT(BoundingVolume) compute_internal_bounds() const;
00075   virtual PT(CollisionEntry)
00076     test_intersection_from_sphere(const CollisionEntry &entry) const;
00077   virtual PT(CollisionEntry)
00078     test_intersection_from_ray(const CollisionEntry &entry) const;
00079   virtual PT(CollisionEntry)
00080     test_intersection_from_segment(const CollisionEntry &entry) const;
00081   virtual PT(CollisionEntry)
00082     test_intersection_from_box(const CollisionEntry &entry) const;
00083   
00084   virtual void fill_viz_geom();
00085 
00086 private:
00087   LPoint3 _center;
00088   LPoint3 _min;
00089   LPoint3 _max;
00090   PN_stdfloat _x, _y, _z, _radius;
00091   LPoint3 _vertex[8]; // Each of the Eight Vertices of the Box
00092   LPlane _planes[6]; //Points to each of the six sides of the Box
00093   
00094   static const int plane_def[6][4];
00095   
00096   static PStatCollector _volume_pcollector;
00097   static PStatCollector _test_pcollector;
00098 
00099 private:
00100   INLINE static bool is_right(const LVector2 &v1, const LVector2 &v2);
00101   INLINE static PN_stdfloat dist_to_line(const LPoint2 &p,
00102                                    const LPoint2 &f, const LVector2 &v);
00103   static PN_stdfloat dist_to_line_segment(const LPoint2 &p,
00104                                     const LPoint2 &f, const LPoint2 &t,
00105                                     const LVector2 &v);
00106   
00107 public:
00108   class PointDef {
00109   public:
00110     INLINE PointDef(const LPoint2 &p, const LVector2 &v);
00111     INLINE PointDef(PN_stdfloat x, PN_stdfloat y);
00112     INLINE PointDef(const PointDef &copy);
00113     INLINE void operator = (const PointDef &copy);
00114 
00115     LPoint2 _p;  // the point in 2-d space
00116     LVector2 _v; // the normalized vector to the next point
00117   };
00118   typedef pvector<PointDef> Points;
00119 
00120   static void compute_vectors(Points &points);
00121   void draw_polygon(GeomNode *viz_geom_node, GeomNode *bounds_viz_geom_node,
00122                     const Points &points) const;
00123 
00124   bool point_is_inside(const LPoint2 &p, const Points &points) const;
00125   PN_stdfloat dist_to_polygon(const LPoint2 &p, const Points &points) const;
00126 
00127   void setup_points(const LPoint3 *begin, const LPoint3 *end, int plane);
00128   INLINE LPoint2 to_2d(const LVecBase3 &point3d, int plane) const;
00129   INLINE void calc_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const;
00130   INLINE void rederive_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const;
00131   INLINE static LPoint3 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat);
00132   LPoint3 legacy_to_3d(const LVecBase2 &point2d, int axis) const;
00133   bool clip_polygon(Points &new_points, const Points &source_points,
00134                     const LPlane &plane,int plane_no) const;
00135   bool apply_clip_plane(Points &new_points, const ClipPlaneAttrib *cpa,
00136                         const TransformState *net_transform, int plane_no) const;
00137 
00138 private:
00139   Points _points[6]; // one set of points for each of the six planes that make up the box
00140   LMatrix4 _to_2d_mat[6]; 
00141 
00142 public:
00143   INLINE Points get_plane_points( int n );
00144 
00145 public:
00146   static void register_with_read_factory();
00147   virtual void write_datagram(BamWriter *manager, Datagram &me);
00148 
00149 protected:
00150   static TypedWritable *make_CollisionBox(const FactoryParams &params);
00151   void fillin(DatagramIterator &scan, BamReader *manager);
00152 
00153 public:
00154   static TypeHandle get_class_type() {
00155     return _type_handle;
00156   }
00157   static void init_type() {
00158     CollisionSolid::init_type();
00159     register_type(_type_handle, "CollisionBox",
00160                   CollisionSolid::get_class_type());
00161   }
00162   virtual TypeHandle get_type() const {
00163     return get_class_type();
00164   }
00165   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00166 
00167 private:
00168   static TypeHandle _type_handle;
00169 };
00170 
00171 #include "collisionBox.I"
00172 
00173 #endif /* COLLISIONBOX_H */
 All Classes Functions Variables Enumerations