Panda3D
bulletCylinderShape.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 bulletCylinderShape.cxx
10  * @author enn0x
11  * @date 2010-02-17
12  */
13 
14 #include "bulletCylinderShape.h"
15 
16 #include "config_bullet.h"
17 
18 using std::endl;
19 
20 TypeHandle BulletCylinderShape::_type_handle;
21 
22 /**
23  *
24  */
25 BulletCylinderShape::
26 BulletCylinderShape(const LVector3 &half_extents, BulletUpAxis up) :
27  _half_extents(half_extents),
28  _up(up) {
29 
30  btVector3 btHalfExtents = LVecBase3_to_btVector3(half_extents);
31 
32  switch (up) {
33  case X_up:
34  _shape = new btCylinderShapeX(btHalfExtents);
35  break;
36  case Y_up:
37  _shape = new btCylinderShape(btHalfExtents);
38  break;
39  case Z_up:
40  _shape = new btCylinderShapeZ(btHalfExtents);
41  break;
42  default:
43  bullet_cat.error() << "invalid up-axis:" << up << endl;
44  break;
45  }
46 
47  nassertv(_shape);
48  _shape->setUserPointer(this);
49 }
50 
51 /**
52  *
53  */
54 BulletCylinderShape::
55 BulletCylinderShape(PN_stdfloat radius, PN_stdfloat height, BulletUpAxis up) :
56  _up(up) {
57 
58  switch (up) {
59  case X_up:
60  _shape = new btCylinderShapeX(btVector3(0.5 * height, radius, radius));
61  _half_extents = btVector3_to_LVector3(btVector3(0.5 * height, radius, radius));
62  break;
63  case Y_up:
64  _shape = new btCylinderShape(btVector3(radius, 0.5 * height, radius));
65  _half_extents = btVector3_to_LVector3(btVector3(radius, 0.5 * height, radius));
66  break;
67  case Z_up:
68  _shape = new btCylinderShapeZ(btVector3(radius, radius, 0.5 * height));
69  _half_extents = btVector3_to_LVector3(btVector3(radius, radius, 0.5 * height));
70  break;
71  default:
72  bullet_cat.error() << "invalid up-axis:" << up << endl;
73  break;
74  }
75 
76  nassertv(_shape);
77  _shape->setUserPointer(this);
78 }
79 
80 /**
81  *
82  */
83 BulletCylinderShape::
84 BulletCylinderShape(const BulletCylinderShape &copy) {
85  LightMutexHolder holder(BulletWorld::get_global_lock());
86 
87  _up = copy._up;
88  _half_extents = copy._half_extents;
89 
90  btVector3 btHalfExtents = LVecBase3_to_btVector3(_half_extents);
91 
92  switch (_up) {
93  case X_up:
94  _shape = new btCylinderShapeX(btHalfExtents);
95  break;
96  case Y_up:
97  _shape = new btCylinderShape(btHalfExtents);
98  break;
99  case Z_up:
100  _shape = new btCylinderShapeZ(btHalfExtents);
101  break;
102  default:
103  bullet_cat.error() << "invalid up-axis:" << _up << endl;
104  break;
105  }
106 
107  nassertv(_shape);
108  _shape->setUserPointer(this);
109 }
110 
111 /**
112  *
113  */
114 btCollisionShape *BulletCylinderShape::
115 ptr() const {
116 
117  return _shape;
118 }
119 
120 /**
121  *
122  */
123 PN_stdfloat BulletCylinderShape::
124 get_radius() const {
125  LightMutexHolder holder(BulletWorld::get_global_lock());
126 
127  return (PN_stdfloat)_shape->getRadius();
128 }
129 
130 /**
131  *
132  */
133 LVecBase3 BulletCylinderShape::
134 get_half_extents_without_margin() const {
135  LightMutexHolder holder(BulletWorld::get_global_lock());
136 
137  return btVector3_to_LVecBase3(_shape->getHalfExtentsWithoutMargin());
138 }
139 
140 /**
141  *
142  */
143 LVecBase3 BulletCylinderShape::
144 get_half_extents_with_margin() const {
145  LightMutexHolder holder(BulletWorld::get_global_lock());
146 
147  return btVector3_to_LVecBase3(_shape->getHalfExtentsWithMargin());
148 }
149 
150 /**
151  * Tells the BamReader how to create objects of type BulletShape.
152  */
155  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
156 }
157 
158 /**
159  * Writes the contents of this object to the datagram for shipping out to a
160  * Bam file.
161  */
164  BulletShape::write_datagram(manager, dg);
165  dg.add_stdfloat(get_margin());
166 
167  // parameters to serialize: radius, height, up
168  _half_extents.write_datagram(dg);
169  dg.add_int8((int8_t)_up);
170 }
171 
172 /**
173  * This function is called by the BamReader's factory when a new object of
174  * type BulletShape is encountered in the Bam file. It should create the
175  * BulletShape and extract its information from the file.
176  */
177 TypedWritable *BulletCylinderShape::
178 make_from_bam(const FactoryParams &params) {
179  // create a default BulletCylinderShape
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  */
194 void BulletCylinderShape::
195 fillin(DatagramIterator &scan, BamReader *manager) {
196  BulletShape::fillin(scan, manager);
197  nassertv(_shape == nullptr);
198 
199  PN_stdfloat margin = scan.get_stdfloat();
200 
201  // parameters to serialize: radius, height, up
202  _half_extents.read_datagram(scan);
203  _up = (BulletUpAxis) scan.get_int8();
204 
205  btVector3 btHalfExtents = LVecBase3_to_btVector3(_half_extents);
206 
207  switch (_up) {
208  case X_up:
209  _shape = new btCylinderShapeX(btHalfExtents);
210  break;
211  case Y_up:
212  _shape = new btCylinderShape(btHalfExtents);
213  break;
214  case Z_up:
215  _shape = new btCylinderShapeZ(btHalfExtents);
216  break;
217  default:
218  bullet_cat.error() << "invalid up-axis:" << _up << endl;
219  break;
220  }
221 
222  nassertv(_shape);
223  _shape->setUserPointer(this);
224  _shape->setMargin(margin);
225 }
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
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
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
void add_int8(int8_t value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:42
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
static void register_with_read_factory()
Tells the BamReader how to create objects of type BulletShape.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
A class to retrieve the individual data elements previously stored in a Datagram.
int8_t get_int8()
Extracts a signed 8-bit integer.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.