Panda3D
collisionRay.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 collisionRay.cxx
10  * @author drose
11  * @date 2000-06-22
12  */
13 
14 #include "collisionRay.h"
15 #include "collisionHandler.h"
16 #include "collisionEntry.h"
17 #include "config_collide.h"
18 #include "geom.h"
19 #include "geomNode.h"
20 #include "boundingLine.h"
21 #include "lensNode.h"
22 #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 "geomLinestrips.h"
29 #include "geomVertexWriter.h"
30 
31 TypeHandle CollisionRay::_type_handle;
32 
33 
34 /**
35  *
36  */
37 CollisionSolid *CollisionRay::
38 make_copy() {
39  return new CollisionRay(*this);
40 }
41 
42 /**
43  *
44  */
45 PT(CollisionEntry) CollisionRay::
46 test_intersection(const CollisionEntry &entry) const {
47  return entry.get_into()->test_intersection_from_ray(entry);
48 }
49 
50 /**
51  * Transforms the solid by the indicated matrix.
52  */
53 void CollisionRay::
54 xform(const LMatrix4 &mat) {
55  _origin = _origin * mat;
56  _direction = _direction * mat;
57 
58  CollisionSolid::xform(mat);
59 }
60 
61 /**
62  * Returns the point in space deemed to be the "origin" of the solid for
63  * collision purposes. The closest intersection point to this origin point is
64  * considered to be the most significant.
65  */
66 LPoint3 CollisionRay::
68  return get_origin();
69 }
70 
71 /**
72  *
73  */
74 void CollisionRay::
75 output(std::ostream &out) const {
76  out << "ray, o (" << get_origin() << "), d (" << get_direction() << ")";
77 }
78 
79 /**
80  * Accepts a LensNode and a 2-d point in the range [-1,1]. Sets the
81  * CollisionRay so that it begins at the LensNode's near plane and extends to
82  * infinity, making it suitable for picking objects from the screen given a
83  * camera and a mouse location.
84  *
85  * Returns true if the point was acceptable, false otherwise.
86  */
87 bool CollisionRay::
88 set_from_lens(LensNode *camera, const LPoint2 &point) {
89  Lens *lens = camera->get_lens();
90 
91  bool success = true;
92  LPoint3 near_point, far_point;
93  if (!lens->extrude(point, near_point, far_point)) {
94  _origin = LPoint3::origin();
95  _direction = LVector3::forward();
96  success = false;
97  } else {
98  _origin = near_point;
99  _direction = far_point - near_point;
100  }
101 
102  mark_internal_bounds_stale();
103  mark_viz_stale();
104 
105  return success;
106 }
107 
108 /**
109  *
110  */
111 PT(BoundingVolume) CollisionRay::
112 compute_internal_bounds() const {
113  return new BoundingLine(_origin, _origin + _direction);
114 }
115 
116 /**
117  * Fills the _viz_geom GeomNode up with Geoms suitable for rendering this
118  * solid.
119  */
120 void CollisionRay::
121 fill_viz_geom() {
122  if (collide_cat.is_debug()) {
123  collide_cat.debug()
124  << "Recomputing viz for " << *this << "\n";
125  }
126 
127  static const int num_points = 100;
128  static const double scale = 100.0;
129 
130  PT(GeomVertexData) vdata = new GeomVertexData
131  ("collision", GeomVertexFormat::get_v3cp(),
132  Geom::UH_static);
133  GeomVertexWriter vertex(vdata, InternalName::get_vertex());
134  GeomVertexWriter color(vdata, InternalName::get_color());
135 
136  for (int i = 0; i < num_points; i++) {
137  double t = ((double)i / (double)num_points);
138  vertex.add_data3(get_origin() + t * scale * get_direction());
139 
140  color.add_data4(LColor(1.0f, 1.0f, 1.0f, 1.0f) +
141  t * LColor(0.0f, 0.0f, 0.0f, -1.0f));
142  }
143 
144  PT(GeomLinestrips) line = new GeomLinestrips(Geom::UH_static);
145  line->add_next_vertices(num_points);
146  line->close_primitive();
147 
148  PT(Geom) geom = new Geom(vdata);
149  geom->add_primitive(line);
150 
151  _viz_geom->add_geom(geom, get_other_viz_state());
152  _bounds_viz_geom->add_geom(geom, get_other_bounds_viz_state());
153 }
154 
155 /**
156  * Tells the BamReader how to create objects of type CollisionRay.
157  */
158 void CollisionRay::
160  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
161 }
162 
163 /**
164  * Writes the contents of this object to the datagram for shipping out to a
165  * Bam file.
166  */
167 void CollisionRay::
169  CollisionSolid::write_datagram(manager, dg);
170  _origin.write_datagram(dg);
171  _direction.write_datagram(dg);
172 }
173 
174 /**
175  * This function is called by the BamReader's factory when a new object of
176  * type CollisionRay is encountered in the Bam file. It should create the
177  * CollisionRay and extract its information from the file.
178  */
179 TypedWritable *CollisionRay::
180 make_from_bam(const FactoryParams &params) {
181  CollisionRay *node = new CollisionRay();
182  DatagramIterator scan;
183  BamReader *manager;
184 
185  parse_params(params, scan, manager);
186  node->fillin(scan, manager);
187 
188  return node;
189 }
190 
191 /**
192  * This internal function is called by make_from_bam to read in all of the
193  * relevant data from the BamFile for the new CollisionRay.
194  */
195 void CollisionRay::
196 fillin(DatagramIterator &scan, BamReader *manager) {
197  CollisionSolid::fillin(scan, manager);
198  _origin.read_datagram(scan);
199  _direction.read_datagram(scan);
200 }
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
An infinite ray, with a specific origin and direction.
Definition: collisionRay.h:27
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual LPoint3 get_collision_origin() const
Returns the point in space deemed to be the "origin" of the solid for collision purposes.
A base class for any number of different kinds of lenses, linear and otherwise.
Definition: lens.h:41
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
The abstract base class for all things that can collide with other things in the world,...
A node that contains a Lens.
Definition: lensNode.h:29
static const GeomVertexFormat * get_v3cp()
Returns a standard vertex format with a packed color and a 3-component vertex position.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
virtual void write_datagram(BamWriter *manager, Datagram &me)
Function to write the important information in the particular object to a Datagram.
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
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Defines a single collision event.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A container for geometry primitives.
Definition: geom.h:54
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void register_with_read_factory()
Tells the BamReader how to create objects of type CollisionRay.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
get_into
Returns the CollisionSolid pointer for the particular solid was collided into.
Defines a series of line strips.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PT(CollisionEntry) CollisionRay
Transforms the solid by the indicated matrix.
A class to retrieve the individual data elements previously stored in a Datagram.
bool set_from_lens(LensNode *camera, const LPoint2 &point)
Accepts a LensNode and a 2-d point in the range [-1,1].
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
CollisionRay()
Creates an invalid ray.
Definition: collisionRay.I:20
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This funny bounding volume is an infinite line with no thickness and extending to infinity in both di...
Definition: boundingLine.h:29
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.