Panda3D
Loading...
Searching...
No Matches
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
34TypeHandle CollisionSegment::_type_handle;
35
36
37/**
38 *
39 */
40CollisionSolid *CollisionSegment::
41make_copy() {
42 return new CollisionSegment(*this);
43}
44
45/**
46 *
47 */
48PT(CollisionEntry) CollisionSegment::
49test_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 */
56void CollisionSegment::
57xform(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 */
71 return get_point_a();
72}
73
74/**
75 *
76 */
77void CollisionSegment::
78output(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 */
91set_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 */
110PT(BoundingVolume) CollisionSegment::
111compute_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 */
141void CollisionSegment::
142fill_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 */
180write_datagram(BamWriter *manager, Datagram &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 */
191TypedWritable *CollisionSegment::
192make_from_bam(const FactoryParams &params) {
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 */
207void CollisionSegment::
208fillin(DatagramIterator &scan, BamReader *manager) {
209 CollisionSolid::fillin(scan, manager);
210 _a.read_datagram(scan);
211 _b.read_datagram(scan);
212}
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.
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
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition bamReader.I:177
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
This defines a bounding convex hexahedron.
This defines a bounding sphere, consisting of a center and a radius.
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
Defines a single collision event.
get_into
Returns the CollisionSolid pointer for the particular solid was collided into.
A finite line segment, with two specific endpoints but no thickness.
static void register_with_read_factory()
Tells the BamReader how to create objects of type CollisionSegment.
bool set_from_lens(LensNode *camera, const LPoint2 &point)
Accepts a LensNode and a 2-d point in the range [-1,1].
virtual LPoint3 get_collision_origin() const
Returns the point in space deemed to be the "origin" of the solid for collision purposes.
CollisionSegment()
Creates an invalid segment.
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,...
virtual void write_datagram(BamWriter *manager, Datagram &me)
Function to write the important information in the particular object to a Datagram.
A class to retrieve the individual data elements previously stored in a Datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
An instance of this class is passed to the Factory when requesting it to do its business and construc...
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
Defines a series of disconnected line segments.
Definition geomLines.h:23
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
static const GeomVertexFormat * get_v3cp()
Returns a standard vertex format with a packed color and a 3-component vertex position.
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
A container for geometry primitives.
Definition geom.h:54
A node that contains a Lens.
Definition lensNode.h:29
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
A base class for any number of different kinds of lenses, linear and otherwise.
Definition lens.h:41
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
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.