Panda3D
collisionCapsule.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 collisionCapsule.h
10  * @author drose
11  * @date 2003-09-25
12  */
13 
14 #ifndef COLLISIONCAPSULE_H
15 #define COLLISIONCAPSULE_H
16 
17 #include "pandabase.h"
18 #include "collisionSolid.h"
19 #include "parabola.h"
20 
21 /**
22  * This implements a solid consisting of a cylinder with hemispherical endcaps,
23  * also known as a capsule or a spherocylinder.
24  *
25  * This shape was previously erroneously called CollisionTube.
26  */
27 class EXPCL_PANDA_COLLIDE CollisionCapsule : public CollisionSolid {
28 PUBLISHED:
29  INLINE explicit CollisionCapsule(const LPoint3 &a, const LPoint3 &db,
30  PN_stdfloat radius);
31  INLINE explicit CollisionCapsule(PN_stdfloat ax, PN_stdfloat ay, PN_stdfloat az,
32  PN_stdfloat bx, PN_stdfloat by, PN_stdfloat bz,
33  PN_stdfloat radius);
34 
35  virtual LPoint3 get_collision_origin() const;
36 
37 private:
38  INLINE CollisionCapsule();
39 
40 public:
41  INLINE CollisionCapsule(const CollisionCapsule &copy);
42  virtual CollisionSolid *make_copy();
43 
44  virtual PT(CollisionEntry)
45  test_intersection(const CollisionEntry &entry) const;
46 
47  virtual void xform(const LMatrix4 &mat);
48 
49  virtual PStatCollector &get_volume_pcollector();
51 
52  virtual void output(std::ostream &out) const;
53 
54  INLINE static void flush_level();
55 
56 PUBLISHED:
57  INLINE void set_point_a(const LPoint3 &a);
58  INLINE void set_point_a(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
59  INLINE const LPoint3 &get_point_a() const;
60 
61  INLINE void set_point_b(const LPoint3 &b);
62  INLINE void set_point_b(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
63  INLINE const LPoint3 &get_point_b() const;
64 
65  INLINE void set_radius(PN_stdfloat radius);
66  INLINE PN_stdfloat get_radius() const;
67 
68 PUBLISHED:
69  MAKE_PROPERTY(point_a, get_point_a, set_point_a);
70  MAKE_PROPERTY(point_b, get_point_b, set_point_b);
71  MAKE_PROPERTY(radius, get_radius, set_radius);
72 
73 protected:
74  virtual PT(BoundingVolume) compute_internal_bounds() const;
75 
76 protected:
77  virtual PT(CollisionEntry)
78  test_intersection_from_sphere(const CollisionEntry &entry) const;
79  virtual PT(CollisionEntry)
80  test_intersection_from_line(const CollisionEntry &entry) const;
81  virtual PT(CollisionEntry)
82  test_intersection_from_ray(const CollisionEntry &entry) const;
83  virtual PT(CollisionEntry)
84  test_intersection_from_segment(const CollisionEntry &entry) const;
85  virtual PT(CollisionEntry)
86  test_intersection_from_capsule(const CollisionEntry &entry) const;
87  virtual PT(CollisionEntry)
88  test_intersection_from_parabola(const CollisionEntry &entry) const;
89 
90  virtual void fill_viz_geom();
91 
92 private:
93  void recalc_internals();
94 
95  LVertex calc_sphere1_vertex(int ri, int si, int num_rings, int num_slices);
96  LVertex calc_sphere2_vertex(int ri, int si, int num_rings, int num_slices,
97  PN_stdfloat length);
98 
99  static void calc_closest_segment_points(double &t1, double &t2,
100  const LPoint3 &from1, const LVector3 &delta1,
101  const LPoint3 &from2, const LVector3 &delta2);
102  bool intersects_line(double &t1, double &t2,
103  const LPoint3 &from, const LVector3 &delta,
104  PN_stdfloat inflate_radius) const;
105  static bool sphere_intersects_line(double &t1, double &t2, PN_stdfloat center_y,
106  const LPoint3 &from, const LVector3 &delta,
107  PN_stdfloat radius);
108  bool intersects_parabola(double &t, const LParabola &parabola,
109  double t1, double t2,
110  const LPoint3 &p1, const LPoint3 &p2) const;
111  void calculate_surface_point_and_normal(const LPoint3 &surface_point,
112  double extra_radius,
113  LPoint3 &result_point,
114  LVector3 &result_normal) const;
115  void set_intersection_point(CollisionEntry *new_entry,
116  const LPoint3 &into_intersection_point,
117  double extra_radius) const;
118 
119 private:
120  LPoint3 _a, _b;
121  PN_stdfloat _radius;
122 
123  // These are derived from the above.
124  LMatrix4 _mat;
125  LMatrix4 _inv_mat;
126  PN_stdfloat _length;
127 
128  static PStatCollector _volume_pcollector;
129  static PStatCollector _test_pcollector;
130 
131 public:
132  static void register_with_read_factory();
133  virtual void write_datagram(BamWriter *manager, Datagram &dg);
134 
135 protected:
136  static TypedWritable *make_from_bam(const FactoryParams &params);
137  void fillin(DatagramIterator &scan, BamReader *manager);
138 
139 public:
140  static TypeHandle get_class_type() {
141  return _type_handle;
142  }
143  static void init_type() {
144  CollisionSolid::init_type();
145  register_type(_type_handle, "CollisionCapsule",
146  CollisionSolid::get_class_type());
147  }
148  virtual TypeHandle get_type() const {
149  return get_class_type();
150  }
151  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
152 
153 private:
154  static TypeHandle _type_handle;
155 
156  friend class CollisionBox;
157 };
158 
159 #include "collisionCapsule.I"
160 
161 #endif
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,...
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 is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void write_datagram(BamWriter *manager, Datagram &me)
Function to write the important information in the particular object to a Datagram.
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.
This implements a solid consisting of a cylinder with hemispherical endcaps, also known as a capsule ...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
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.
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