Panda3D
collisionBox.h
1 
2 // Filename: collisionBox.h
3 // Created by: amith tudur( 31Jul09 )
4 //
5 ////////////////////////////////////////////////////////////////////
6 //
7 // PANDA 3D SOFTWARE
8 // Copyright (c) Carnegie Mellon University. All rights reserved.
9 //
10 // All use of this software is subject to the terms of the revised BSD
11 // license. You should have received a copy of this license along
12 // with this source code in a file named "LICENSE."
13 //
14 ////////////////////////////////////////////////////////////////////
15 
16 #ifndef COLLISIONBOX_H
17 #define COLLISIONBOX_H
18 
19 #include "pandabase.h"
20 #include "collisionSolid.h"
21 #include "parabola.h"
22 #include "plane.h"
23 #include "look_at.h"
24 #include "clipPlaneAttrib.h"
25 
26 ////////////////////////////////////////////////////////////////////
27 // Class : CollisionBox
28 // Description : A cuboid collision volume or object.
29 ////////////////////////////////////////////////////////////////////
30 class EXPCL_PANDA_COLLIDE CollisionBox : public CollisionSolid {
31 PUBLISHED:
32  INLINE CollisionBox(const LPoint3 &center,
33  PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
34  INLINE CollisionBox(const LPoint3 &min, const LPoint3 &max);
35 
36  virtual LPoint3 get_collision_origin() const;
37 
38 protected:
39  INLINE CollisionBox();
40 
41 public:
42  INLINE CollisionBox(const CollisionBox &copy);
43  virtual CollisionSolid *make_copy();
44 
45  virtual PT(CollisionEntry)
46  test_intersection(const CollisionEntry &entry) const;
47  virtual void xform(const LMatrix4 &mat);
48 
49  virtual PStatCollector &get_volume_pcollector();
51 
52  virtual void output(ostream &out) const;
53 
54  virtual LPoint3 get_approx_center() const;
55  virtual LPoint3 get_min() const;
56  virtual LPoint3 get_max() const;
57 
58  INLINE static void flush_level();
59  void setup_box();
60 
61 PUBLISHED:
62  INLINE_MATHUTIL int get_num_points() const;
63  INLINE_MATHUTIL LPoint3 get_point_aabb(int n) const;
64  INLINE_MATHUTIL LPoint3 get_point(int n) const;
65  INLINE_MATHUTIL int get_num_planes() const;
66  INLINE_MATHUTIL LPlane set_plane(int n) const;
67  INLINE_MATHUTIL LPlane get_plane(int n) const;
68  INLINE void set_center(const LPoint3 &center);
69  INLINE void set_center(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
70  INLINE const LPoint3 &get_center() const;
71  INLINE PN_stdfloat get_radius() const;
72 
73 protected:
74  virtual PT(BoundingVolume) compute_internal_bounds() const;
75  virtual PT(CollisionEntry)
76  test_intersection_from_sphere(const CollisionEntry &entry) const;
77  virtual PT(CollisionEntry)
78  test_intersection_from_ray(const CollisionEntry &entry) const;
79  virtual PT(CollisionEntry)
80  test_intersection_from_segment(const CollisionEntry &entry) const;
81  virtual PT(CollisionEntry)
82  test_intersection_from_box(const CollisionEntry &entry) const;
83 
84  virtual void fill_viz_geom();
85 
86 private:
87  LPoint3 _center;
88  LPoint3 _min;
89  LPoint3 _max;
90  PN_stdfloat _x, _y, _z, _radius;
91  LPoint3 _vertex[8]; // Each of the Eight Vertices of the Box
92  LPlane _planes[6]; //Points to each of the six sides of the Box
93 
94  static const int plane_def[6][4];
95 
96  static PStatCollector _volume_pcollector;
97  static PStatCollector _test_pcollector;
98 
99 private:
100  INLINE static bool is_right(const LVector2 &v1, const LVector2 &v2);
101  INLINE static PN_stdfloat dist_to_line(const LPoint2 &p,
102  const LPoint2 &f, const LVector2 &v);
103  static PN_stdfloat dist_to_line_segment(const LPoint2 &p,
104  const LPoint2 &f, const LPoint2 &t,
105  const LVector2 &v);
106 
107 public:
108  class PointDef {
109  public:
110  INLINE PointDef(const LPoint2 &p, const LVector2 &v);
111  INLINE PointDef(PN_stdfloat x, PN_stdfloat y);
112  INLINE PointDef(const PointDef &copy);
113  INLINE void operator = (const PointDef &copy);
114 
115  LPoint2 _p; // the point in 2-d space
116  LVector2 _v; // the normalized vector to the next point
117  };
118  typedef pvector<PointDef> Points;
119 
120  static void compute_vectors(Points &points);
121  void draw_polygon(GeomNode *viz_geom_node, GeomNode *bounds_viz_geom_node,
122  const Points &points) const;
123 
124  bool point_is_inside(const LPoint2 &p, const Points &points) const;
125  PN_stdfloat dist_to_polygon(const LPoint2 &p, const Points &points) const;
126 
127  void setup_points(const LPoint3 *begin, const LPoint3 *end, int plane);
128  INLINE LPoint2 to_2d(const LVecBase3 &point3d, int plane) const;
129  INLINE void calc_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const;
130  INLINE void rederive_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const;
131  INLINE static LPoint3 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat);
132  LPoint3 legacy_to_3d(const LVecBase2 &point2d, int axis) const;
133  bool clip_polygon(Points &new_points, const Points &source_points,
134  const LPlane &plane,int plane_no) const;
135  bool apply_clip_plane(Points &new_points, const ClipPlaneAttrib *cpa,
136  const TransformState *net_transform, int plane_no) const;
137 
138 private:
139  Points _points[6]; // one set of points for each of the six planes that make up the box
140  LMatrix4 _to_2d_mat[6];
141 
142 public:
143  INLINE Points get_plane_points( int n );
144 
145 public:
146  static void register_with_read_factory();
147  virtual void write_datagram(BamWriter *manager, Datagram &me);
148 
149 protected:
150  static TypedWritable *make_CollisionBox(const FactoryParams &params);
151  void fillin(DatagramIterator &scan, BamReader *manager);
152 
153 public:
154  static TypeHandle get_class_type() {
155  return _type_handle;
156  }
157  static void init_type() {
158  CollisionSolid::init_type();
159  register_type(_type_handle, "CollisionBox",
160  CollisionSolid::get_class_type());
161  }
162  virtual TypeHandle get_type() const {
163  return get_class_type();
164  }
165  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
166 
167 private:
168  static TypeHandle _type_handle;
169 };
170 
171 #include "collisionBox.I"
172 
173 #endif /* COLLISIONBOX_H */
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
The abstract base class for all things that can collide with other things in the world, and all the things they can collide with (except geometry).
A cuboid collision volume or object.
Definition: collisionBox.h:30
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This functions similarly to a LightAttrib.
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
A lightweight class that represents a single element that may be timed and/or counted via stats...
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
Defines a single collision event.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
This is a two-component vector offset.
Definition: lvector2.h:91
virtual PStatCollector & get_test_pcollector()
Returns a PStatCollector that is used to count the number of intersection tests made against a solid ...
A class to retrieve the individual data elements previously stored in a Datagram. ...
This is a two-component point in space.
Definition: lpoint2.h:92
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:37