Panda3D
bulletMultiSphereShape.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 bulletMultiSphereShape.cxx
10  * @author enn0x
11  * @date 2012-01-05
12  */
13 
14 #include "bulletMultiSphereShape.h"
15 
16 #include "bulletWorld.h"
17 
18 #include "geomVertexReader.h"
19 
20 TypeHandle BulletMultiSphereShape::_type_handle;
21 
22 /**
23  *
24  */
25 BulletMultiSphereShape::
26 BulletMultiSphereShape(const PTA_LVecBase3 &points, const PTA_stdfloat &radii) {
27 
28  int num_spheres = std::min(points.size(), radii.size());
29 
30  // Convert points
31  btVector3 *bt_points = new btVector3[num_spheres];
32  for (int i=0; i<num_spheres; i++) {
33  bt_points[i] = LVecBase3_to_btVector3(points[i]);
34  }
35 
36  // Convert radii
37  btScalar *bt_radii = new btScalar[num_spheres];
38  for (int j=0; j<num_spheres; j++) {
39  bt_radii[j] = (PN_stdfloat)radii[j];
40  }
41 
42  // Create shape
43  _shape = new btMultiSphereShape(bt_points, bt_radii, num_spheres);
44  _shape->setUserPointer(this);
45 }
46 
47 /**
48  *
49  */
50 BulletMultiSphereShape::
51 BulletMultiSphereShape(const BulletMultiSphereShape &copy) {
52  LightMutexHolder holder(BulletWorld::get_global_lock());
53 
54  _shape = copy._shape;
55 }
56 
57 /**
58  *
59  */
60 void BulletMultiSphereShape::
61 operator = (const BulletMultiSphereShape &copy) {
62  LightMutexHolder holder(BulletWorld::get_global_lock());
63 
64  _shape = copy._shape;
65 }
66 
67 /**
68  *
69  */
70 btCollisionShape *BulletMultiSphereShape::
71 ptr() const {
72 
73  return _shape;
74 }
75 
76 /**
77  *
78  */
79 int BulletMultiSphereShape::
80 get_sphere_count() const {
81  LightMutexHolder holder(BulletWorld::get_global_lock());
82 
83  return _shape->getSphereCount();
84 }
85 
86 /**
87  *
88  */
89 LPoint3 BulletMultiSphereShape::
90 get_sphere_pos(int index) const {
91  LightMutexHolder holder(BulletWorld::get_global_lock());
92 
93  nassertr(index >=0 && index <_shape->getSphereCount(), LPoint3::zero());
94  return btVector3_to_LPoint3(_shape->getSpherePosition(index));
95 }
96 
97 /**
98  *
99  */
100 PN_stdfloat BulletMultiSphereShape::
101 get_sphere_radius(int index) const {
102  LightMutexHolder holder(BulletWorld::get_global_lock());
103 
104  nassertr(index >=0 && index <_shape->getSphereCount(), 0.0);
105  return (PN_stdfloat)_shape->getSphereRadius(index);
106 }
107 
108 /**
109  * Tells the BamReader how to create objects of type BulletShape.
110  */
113  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
114 }
115 
116 /**
117  * Writes the contents of this object to the datagram for shipping out to a
118  * Bam file.
119  */
122  BulletShape::write_datagram(manager, dg);
123  dg.add_stdfloat(get_margin());
124 
125  // parameters to serialize: sphere count, points, radii
126  dg.add_int32(get_sphere_count());
127  for (int i = 0; i < get_sphere_count(); ++i){
128  get_sphere_pos(i).write_datagram(dg);
129  }
130 
131  for (int i = 0; i < get_sphere_count(); ++i){
132  dg.add_stdfloat(get_sphere_radius(i));
133  }
134 }
135 
136 /**
137  * This function is called by the BamReader's factory when a new object of
138  * type BulletShape is encountered in the Bam file. It should create the
139  * BulletShape and extract its information from the file.
140  */
141 TypedWritable *BulletMultiSphereShape::
142 make_from_bam(const FactoryParams &params) {
143  // create a default BulletMultiSphereShape
145  DatagramIterator scan;
146  BamReader *manager;
147 
148  parse_params(params, scan, manager);
149  param->fillin(scan, manager);
150 
151  return param;
152 }
153 
154 /**
155  * This internal function is called by make_from_bam to read in all of the
156  * relevant data from the BamFile for the new BulletShape.
157  */
158 void BulletMultiSphereShape::
159 fillin(DatagramIterator &scan, BamReader *manager) {
160  BulletShape::fillin(scan, manager);
161  nassertv(_shape == nullptr);
162 
163  PN_stdfloat margin = scan.get_stdfloat();
164 
165  // parameters to serialize: sphere count, points, radii
166  int sphereCount = scan.get_int32();
167  btVector3 *positions = new btVector3[sphereCount];
168  for (int i = 0; i < sphereCount; ++i){
169  LVector3 pos;
170  pos.read_datagram(scan);
171  positions[i] = LVecBase3_to_btVector3(pos);
172  }
173 
174  btScalar *radii = new btScalar[sphereCount];
175  for (int i = 0; i < sphereCount; ++i){
176  radii[i] = scan.get_stdfloat();
177  }
178 
179  _shape = new btMultiSphereShape(positions, radii, sphereCount);
180  _shape->setUserPointer(this);
181  _shape->setMargin(margin);
182 }
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
static void register_with_read_factory()
Tells the BamReader how to create objects of type BulletShape.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int32_t get_int32()
Extracts a signed 32-bit integer.
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.
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
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
Similar to MutexHolder, but for a light mutex.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
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.
void add_int32(int32_t value)
Adds a signed 32-bit integer to the datagram.
Definition: datagram.I:67
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
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
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38