Panda3D
Loading...
Searching...
No Matches
bulletConvexHullShape.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 bulletConvexHullShape.cxx
10 * @author enn0x
11 * @date 2010-01-26
12 */
13
15
16#include "bulletWorld.h"
17
18#include "nodePathCollection.h"
19#include "geomNode.h"
20#include "geomVertexReader.h"
21
22TypeHandle BulletConvexHullShape::_type_handle;
23
24/**
25 *
26 */
27BulletConvexHullShape::
28BulletConvexHullShape() {
29
30 _shape = new btConvexHullShape(nullptr, 0);
31 _shape->setUserPointer(this);
32}
33
34/**
35 *
36 */
37BulletConvexHullShape::
38BulletConvexHullShape(const BulletConvexHullShape &copy) {
39 LightMutexHolder holder(BulletWorld::get_global_lock());
40
41 _shape = new btConvexHullShape(nullptr, 0);
42 _shape->setUserPointer(this);
43
44#if BT_BULLET_VERSION >= 282
45 for (int i = 0; i < copy._shape->getNumPoints(); ++i) {
46 _shape->addPoint(copy._shape->getUnscaledPoints()[i], false);
47 }
48 _shape->recalcLocalAabb();
49#else
50 for (int i = 0; i < copy._shape->getNumPoints(); ++i) {
51 _shape->addPoint(copy._shape->getUnscaledPoints()[i]);
52 }
53#endif
54}
55
56/**
57 *
58 */
59btCollisionShape *BulletConvexHullShape::
60ptr() const {
61
62 return _shape;
63}
64
65/**
66 *
67 */
68void BulletConvexHullShape::
69add_point(const LPoint3 &p) {
70 LightMutexHolder holder(BulletWorld::get_global_lock());
71
72 _shape->addPoint(LVecBase3_to_btVector3(p));
73}
74
75/**
76 *
77 */
78void BulletConvexHullShape::
79add_array(const PTA_LVecBase3 &points) {
80 LightMutexHolder holder(BulletWorld::get_global_lock());
81
82 if (_shape)
83 delete _shape;
84
85 _shape = new btConvexHullShape(nullptr, 0);
86 _shape->setUserPointer(this);
87
88 PTA_LVecBase3::const_iterator it;
89
90#if BT_BULLET_VERSION >= 282
91 for (it = points.begin(); it != points.end(); ++it) {
92 _shape->addPoint(LVecBase3_to_btVector3(*it), false);
93 }
94 _shape->recalcLocalAabb();
95#else
96 for (it = points.begin(); it != points.end(); ++it) {
97 _shape->addPoint(LVecBase3_to_btVector3(*it));
98 }
99#endif
100}
101
102/**
103 *
104 */
105void BulletConvexHullShape::
106add_geom(const Geom *geom, const TransformState *ts) {
107 LightMutexHolder holder(BulletWorld::get_global_lock());
108
109 nassertv(geom);
110 nassertv(ts);
111
112 LMatrix4 m = ts->get_mat();
113
114 // Collect points
115 pvector<LPoint3> points;
116
117 CPT(GeomVertexData) vdata = geom->get_vertex_data();
118 GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex());
119
120 while (!reader.is_at_end()) {
121 points.push_back(m.xform_point(reader.get_data3()));
122 }
123
124 if (_shape)
125 delete _shape;
126
127 // Create shape
128 _shape = new btConvexHullShape(nullptr, 0);
129 _shape->setUserPointer(this);
130
132
133#if BT_BULLET_VERSION >= 282
134 for (it = points.begin(); it != points.end(); ++it) {
135 _shape->addPoint(LVecBase3_to_btVector3(*it), false);
136 }
137 _shape->recalcLocalAabb();
138#else
139 for (it = points.begin(); it != points.end(); ++it) {
140 _shape->addPoint(LVecBase3_to_btVector3(*it));
141 }
142#endif
143}
144
145/**
146 * Tells the BamReader how to create objects of type BulletShape.
147 */
150 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
151}
152
153/**
154 * Writes the contents of this object to the datagram for shipping out to a
155 * Bam file.
156 */
158write_datagram(BamWriter *manager, Datagram &dg) {
159 BulletShape::write_datagram(manager, dg);
160 dg.add_stdfloat(get_margin());
161
162 unsigned int num_points = _shape->getNumPoints();
163 dg.add_uint32(num_points);
164
165 const btVector3 *points = _shape->getUnscaledPoints();
166
167 for (unsigned int i = 0; i < num_points; ++i) {
168 LVecBase3 point = btVector3_to_LVecBase3(points[i]);
169 point.write_datagram(dg);
170 }
171}
172
173/**
174 * This function is called by the BamReader's factory when a new object of
175 * type BulletShape is encountered in the Bam file. It should create the
176 * BulletShape and extract its information from the file.
177 */
178TypedWritable *BulletConvexHullShape::
179make_from_bam(const FactoryParams &params) {
181 DatagramIterator scan;
182 BamReader *manager;
183
184 parse_params(params, scan, manager);
185 param->fillin(scan, manager);
186
187 return param;
188}
189
190/**
191 * This internal function is called by make_from_bam to read in all of the
192 * relevant data from the BamFile for the new BulletShape.
193 */
194void BulletConvexHullShape::
195fillin(DatagramIterator &scan, BamReader *manager) {
196 BulletShape::fillin(scan, manager);
197 nassertv(_shape);
198
199 _shape->setMargin(scan.get_stdfloat());
200 unsigned int num_points = scan.get_uint32();
201
202#if BT_BULLET_VERSION >= 282
203 for (unsigned int i = 0; i < num_points; ++i) {
204 LVecBase3 point;
205 point.read_datagram(scan);
206 _shape->addPoint(LVecBase3_to_btVector3(point), false);
207 }
208 _shape->recalcLocalAabb();
209#else
210 for (unsigned int i = 0; i < num_points; ++i) {
211 LVecBase3 point;
212 point.read_datagram(scan);
213 _shape->addPoint(LVecBase3_to_btVector3(point));
214 }
215#endif
216}
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.
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
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
static void register_with_read_factory()
Tells the BamReader how to create objects of type BulletShape.
A class to retrieve the individual data elements previously stored in a Datagram.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the datagram.
Definition datagram.I:94
void add_stdfloat(PN_stdfloat value)
Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().
Definition datagram.I:133
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
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
This object provides a high-level interface for quickly reading a sequence of numeric values from a v...
bool is_at_end() const
Returns true if the reader is currently at the end of the list of vertices, false otherwise.
const LVecBase3 & get_data3()
Returns the data associated with the read row, expressed as a 3-component value, and advances the rea...
A container for geometry primitives.
Definition geom.h:54
Similar to MutexHolder, but for a light mutex.
Indicates a coordinate-system transform on vertices.
get_mat
Returns the matrix that describes the transform.
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.
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
This is our own Panda specialization on the default STL vector.
Definition pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.