Panda3D
 All Classes Functions Variables Enumerations
collisionLine.cxx
1 // Filename: collisionLine.cxx
2 // Created by: drose (05Jan05)
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 "collisionLine.h"
16 #include "collisionHandler.h"
17 #include "collisionEntry.h"
18 #include "config_collide.h"
19 #include "geom.h"
20 #include "geomNode.h"
21 #include "boundingLine.h"
22 #include "lensNode.h"
23 #include "lens.h"
24 #include "datagram.h"
25 #include "datagramIterator.h"
26 #include "bamReader.h"
27 #include "bamWriter.h"
28 #include "geom.h"
29 #include "geomLinestrips.h"
30 #include "geomVertexWriter.h"
31 
32 TypeHandle CollisionLine::_type_handle;
33 
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: CollisionLine::make_copy
37 // Access: Public, Virtual
38 // Description:
39 ////////////////////////////////////////////////////////////////////
40 CollisionSolid *CollisionLine::
41 make_copy() {
42  return new CollisionLine(*this);
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: CollisionLine::test_intersection
47 // Access: Public, Virtual
48 // Description:
49 ////////////////////////////////////////////////////////////////////
51 test_intersection(const CollisionEntry &entry) const {
52  return entry.get_into()->test_intersection_from_line(entry);
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: CollisionLine::output
57 // Access: Public, Virtual
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 void CollisionLine::
61 output(ostream &out) const {
62  out << "line, o (" << get_origin() << "), d (" << get_direction() << ")";
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: CollisionLine::fill_viz_geom
67 // Access: Protected, Virtual
68 // Description: Fills the _viz_geom GeomNode up with Geoms suitable
69 // for rendering this solid.
70 ////////////////////////////////////////////////////////////////////
71 void CollisionLine::
72 fill_viz_geom() {
73  if (collide_cat.is_debug()) {
74  collide_cat.debug()
75  << "Recomputing viz for " << *this << "\n";
76  }
77 
78  static const int num_points = 100;
79  static const double scale = 100.0;
80 
81  PT(GeomVertexData) vdata = new GeomVertexData
82  ("collision", GeomVertexFormat::get_v3cp(),
83  Geom::UH_static);
84  GeomVertexWriter vertex(vdata, InternalName::get_vertex());
85  GeomVertexWriter color(vdata, InternalName::get_color());
86 
87  for (int i = 0; i < num_points; i++) {
88  double t = ((double)i / (double)num_points - 0.5) * 2.0;
89  vertex.add_data3(get_origin() + t * scale * get_direction());
90 
91  color.add_data4(LColor(1.0f, 1.0f, 1.0f, 1.0f) +
92  fabs(t) * LColor(0.0f, 0.0f, 0.0f, -1.0f));
93  }
94 
95  PT(GeomLinestrips) line = new GeomLinestrips(Geom::UH_static);
96  line->add_next_vertices(num_points);
97  line->close_primitive();
98 
99  PT(Geom) geom = new Geom(vdata);
100  geom->add_primitive(line);
101 
102  _viz_geom->add_geom(geom, get_other_viz_state());
103  _bounds_viz_geom->add_geom(geom, get_other_bounds_viz_state());
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: CollisionLine::register_with_read_factory
108 // Access: Public, Static
109 // Description: Tells the BamReader how to create objects of type
110 // CollisionLine.
111 ////////////////////////////////////////////////////////////////////
112 void CollisionLine::
114  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
115 }
116 
117 ////////////////////////////////////////////////////////////////////
118 // Function: CollisionLine::write_datagram
119 // Access: Public, Virtual
120 // Description: Writes the contents of this object to the datagram
121 // for shipping out to a Bam file.
122 ////////////////////////////////////////////////////////////////////
123 void CollisionLine::
125  CollisionRay::write_datagram(manager, dg);
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: CollisionLine::make_from_bam
130 // Access: Protected, Static
131 // Description: This function is called by the BamReader's factory
132 // when a new object of type CollisionLine is encountered
133 // in the Bam file. It should create the CollisionLine
134 // and extract its information from the file.
135 ////////////////////////////////////////////////////////////////////
136 TypedWritable *CollisionLine::
137 make_from_bam(const FactoryParams &params) {
138  CollisionLine *node = new CollisionLine();
139  DatagramIterator scan;
140  BamReader *manager;
141 
142  parse_params(params, scan, manager);
143  node->fillin(scan, manager);
144 
145  return node;
146 }
147 
148 ////////////////////////////////////////////////////////////////////
149 // Function: CollisionLine::fillin
150 // Access: Protected
151 // Description: This internal function is called by make_from_bam to
152 // read in all of the relevant data from the BamFile for
153 // the new CollisionLine.
154 ////////////////////////////////////////////////////////////////////
155 void CollisionLine::
156 fillin(DatagramIterator &scan, BamReader *manager) {
157  CollisionRay::fillin(scan, manager);
158 }
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
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, and all the things they can collide with (except geometry).
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
CollisionLine()
Creates an invalid line.
Definition: collisionLine.I:25
Defines a single collision event.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
Definition: geom.h:58
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
An infinite line, similar to a CollisionRay, except that it extends in both directions.
Definition: collisionLine.h:28
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
Defines a series of line strips.
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:85
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:43
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.