Panda3D
collisionHandlerQueue.cxx
1 // Filename: collisionHandlerQueue.cxx
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 #include "collisionHandlerQueue.h"
16 #include "config_collide.h"
17 #include "indent.h"
18 
19 TypeHandle CollisionHandlerQueue::_type_handle;
20 
21 // This class is used in sort_entries(), below.
23 public:
25  _entry = entry;
26  LVector3 vec =
27  entry->get_surface_point(entry->get_from_node_path()) -
28  entry->get_from()->get_collision_origin();
29  _dist2 = vec.length_squared();
30  }
31  bool operator < (const CollisionEntrySorter &other) const {
32  return _dist2 < other._dist2;
33  }
34 
35  CollisionEntry *_entry;
36  PN_stdfloat _dist2;
37 };
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: CollisionHandlerQueue::Constructor
41 // Access: Published
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 CollisionHandlerQueue::
45 CollisionHandlerQueue() {
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: CollisionHandlerQueue::begin_group
50 // Access: Published, Virtual
51 // Description: Will be called by the CollisionTraverser before a new
52 // traversal is begun. It instructs the handler to
53 // reset itself in preparation for a number of
54 // CollisionEntries to be sent.
55 ////////////////////////////////////////////////////////////////////
58  _entries.clear();
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: CollisionHandlerQueue::add_entry
63 // Access: Published, Virtual
64 // Description: Called between a begin_group() .. end_group()
65 // sequence for each collision that is detected.
66 ////////////////////////////////////////////////////////////////////
69  nassertv(entry != (CollisionEntry *)NULL);
70  _entries.push_back(entry);
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: CollisionHandlerQueue::sort_entries
75 // Access: Published
76 // Description: Sorts all the detected collisions front-to-back by
77 // from_intersection_point() so that those intersection
78 // points closest to the collider's origin (e.g., the
79 // center of the CollisionSphere, or the point_a of a
80 // CollisionSegment) appear first.
81 ////////////////////////////////////////////////////////////////////
84  // Build up a temporary vector of entries so we can sort the
85  // pointers. This uses the class defined above.
86  typedef pvector<CollisionEntrySorter> Sorter;
87  Sorter sorter;
88  sorter.reserve(_entries.size());
89 
90  Entries::const_iterator ei;
91  for (ei = _entries.begin(); ei != _entries.end(); ++ei) {
92  sorter.push_back(CollisionEntrySorter(*ei));
93  }
94 
95  sort(sorter.begin(), sorter.end());
96  nassertv(sorter.size() == _entries.size());
97 
98  // Now that they're sorted, get them back. We do this in two steps,
99  // building up a temporary vector first, so we don't accidentally
100  // delete all the entries when the pointers go away.
101  Entries sorted_entries;
102  sorted_entries.reserve(sorter.size());
103  Sorter::const_iterator si;
104  for (si = sorter.begin(); si != sorter.end(); ++si) {
105  sorted_entries.push_back((*si)._entry);
106  }
107 
108  _entries.swap(sorted_entries);
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function: CollisionHandlerQueue::clear_entries
113 // Access: Published
114 // Description: Removes all the entries from the queue.
115 ////////////////////////////////////////////////////////////////////
118  _entries.clear();
119 }
120 
121 ////////////////////////////////////////////////////////////////////
122 // Function: CollisionHandlerQueue::get_num_entries
123 // Access: Published
124 // Description: Returns the number of CollisionEntries detected last
125 // pass.
126 ////////////////////////////////////////////////////////////////////
129  return _entries.size();
130 }
131 
132 ////////////////////////////////////////////////////////////////////
133 // Function: CollisionHandlerQueue::get_entry
134 // Access: Published
135 // Description: Returns the nth CollisionEntry detected last pass.
136 ////////////////////////////////////////////////////////////////////
138 get_entry(int n) const {
139  nassertr(n >= 0 && n < (int)_entries.size(), NULL);
140  return _entries[n];
141 }
142 
143 ////////////////////////////////////////////////////////////////////
144 // Function: CollisionHandlerQueue::output
145 // Access: Published
146 // Description:
147 ////////////////////////////////////////////////////////////////////
148 void CollisionHandlerQueue::
149 output(ostream &out) const {
150  out << "CollisionHandlerQueue, " << _entries.size() << " entries";
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function: CollisionHandlerQueue::write
155 // Access: Published
156 // Description:
157 ////////////////////////////////////////////////////////////////////
158 void CollisionHandlerQueue::
159 write(ostream &out, int indent_level) const {
160  indent(out, indent_level)
161  << "CollisionHandlerQueue, " << _entries.size() << " entries:\n";
162 
163  Entries::const_iterator ei;
164  for (ei = _entries.begin(); ei != _entries.end(); ++ei) {
165  (*ei)->write(out, indent_level + 2);
166  }
167 }
float length_squared() const
Returns the square of the vector&#39;s length, cheap and easy.
Definition: lvecBase3.h:749
LPoint3 get_surface_point(const NodePath &space) const
Returns the point, on the surface of the "into" object, at which a collision is detected.
int get_num_entries() const
Returns the number of CollisionEntries detected last pass.
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
void sort_entries()
Sorts all the detected collisions front-to-back by from_intersection_point() so that those intersecti...
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
virtual void add_entry(CollisionEntry *entry)
Called between a begin_group() .
Defines a single collision event.
CollisionEntry * get_entry(int n) const
Returns the nth CollisionEntry detected last pass.
NodePath get_from_node_path() const
Returns the NodePath that represents the CollisionNode that contains the CollisionSolid that triggere...
void clear_entries()
Removes all the entries from the queue.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual void begin_group()
Will be called by the CollisionTraverser before a new traversal is begun.
const CollisionSolid * get_from() const
Returns the CollisionSolid pointer for the particular solid that triggered this collision.