Panda3D
collisionTraverser.h
1 // Filename: collisionTraverser.h
2 // Created by: drose (16Mar02)
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 
15 #ifndef COLLISIONTRAVERSER_H
16 #define COLLISIONTRAVERSER_H
17 
18 #include "pandabase.h"
19 
20 #include "collisionHandler.h"
21 #include "collisionLevelState.h"
22 
23 #include "pointerTo.h"
24 #include "pStatCollector.h"
25 
26 #include "pset.h"
27 #include "register_type.h"
28 
29 class CollisionNode;
30 class CollisionRecorder;
31 class CollisionVisualizer;
32 class Geom;
33 class NodePath;
34 class CollisionEntry;
35 
36 ////////////////////////////////////////////////////////////////////
37 // Class : CollisionTraverser
38 // Description : This class manages the traversal through the scene
39 // graph to detect collisions. It holds ownership of a
40 // number of collider objects, each of which is a
41 // CollisionNode and an associated CollisionHandler.
42 //
43 // When traverse() is called, it begins at the indicated
44 // root and detects all collisions with any of its
45 // collider objects against nodes at or below the
46 // indicated root, calling the appropriate
47 // CollisionHandler for each detected collision.
48 ////////////////////////////////////////////////////////////////////
49 class EXPCL_PANDA_COLLIDE CollisionTraverser : public Namable {
50 PUBLISHED:
51  CollisionTraverser(const string &name = "ctrav");
53 
54  INLINE void set_respect_prev_transform(bool flag);
55  INLINE bool get_respect_prev_transform() const;
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 
66  void traverse(const NodePath &root);
67 
68 #ifdef DO_COLLISION_RECORDING
69  void set_recorder(CollisionRecorder *recorder);
70  INLINE bool has_recorder() const;
71  INLINE CollisionRecorder *get_recorder() const;
72  INLINE void clear_recorder();
73 
74  CollisionVisualizer *show_collisions(const NodePath &root);
75  void hide_collisions();
76 #endif // DO_COLLISION_RECORDING
77 
78  void output(ostream &out) const;
79  void write(ostream &out, int indent_level) const;
80 
81 private:
83  void prepare_colliders_single(LevelStatesSingle &level_states, const NodePath &root);
84  void r_traverse_single(CollisionLevelStateSingle &level_state, size_t pass);
85 
87  void prepare_colliders_double(LevelStatesDouble &level_states, const NodePath &root);
88  void r_traverse_double(CollisionLevelStateDouble &level_state, size_t pass);
89 
91  void prepare_colliders_quad(LevelStatesQuad &level_states, const NodePath &root);
92  void r_traverse_quad(CollisionLevelStateQuad &level_state, size_t pass);
93 
94  void compare_collider_to_node(CollisionEntry &entry,
95  const GeometricBoundingVolume *from_parent_gbv,
96  const GeometricBoundingVolume *from_node_gbv,
97  const GeometricBoundingVolume *into_node_gbv);
98  void compare_collider_to_geom_node(CollisionEntry &entry,
99  const GeometricBoundingVolume *from_parent_gbv,
100  const GeometricBoundingVolume *from_node_gbv,
101  const GeometricBoundingVolume *into_node_gbv);
102  void compare_collider_to_solid(CollisionEntry &entry,
103  const GeometricBoundingVolume *from_node_gbv,
104  const GeometricBoundingVolume *solid_gbv);
105  void compare_collider_to_geom(CollisionEntry &entry, const Geom *geom,
106  const GeometricBoundingVolume *from_node_gbv,
107  const GeometricBoundingVolume *solid_gbv);
108 
109  PStatCollector &get_pass_collector(int pass);
110 
111 private:
112  PT(CollisionHandler) _default_handler;
113  TypeHandle _graph_type;
114 
115  class OrderedColliderDef {
116  public:
117  NodePath _node_path;
118  bool _in_graph;
119  };
120 
122  Colliders _colliders;
124  OrderedColliders _ordered_colliders;
125 
127  Handlers _handlers;
128 
129  Handlers::iterator remove_handler(Handlers::iterator hi);
130 
131  bool _respect_prev_transform;
132 #ifdef DO_COLLISION_RECORDING
133  CollisionRecorder *_recorder;
134  NodePath _collision_visualizer_np;
135 #endif // DO_COLLISION_RECORDING
136 
137  // Statistics
138  static PStatCollector _collisions_pcollector;
139 
140  static PStatCollector _cnode_volume_pcollector;
141  static PStatCollector _gnode_volume_pcollector;
142  static PStatCollector _geom_volume_pcollector;
143 
144  PStatCollector _this_pcollector;
146  PassCollectors _pass_collectors;
147  // pstats category for actual collision detection (vs. bounding heirarchy collision detection)
149  SolidCollideCollectors _solid_collide_collectors;
150 
151 public:
152  static TypeHandle get_class_type() {
153  return _type_handle;
154  }
155  static void init_type() {
156  register_type(_type_handle, "CollisionTraverser");
157  }
158 
159 private:
160  static TypeHandle _type_handle;
161 
162  friend class SortByColliderSort;
163 };
164 
165 INLINE ostream &operator << (ostream &out, const CollisionTraverser &trav) {
166  trav.output(out);
167  return out;
168 }
169 
170 #include "collisionTraverser.I"
171 
172 #endif
173 
The abstract interface to a number of classes that decide what to do when a collision is detected...
void output(ostream &out) const
Outputs the Namable.
Definition: namable.I:97
This is the state information the CollisionTraverser retains for each level during traversal...
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
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:29
Defines a single collision event.
A container for geometry primitives.
Definition: geom.h:58
A node in the scene graph that can hold any number of CollisionSolids.
Definition: collisionNode.h:33
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:85
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165