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