Panda3D
collisionSegment.cxx
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 collisionSegment.cxx
10  * @author drose
11  * @date 2001-01-30
12  */
13 
14 #include "collisionSegment.h"
15 #include "collisionHandler.h"
16 #include "collisionEntry.h"
17 #include "config_collide.h"
18 #include "geom.h"
19 #include "lensNode.h"
20 #include "geomNode.h"
21 #include "lens.h"
23 #include "datagram.h"
24 #include "datagramIterator.h"
25 #include "bamReader.h"
26 #include "bamWriter.h"
27 #include "geom.h"
28 #include "geomLines.h"
29 #include "boundingSphere.h"
30 #include "boundingHexahedron.h"
31 #include "geomVertexWriter.h"
32 #include "look_at.h"
33 
34 TypeHandle CollisionSegment::_type_handle;
35 
36 
37 /**
38  *
39  */
40 CollisionSolid *CollisionSegment::
41 make_copy() {
42  return new CollisionSegment(*this);
43 }
44 
45 /**
46  *
47  */
48 PT(CollisionEntry) CollisionSegment::
49 test_intersection(const CollisionEntry &entry) const {
50  return entry.get_into()->test_intersection_from_segment(entry);
51 }
52 
53 /**
54  * Transforms the solid by the indicated matrix.
55  */
56 void CollisionSegment::
57 xform(const LMatrix4 &mat) {
58  _a = _a * mat;
59  _b = _b * mat;
60 
61  CollisionSolid::xform(mat);
62 }
63 
64 /**
65  * Returns the point in space deemed to be the "origin" of the solid for
66  * collision purposes. The closest intersection point to this origin point is
67  * considered to be the most significant.
68  */
70 get_collision_origin() const {
71  return get_point_a();
72 }
73 
74 /**
75  *
76  */
77 void CollisionSegment::
78 output(std::ostream &out) const {
79  out << "segment, a (" << _a << "), b (" << _b << ")";
80 }
81 
82 /**
83  * Accepts a LensNode and a 2-d point in the range [-1,1]. Sets the
84  * CollisionSegment so that it begins at the LensNode's near plane and extends
85  * to the far plane, making it suitable for picking objects from the screen
86  * given a camera and a mouse location.
87  *
88  * Returns true if the point was acceptable, false otherwise.
89  */
91 set_from_lens(LensNode *camera, const LPoint2 &point) {
92  Lens *proj = camera->get_lens();
93 
94  bool success = true;
95  if (!proj->extrude(point, _a, _b)) {
96  _a = LPoint3::origin();
97  _b = _a + LVector3::forward();
98  success = false;
99  }
100 
101  mark_internal_bounds_stale();
102  mark_viz_stale();
103 
104  return success;
105 }
106 
107 /**
108  *
109  */
110 PT(BoundingVolume) CollisionSegment::
111 compute_internal_bounds() const {
112 
113  LVector3 pdelta = _b - _a;
114 
115  // If p1 and p2 are sufficiently close, just put a sphere around them.
116  PN_stdfloat d2 = pdelta.length_squared();
117  if (d2 < collision_parabola_bounds_threshold * collision_parabola_bounds_threshold) {
118  LPoint3 pmid = (_a + _b) * 0.5f;
119  return new BoundingSphere(pmid, csqrt(d2) * 0.5f);
120  }
121 
122  LMatrix4 from_segment;
123  look_at(from_segment, pdelta, LPoint3(0,0,1), CS_zup_right);
124  from_segment.set_row(3, _a);
125 
126  PN_stdfloat max_y = sqrt(d2) + 0.01;
127  PT(BoundingHexahedron) volume =
128  new BoundingHexahedron(LPoint3(-0.01, max_y, -0.01), LPoint3(0.01, max_y, -0.01),
129  LPoint3(0.01, max_y, 0.01), LPoint3(-0.01, max_y, 0.01),
130  LPoint3(-0.01, -0.01, -0.01), LPoint3(0.01, 0.01, -0.01),
131  LPoint3(0.01, -0.01, 0.01), LPoint3(-0.01, -0.01, 0.01));
132 
133  volume->xform(from_segment);
134  return volume;
135 }
136 
137 /**
138  * Fills the _viz_geom GeomNode up with Geoms suitable for rendering this
139  * solid.
140  */
141 void CollisionSegment::
142 fill_viz_geom() {
143  if (collide_cat.is_debug()) {
144  collide_cat.debug()
145  << "Recomputing viz for " << *this << "\n";
146  }
147 
148  PT(GeomVertexData) vdata = new GeomVertexData
149  ("collision", GeomVertexFormat::get_v3cp(),
150  Geom::UH_static);
151  GeomVertexWriter vertex(vdata, InternalName::get_vertex());
152 
153  vertex.add_data3(_a);
154  vertex.add_data3(_b);
155 
156  PT(GeomLines) line = new GeomLines(Geom::UH_static);
157  line->add_next_vertices(2);
158  line->close_primitive();
159 
160  PT(Geom) geom = new Geom(vdata);
161  geom->add_primitive(line);
162 
163  _viz_geom->add_geom(geom, get_other_viz_state());
164  _bounds_viz_geom->add_geom(geom, get_other_bounds_viz_state());
165 }
166 
167 /**
168  * Tells the BamReader how to create objects of type CollisionSegment.
169  */
172  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
173 }
174 
175 /**
176  * Writes the contents of this object to the datagram for shipping out to a
177  * Bam file.
178  */
180 write_datagram(BamWriter *manager, Datagram &dg) {
181  CollisionSolid::write_datagram(manager, dg);
182  _a.write_datagram(dg);
183  _b.write_datagram(dg);
184 }
185 
186 /**
187  * This function is called by the BamReader's factory when a new object of
188  * type CollisionSegment is encountered in the Bam file. It should create the
189  * CollisionSegment and extract its information from the file.
190  */
191 TypedWritable *CollisionSegment::
192 make_from_bam(const FactoryParams &params) {
193  CollisionSegment *node = new CollisionSegment();
194  DatagramIterator scan;
195  BamReader *manager;
196 
197  parse_params(params, scan, manager);
198  node->fillin(scan, manager);
199 
200  return node;
201 }
202 
203 /**
204  * This internal function is called by make_from_bam to read in all of the
205  * relevant data from the BamFile for the new CollisionSegment.
206  */
207 void CollisionSegment::
208 fillin(DatagramIterator &scan, BamReader *manager) {
209  CollisionSolid::fillin(scan, manager);
210  _a.read_datagram(scan);
211  _b.read_datagram(scan);
212 }
Geom
A container for geometry primitives.
Definition: geom.h:54
CollisionSegment
A finite line segment, with two specific endpoints but no thickness.
Definition: collisionSegment.h:31
CollisionSegment::set_from_lens
bool set_from_lens(LensNode *camera, const LPoint2 &point)
Accepts a LensNode and a 2-d point in the range [-1,1].
Definition: collisionSegment.cxx:91
geomVertexWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
CollisionSegment::CollisionSegment
CollisionSegment()
Creates an invalid segment.
Definition: collisionSegment.I:20
CollisionSegment::write_datagram
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: collisionSegment.cxx:180
GeomVertexData
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
Definition: geomVertexData.h:68
BoundingSphere
This defines a bounding sphere, consisting of a center and a radius.
Definition: boundingSphere.h:25
collisionSegment.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition: datagramIterator.h:27
CollisionEntry
Defines a single collision event.
Definition: collisionEntry.h:42
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
GeomVertexWriter
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
Definition: geomVertexWriter.h:55
CollisionSolid::write_datagram
virtual void write_datagram(BamWriter *manager, Datagram &me)
Function to write the important information in the particular object to a Datagram.
Definition: collisionSolid.cxx:345
BamReader::get_factory
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
geomLines.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bamReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PT
PT(CollisionEntry) CollisionSegment
Transforms the solid by the indicated matrix.
Definition: collisionSegment.cxx:48
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
boundingHexahedron.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
collisionHandler.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
boundingSphere.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
CollisionSegment::register_with_read_factory
static void register_with_read_factory()
Tells the BamReader how to create objects of type CollisionSegment.
Definition: collisionSegment.cxx:171
Lens
A base class for any number of different kinds of lenses, linear and otherwise.
Definition: lens.h:41
datagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geom.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
CollisionSegment::get_collision_origin
virtual LPoint3 get_collision_origin() const
Returns the point in space deemed to be the "origin" of the solid for collision purposes.
Definition: collisionSegment.cxx:70
Factory::register_factory
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
CollisionSolid
The abstract base class for all things that can collide with other things in the world,...
Definition: collisionSolid.h:45
LensNode::get_lens
Lens * get_lens(int index=0) const
Returns a pointer to the particular Lens associated with this LensNode, or NULL if there is not yet a...
Definition: lensNode.I:47
collisionEntry.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
lensNode.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
LensNode
A node that contains a Lens.
Definition: lensNode.h:29
GeomLines
Defines a series of disconnected line segments.
Definition: geomLines.h:23
CollisionEntry::get_into
get_into
Returns the CollisionSolid pointer for the particular solid was collided into.
Definition: collisionEntry.h:98
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geomNode.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BoundingHexahedron
This defines a bounding convex hexahedron.
Definition: boundingHexahedron.h:32
config_collide.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BoundingVolume
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
Definition: boundingVolume.h:41
bamWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
look_at.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GeomVertexFormat::get_v3cp
static const GeomVertexFormat * get_v3cp()
Returns a standard vertex format with a packed color and a 3-component vertex position.
Definition: geomVertexFormat.I:287
parse_params
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
Lens::extrude
bool extrude(const LPoint2 &point2d, LPoint3 &near_point, LPoint3 &far_point) const
Given a 2-d point in the range (-1,1) in both dimensions, where (0,0) is the center of the lens and (...
Definition: lens.I:24
lens.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geometricBoundingVolume.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.