Panda3D
collisionLine.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 collisionLine.cxx
10  * @author drose
11  * @date 2005-01-05
12  */
13 
14 #include "collisionLine.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 CollisionLine::_type_handle;
32 
33 
34 /**
35  *
36  */
37 CollisionSolid *CollisionLine::
38 make_copy() {
39  return new CollisionLine(*this);
40 }
41 
42 /**
43  *
44  */
45 PT(CollisionEntry) CollisionLine::
46 test_intersection(const CollisionEntry &entry) const {
47  return entry.get_into()->test_intersection_from_line(entry);
48 }
49 
50 /**
51  *
52  */
53 void CollisionLine::
54 output(std::ostream &out) const {
55  out << "line, o (" << get_origin() << "), d (" << get_direction() << ")";
56 }
57 
58 /**
59  * Fills the _viz_geom GeomNode up with Geoms suitable for rendering this
60  * solid.
61  */
62 void CollisionLine::
63 fill_viz_geom() {
64  if (collide_cat.is_debug()) {
65  collide_cat.debug()
66  << "Recomputing viz for " << *this << "\n";
67  }
68 
69  static const int num_points = 100;
70  static const double scale = 100.0;
71 
72  PT(GeomVertexData) vdata = new GeomVertexData
73  ("collision", GeomVertexFormat::get_v3cp(),
74  Geom::UH_static);
75  GeomVertexWriter vertex(vdata, InternalName::get_vertex());
76  GeomVertexWriter color(vdata, InternalName::get_color());
77 
78  for (int i = 0; i < num_points; i++) {
79  double t = ((double)i / (double)num_points - 0.5) * 2.0;
80  vertex.add_data3(get_origin() + t * scale * get_direction());
81 
82  color.add_data4(LColor(1.0f, 1.0f, 1.0f, 1.0f) +
83  fabs(t) * LColor(0.0f, 0.0f, 0.0f, -1.0f));
84  }
85 
86  PT(GeomLinestrips) line = new GeomLinestrips(Geom::UH_static);
87  line->add_next_vertices(num_points);
88  line->close_primitive();
89 
90  PT(Geom) geom = new Geom(vdata);
91  geom->add_primitive(line);
92 
93  _viz_geom->add_geom(geom, get_other_viz_state());
94  _bounds_viz_geom->add_geom(geom, get_other_bounds_viz_state());
95 }
96 
97 /**
98  * Tells the BamReader how to create objects of type CollisionLine.
99  */
100 void CollisionLine::
102  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
103 }
104 
105 /**
106  * Writes the contents of this object to the datagram for shipping out to a
107  * Bam file.
108  */
109 void CollisionLine::
111  CollisionRay::write_datagram(manager, dg);
112 }
113 
114 /**
115  * This function is called by the BamReader's factory when a new object of
116  * type CollisionLine is encountered in the Bam file. It should create the
117  * CollisionLine and extract its information from the file.
118  */
119 TypedWritable *CollisionLine::
120 make_from_bam(const FactoryParams &params) {
121  CollisionLine *node = new CollisionLine();
122  DatagramIterator scan;
123  BamReader *manager;
124 
125  parse_params(params, scan, manager);
126  node->fillin(scan, manager);
127 
128  return node;
129 }
130 
131 /**
132  * This internal function is called by make_from_bam to read in all of the
133  * relevant data from the BamFile for the new CollisionLine.
134  */
135 void CollisionLine::
136 fillin(DatagramIterator &scan, BamReader *manager) {
137  CollisionRay::fillin(scan, manager);
138 }
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
The abstract base class for all things that can collide with other things in the world,...
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.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
CollisionLine()
Creates an invalid line.
Definition: collisionLine.I:20
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
PT(CollisionEntry) CollisionLine
Fills the _viz_geom GeomNode up with Geoms suitable for rendering this solid.
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.
An infinite line, similar to a CollisionRay, except that it extends in both directions.
Definition: collisionLine.h:25
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
static void register_with_read_factory()
Tells the BamReader how to create objects of type CollisionLine.
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.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.