Panda3D
Loading...
Searching...
No Matches
collisionBox.h
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 collisionBox.h
10 * @author amith tudur
11 * @date 2009-07-31
12 */
13
14#ifndef COLLISIONBOX_H
15#define COLLISIONBOX_H
16
17#include "pandabase.h"
18#include "collisionSolid.h"
19#include "parabola.h"
20#include "plane.h"
21#include "look_at.h"
22#include "clipPlaneAttrib.h"
23
24/**
25 * A cuboid collision volume or object.
26 */
27class EXPCL_PANDA_COLLIDE CollisionBox : public CollisionSolid {
28PUBLISHED:
29 INLINE explicit CollisionBox(const LPoint3 &center,
30 PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
31 INLINE explicit CollisionBox(const LPoint3 &min, const LPoint3 &max);
32
33 virtual LPoint3 get_collision_origin() const;
34
35protected:
36 INLINE CollisionBox();
37
38public:
39 INLINE CollisionBox(const CollisionBox &copy);
40 virtual CollisionSolid *make_copy();
41
42 virtual PT(CollisionEntry)
43 test_intersection(const CollisionEntry &entry) const;
44 virtual void xform(const LMatrix4 &mat);
45
48
49 virtual void output(std::ostream &out) const;
50
51 INLINE static void flush_level();
52 void setup_box();
53
54PUBLISHED:
55 INLINE int get_num_points() const;
56 INLINE LPoint3 get_point_aabb(int n) const;
57 INLINE LPoint3 get_point(int n) const;
58 INLINE int get_num_planes() const;
59 INLINE LPlane set_plane(int n) const;
60 INLINE LPlane get_plane(int n) const;
61 INLINE void set_center(const LPoint3 &center);
62 INLINE void set_center(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z);
63 INLINE const LPoint3 &get_center() const;
64 INLINE const LPoint3 &get_min() const;
65 INLINE const LPoint3 &get_max() const;
66 INLINE LVector3 get_dimensions() const;
67
68PUBLISHED:
69 MAKE_PROPERTY(center, get_center);
70 MAKE_PROPERTY(min, get_min);
71 MAKE_PROPERTY(max, get_max);
72 MAKE_PROPERTY(dimensions, get_dimensions);
73
74protected:
75 virtual PT(BoundingVolume) compute_internal_bounds() const;
76 virtual PT(CollisionEntry)
77 test_intersection_from_sphere(const CollisionEntry &entry) const;
78 virtual PT(CollisionEntry)
79 test_intersection_from_line(const CollisionEntry &entry) const;
80 virtual PT(CollisionEntry)
81 test_intersection_from_ray(const CollisionEntry &entry) const;
82 virtual PT(CollisionEntry)
83 test_intersection_from_segment(const CollisionEntry &entry) const;
84 virtual PT(CollisionEntry)
85 test_intersection_from_capsule(const CollisionEntry &entry) const;
86 virtual PT(CollisionEntry)
87 test_intersection_from_box(const CollisionEntry &entry) const;
88
89 virtual void fill_viz_geom();
90
91protected:
92 bool intersects_line(double &t1, double &t2,
93 const LPoint3 &from, const LVector3 &delta,
94 PN_stdfloat inflate_size=0) const;
95
96private:
97 LPoint3 _center;
98 LPoint3 _min;
99 LPoint3 _max;
100 PN_stdfloat _x, _y, _z, _radius;
101 LPoint3 _vertex[8]; // Each of the Eight Vertices of the Box
102 LPlane _planes[6]; //Points to each of the six sides of the Box
103
104 static const int plane_def[6][4];
105
106 static PStatCollector _volume_pcollector;
107 static PStatCollector _test_pcollector;
108
109private:
110 INLINE static bool is_right(const LVector2 &v1, const LVector2 &v2);
111 INLINE static PN_stdfloat dist_to_line(const LPoint2 &p,
112 const LPoint2 &f, const LVector2 &v);
113 static PN_stdfloat dist_to_line_segment(const LPoint2 &p,
114 const LPoint2 &f, const LPoint2 &t,
115 const LVector2 &v);
116
117public:
118 class PointDef {
119 public:
120 INLINE PointDef(const LPoint2 &p, const LVector2 &v);
121 INLINE PointDef(PN_stdfloat x, PN_stdfloat y);
122 INLINE PointDef(const PointDef &copy);
123 INLINE void operator = (const PointDef &copy);
124
125 LPoint2 _p; // the point in 2-d space
126 LVector2 _v; // the normalized vector to the next point
127 };
128 typedef pvector<PointDef> Points;
129
130 static void compute_vectors(Points &points);
131 void draw_polygon(GeomNode *viz_geom_node, GeomNode *bounds_viz_geom_node,
132 const Points &points) const;
133
134 bool point_is_inside(const LPoint2 &p, const Points &points) const;
135 PN_stdfloat dist_to_polygon(const LPoint2 &p, const Points &points) const;
136
137 void setup_points(const LPoint3 *begin, const LPoint3 *end, int plane);
138 INLINE LPoint2 to_2d(const LVecBase3 &point3d, int plane) const;
139 INLINE void calc_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const;
140 INLINE void rederive_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const;
141 INLINE static LPoint3 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat);
142 LPoint3 legacy_to_3d(const LVecBase2 &point2d, int axis) const;
143 bool clip_polygon(Points &new_points, const Points &source_points,
144 const LPlane &plane,int plane_no) const;
145 bool apply_clip_plane(Points &new_points, const ClipPlaneAttrib *cpa,
146 const TransformState *net_transform, int plane_no) const;
147
148private:
149 Points _points[6]; // one set of points for each of the six planes that make up the box
150 LMatrix4 _to_2d_mat[6];
151
152public:
153 INLINE Points get_plane_points( int n );
154
155public:
156 static void register_with_read_factory();
157 virtual void write_datagram(BamWriter *manager, Datagram &me);
158
159protected:
160 static TypedWritable *make_CollisionBox(const FactoryParams &params);
161 void fillin(DatagramIterator &scan, BamReader *manager);
162
163public:
164 static TypeHandle get_class_type() {
165 return _type_handle;
166 }
167 static void init_type() {
168 CollisionSolid::init_type();
169 register_type(_type_handle, "CollisionBox",
170 CollisionSolid::get_class_type());
171 }
172 virtual TypeHandle get_type() const {
173 return get_class_type();
174 }
175 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
176
177private:
178 static TypeHandle _type_handle;
179};
180
181#include "collisionBox.I"
182
183#endif /* COLLISIONBOX_H */
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
This functions similarly to a LightAttrib.
static void register_with_read_factory()
Factory method to generate a CollisionBox object.
PN_stdfloat dist_to_polygon(const LPoint2 &p, const Points &points) const
Returns the linear distance from the 2-d point to the nearest part of the polygon defined by the poin...
void setup_points(const LPoint3 *begin, const LPoint3 *end, int plane)
Computes the plane and 2d projection of points that make up this side.
static void flush_level()
Flushes the PStatCollectors used during traversal.
bool apply_clip_plane(Points &new_points, const ClipPlaneAttrib *cpa, const TransformState *net_transform, int plane_no) const
Clips the polygon by all of the clip planes named in the clip plane attribute and fills new_points up...
CollisionBox(const LPoint3 &center, PN_stdfloat x, PN_stdfloat y, PN_stdfloat z)
Create the Box by giving a Center and distances of each of the sides of box from the Center.
bool clip_polygon(Points &new_points, const Points &source_points, const LPlane &plane, int plane_no) const
Clips the source_points of the polygon by the indicated clipping plane, and modifies new_points to re...
Points get_plane_points(int n)
returns the points that form the nth plane
LPoint3 get_point_aabb(int n) const
Returns the nth vertex of the Axis Aligned Bounding Box.
virtual PointerTo< CollisionEntry > test_intersection(const CollisionEntry &entry) const
First Dispatch point for box as a FROM object.
void calc_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const
Fills the indicated matrix with the appropriate rotation transform to move points from the 2-d plane ...
static void compute_vectors(Points &points)
Now that the _p members of the given points array have been computed, go back and compute all of the ...
void setup_box()
Compute parameters for each of the box's sides.
LPlane get_plane(int n) const
Returns the nth face of the rectangular solid.
bool point_is_inside(const LPoint2 &p, const Points &points) const
Returns true if the indicated point is within the polygon's 2-d space, false otherwise.
int get_num_points() const
Returns 8: the number of vertices of a rectangular solid.
LPoint2 to_2d(const LVecBase3 &point3d, int plane) const
Assuming the indicated point in 3-d space lies within the polygon's plane, returns the corresponding ...
LPoint3 get_point(int n) const
Returns the nth vertex of the OBB.
virtual LPoint3 get_collision_origin() const
Returns the point in space deemed to be the "origin" of the solid for collision purposes.
void rederive_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const
Fills the indicated matrix with the appropriate rotation transform to move points from the 2-d plane ...
static LPoint3 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat)
Extrude the indicated point in the polygon's 2-d definition space back into 3-d coordinates.
int get_num_planes() const
Returns 6: the number of faces of a rectangular solid.
LPlane set_plane(int n) const
Creates the nth face of the rectangular solid.
Defines a single collision event.
virtual void write_datagram(BamWriter *manager, Datagram &me)
Function to write the important information in the particular object to a Datagram.
virtual PStatCollector & get_test_pcollector()
Returns a PStatCollector that is used to count the number of intersection tests made against a solid ...
virtual void xform(const LMatrix4 &mat)
Transforms the solid by the indicated matrix.
virtual PStatCollector & get_volume_pcollector()
Returns a PStatCollector that is used to count the number of bounding volume tests made against a sol...
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...
A node that holds Geom objects, renderable pieces of geometry.
Definition geomNode.h:34
A lightweight class that represents a single element that may be timed and/or counted via stats.
Indicates a coordinate-system transform on vertices.
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.
bool is_right(const LVector2 &v1, const LVector2 &v2)
Returns true if the 2-d v1 is to the right of v2.
Definition pgItem.cxx:51
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...