collisionBox.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Filename: collisionBox.h
  2. // Created by: amith tudur( 31Jul09 )
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #ifndef COLLISIONBOX_H
  15. #define COLLISIONBOX_H
  16. #include "pandabase.h"
  17. #include "collisionSolid.h"
  18. #include "parabola.h"
  19. #include "plane.h"
  20. #include "look_at.h"
  21. #include "clipPlaneAttrib.h"
  22. ////////////////////////////////////////////////////////////////////
  23. // Class : CollisionBox
  24. // Description : A cuboid collision volume or object.
  25. ////////////////////////////////////////////////////////////////////
  26. class EXPCL_PANDA_COLLIDE CollisionBox : public CollisionSolid {
  27. PUBLISHED:
  28. INLINE CollisionBox(const LPoint3 &center,
  29. PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
  30. INLINE CollisionBox(const LPoint3 &min, const LPoint3 &max);
  31. virtual LPoint3 get_collision_origin() const;
  32. protected:
  33. INLINE CollisionBox();
  34. public:
  35. INLINE CollisionBox(const CollisionBox &copy);
  36. virtual CollisionSolid *make_copy();
  37. virtual PT(CollisionEntry)
  38. test_intersection(const CollisionEntry &entry) const;
  39. virtual void xform(const LMatrix4 &mat);
  40. virtual PStatCollector &get_volume_pcollector();
  41. virtual PStatCollector &get_test_pcollector();
  42. virtual void output(ostream &out) const;
  43. virtual LPoint3 get_approx_center() const;
  44. virtual LPoint3 get_min() const;
  45. virtual LPoint3 get_max() const;
  46. INLINE static void flush_level();
  47. void setup_box();
  48. PUBLISHED:
  49. INLINE_MATHUTIL int get_num_points() const;
  50. INLINE_MATHUTIL LPoint3 get_point_aabb(int n) const;
  51. INLINE_MATHUTIL LPoint3 get_point(int n) const;
  52. INLINE_MATHUTIL int get_num_planes() const;
  53. INLINE_MATHUTIL LPlane set_plane(int n) const;
  54. INLINE_MATHUTIL LPlane get_plane(int n) const;
  55. INLINE void set_center(const LPoint3 &center);
  56. INLINE void set_center(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
  57. INLINE const LPoint3 &get_center() const;
  58. INLINE PN_stdfloat get_radius() const;
  59. protected:
  60. virtual PT(BoundingVolume) compute_internal_bounds() const;
  61. virtual PT(CollisionEntry)
  62. test_intersection_from_sphere(const CollisionEntry &entry) const;
  63. virtual PT(CollisionEntry)
  64. test_intersection_from_ray(const CollisionEntry &entry) const;
  65. virtual PT(CollisionEntry)
  66. test_intersection_from_segment(const CollisionEntry &entry) const;
  67. virtual PT(CollisionEntry)
  68. test_intersection_from_box(const CollisionEntry &entry) const;
  69. virtual void fill_viz_geom();
  70. private:
  71. LPoint3 _center;
  72. LPoint3 _min;
  73. LPoint3 _max;
  74. PN_stdfloat _x, _y, _z, _radius;
  75. LPoint3 _vertex[8]; // Each of the Eight Vertices of the Box
  76. LPlane _planes[6]; //Points to each of the six sides of the Box
  77. static const int plane_def[6][4];
  78. static PStatCollector _volume_pcollector;
  79. static PStatCollector _test_pcollector;
  80. private:
  81. INLINE static bool is_right(const LVector2 &v1, const LVector2 &v2);
  82. INLINE static PN_stdfloat dist_to_line(const LPoint2 &p,
  83. const LPoint2 &f, const LVector2 &v);
  84. static PN_stdfloat dist_to_line_segment(const LPoint2 &p,
  85. const LPoint2 &f, const LPoint2 &t,
  86. const LVector2 &v);
  87. public:
  88. class PointDef {
  89. public:
  90. INLINE PointDef(const LPoint2 &p, const LVector2 &v);
  91. INLINE PointDef(PN_stdfloat x, PN_stdfloat y);
  92. INLINE PointDef(const PointDef &copy);
  93. INLINE void operator = (const PointDef &copy);
  94. LPoint2 _p; // the point in 2-d space
  95. LVector2 _v; // the normalized vector to the next point
  96. };
  97. typedef pvector<PointDef> Points;
  98. static void compute_vectors(Points &points);
  99. void draw_polygon(GeomNode *viz_geom_node, GeomNode *bounds_viz_geom_node,
  100. const Points &points) const;
  101. bool point_is_inside(const LPoint2 &p, const Points &points) const;
  102. PN_stdfloat dist_to_polygon(const LPoint2 &p, const Points &points) const;
  103. void setup_points(const LPoint3 *begin, const LPoint3 *end, int plane);
  104. INLINE LPoint2 to_2d(const LVecBase3 &point3d, int plane) const;
  105. INLINE void calc_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const;
  106. INLINE void rederive_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const;
  107. INLINE static LPoint3 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat);
  108. LPoint3 legacy_to_3d(const LVecBase2 &point2d, int axis) const;
  109. bool clip_polygon(Points &new_points, const Points &source_points,
  110. const LPlane &plane,int plane_no) const;
  111. bool apply_clip_plane(Points &new_points, const ClipPlaneAttrib *cpa,
  112. const TransformState *net_transform, int plane_no) const;
  113. private:
  114. Points _points[6]; // one set of points for each of the six planes that make up the box
  115. LMatrix4 _to_2d_mat[6];
  116. public:
  117. INLINE Points get_plane_points( int n );
  118. public:
  119. static void register_with_read_factory();
  120. virtual void write_datagram(BamWriter *manager, Datagram &me);
  121. protected:
  122. static TypedWritable *make_CollisionBox(const FactoryParams &params);
  123. void fillin(DatagramIterator &scan, BamReader *manager);
  124. public:
  125. static TypeHandle get_class_type() {
  126. return _type_handle;
  127. }
  128. static void init_type() {
  129. CollisionSolid::init_type();
  130. register_type(_type_handle, "CollisionBox",
  131. CollisionSolid::get_class_type());
  132. }
  133. virtual TypeHandle get_type() const {
  134. return get_class_type();
  135. }
  136. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  137. private:
  138. static TypeHandle _type_handle;
  139. };
  140. #include "collisionBox.I"
  141. #endif /* COLLISIONBOX_H */