Panda3D
collisionTraverser.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 collisionTraverser.h
10  * @author drose
11  * @date 2002-03-16
12  */
13 
14 #ifndef COLLISIONTRAVERSER_H
15 #define COLLISIONTRAVERSER_H
16 
17 #include "pandabase.h"
18 
19 #include "collisionHandler.h"
20 #include "collisionLevelState.h"
21 
22 #include "pointerTo.h"
23 #include "pStatCollector.h"
24 
25 #include "pset.h"
26 #include "register_type.h"
27 
28 class CollisionNode;
29 class CollisionRecorder;
30 class CollisionVisualizer;
31 class Geom;
32 class NodePath;
33 class CollisionEntry;
34 
35 /**
36  * This class manages the traversal through the scene graph to detect
37  * collisions. It holds ownership of a number of collider objects, each of
38  * which is a CollisionNode and an associated CollisionHandler.
39  *
40  * When traverse() is called, it begins at the indicated root and detects all
41  * collisions with any of its collider objects against nodes at or below the
42  * indicated root, calling the appropriate CollisionHandler for each detected
43  * collision.
44  */
45 class EXPCL_PANDA_COLLIDE CollisionTraverser : public Namable {
46 PUBLISHED:
47  explicit CollisionTraverser(const std::string &name = "ctrav");
49 
50  INLINE void set_respect_prev_transform(bool flag);
51  INLINE bool get_respect_prev_transform() const;
52  MAKE_PROPERTY(respect_preV_transform, get_respect_prev_transform,
53  set_respect_prev_transform);
54  MAKE_PROPERTY(respect_prev_transform, get_respect_prev_transform,
55  set_respect_prev_transform);
56 
57  void add_collider(const NodePath &collider, CollisionHandler *handler);
58  bool remove_collider(const NodePath &collider);
59  bool has_collider(const NodePath &collider) const;
60  int get_num_colliders() const;
61  NodePath get_collider(int n) const;
62  MAKE_SEQ(get_colliders, get_num_colliders, get_collider);
63  CollisionHandler *get_handler(const NodePath &collider) const;
64  void clear_colliders();
65  MAKE_SEQ_PROPERTY(colliders, get_num_colliders, get_collider);
66 
67  void traverse(const NodePath &root);
68 
69 #ifdef DO_COLLISION_RECORDING
70  void set_recorder(CollisionRecorder *recorder);
71  INLINE bool has_recorder() const;
72  INLINE CollisionRecorder *get_recorder() const;
73  INLINE void clear_recorder();
74  MAKE_PROPERTY2(recorder, has_recorder, get_recorder,
75  set_recorder, clear_recorder);
76 
77  CollisionVisualizer *show_collisions(const NodePath &root);
78  void hide_collisions();
79 #endif // DO_COLLISION_RECORDING
80 
81  void output(std::ostream &out) const;
82  void write(std::ostream &out, int indent_level) const;
83 
84 private:
86  void prepare_colliders_single(LevelStatesSingle &level_states, const NodePath &root);
87  void r_traverse_single(CollisionLevelStateSingle &level_state, size_t pass);
88 
90  void prepare_colliders_double(LevelStatesDouble &level_states, const NodePath &root);
91  void r_traverse_double(CollisionLevelStateDouble &level_state, size_t pass);
92 
94  void prepare_colliders_quad(LevelStatesQuad &level_states, const NodePath &root);
95  void r_traverse_quad(CollisionLevelStateQuad &level_state, size_t pass);
96 
97  void compare_collider_to_node(CollisionEntry &entry,
98  const GeometricBoundingVolume *from_parent_gbv,
99  const GeometricBoundingVolume *from_node_gbv,
100  const GeometricBoundingVolume *into_node_gbv);
101  void compare_collider_to_geom_node(CollisionEntry &entry,
102  const GeometricBoundingVolume *from_parent_gbv,
103  const GeometricBoundingVolume *from_node_gbv,
104  const GeometricBoundingVolume *into_node_gbv);
105  void compare_collider_to_solid(CollisionEntry &entry,
106  const GeometricBoundingVolume *from_node_gbv,
107  const GeometricBoundingVolume *solid_gbv);
108  void compare_collider_to_geom(CollisionEntry &entry, const Geom *geom,
109  const GeometricBoundingVolume *from_node_gbv,
110  const GeometricBoundingVolume *solid_gbv);
111 
112  PStatCollector &get_pass_collector(int pass);
113 
114 private:
115  PT(CollisionHandler) _default_handler;
116 
117  class OrderedColliderDef {
118  public:
119  NodePath _node_path;
120  bool _in_graph;
121  };
122 
123  typedef pmap<NodePath, PT(CollisionHandler) > Colliders;
124  Colliders _colliders;
125  typedef pvector<OrderedColliderDef> OrderedColliders;
126  OrderedColliders _ordered_colliders;
127 
128  typedef pmap<PT(CollisionHandler), int> Handlers;
129  Handlers _handlers;
130 
131  Handlers::iterator remove_handler(Handlers::iterator hi);
132 
133  bool _respect_prev_transform;
134 #ifdef DO_COLLISION_RECORDING
135  CollisionRecorder *_recorder;
136  NodePath _collision_visualizer_np;
137 #endif // DO_COLLISION_RECORDING
138 
139  // Statistics
140  static PStatCollector _collisions_pcollector;
141 
142  static PStatCollector _cnode_volume_pcollector;
143  static PStatCollector _gnode_volume_pcollector;
144  static PStatCollector _geom_volume_pcollector;
145 
146  PStatCollector _this_pcollector;
147  typedef pvector<PStatCollector> PassCollectors;
148  PassCollectors _pass_collectors;
149  // pstats category for actual collision detection (vs. bounding heirarchy
150  // collision detection)
151  typedef pvector<PStatCollector> SolidCollideCollectors;
152  SolidCollideCollectors _solid_collide_collectors;
153 
154 public:
155  static TypeHandle get_class_type() {
156  return _type_handle;
157  }
158  static void init_type() {
159  register_type(_type_handle, "CollisionTraverser");
160  }
161 
162 private:
163  static TypeHandle _type_handle;
164 
165  friend class SortByColliderSort;
166 };
167 
168 INLINE std::ostream &operator << (std::ostream &out, const CollisionTraverser &trav) {
169  trav.output(out);
170  return out;
171 }
172 
173 #include "collisionTraverser.I"
174 
175 #endif
The abstract interface to a number of classes that decide what to do when a collision is detected.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
This is the state information the CollisionTraverser retains for each level during traversal.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
void output(std::ostream &out) const
Outputs the Namable.
Definition: namable.I:61
A lightweight class that represents a single element that may be timed and/or counted via stats.
This is another abstract class, for a general class of bounding volumes that actually enclose points ...
A base class for all things which can have a name.
Definition: namable.h:26
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Defines a single collision event.
A container for geometry primitives.
Definition: geom.h:54
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A node in the scene graph that can hold any number of CollisionSolids.
Definition: collisionNode.h:30
This class manages the traversal through the scene graph to detect collisions.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161