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