Panda3D
bulletCapsuleShape.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 bulletCapsuleShape.cxx
10  * @author enn0x
11  * @date 2010-01-27
12  */
13 
14 #include "bulletCapsuleShape.h"
15 
16 #include "config_bullet.h"
17 
18 TypeHandle BulletCapsuleShape::_type_handle;
19 
20 /**
21  *
22  */
23 BulletCapsuleShape::
24 BulletCapsuleShape(PN_stdfloat radius, PN_stdfloat height, BulletUpAxis up) :
25  _radius(radius),
26  _height(height),
27  _up(up) {
28 
29  switch (up) {
30  case X_up:
31  _shape = new btCapsuleShapeX(radius, height);
32  break;
33  case Y_up:
34  _shape = new btCapsuleShape(radius, height);
35  break;
36  case Z_up:
37  _shape = new btCapsuleShapeZ(radius, height);
38  break;
39  default:
40  bullet_cat.error() << "invalid up-axis:" << up << std::endl;
41  break;
42  }
43 
44  nassertv(_shape);
45  _shape->setUserPointer(this);
46 }
47 
48 /**
49  *
50  */
51 BulletCapsuleShape::
52 BulletCapsuleShape(const BulletCapsuleShape &copy) {
53  LightMutexHolder holder(BulletWorld::get_global_lock());
54 
55  _radius = copy._radius;
56  _height = copy._height;
57  _up = copy._up;
58 
59  switch (_up) {
60  case X_up:
61  _shape = new btCapsuleShapeX(_radius, _height);
62  break;
63  case Y_up:
64  _shape = new btCapsuleShape(_radius, _height);
65  break;
66  case Z_up:
67  _shape = new btCapsuleShapeZ(_radius, _height);
68  break;
69  default:
70  bullet_cat.error() << "invalid up-axis:" << _up << std::endl;
71  break;
72  }
73 
74  nassertv(_shape);
75  _shape->setUserPointer(this);
76 }
77 
78 /**
79  *
80  */
81 btCollisionShape *BulletCapsuleShape::
82 ptr() const {
83 
84  return _shape;
85 }
86 
87 
88 /**
89  * Constructs a new BulletCapsuleShape using the information from a
90  * CollisionCapsule from the builtin collision system.
91  */
94 
95  PN_stdfloat radius = solid->get_radius();
96  // Get capsule's cylinder height: length from point A to point B
97  PN_stdfloat height = (solid->get_point_b() - solid->get_point_a()).length();
98 
99  // CollisionCapsules are always Z-Up.
100  return new BulletCapsuleShape(radius, height, Z_up);
101 }
102 
103 /**
104  * Tells the BamReader how to create objects of type BulletShape.
105  */
108  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
109 }
110 
111 /**
112  * Writes the contents of this object to the datagram for shipping out to a
113  * Bam file.
114  */
117  BulletShape::write_datagram(manager, dg);
118  dg.add_stdfloat(get_margin());
119 
120  // parameters to serialize: radius, height, up
121  dg.add_stdfloat(_radius);
122  dg.add_stdfloat(_height);
123  dg.add_int8((int8_t)_shape->getUpAxis());
124 }
125 
126 /**
127  * This function is called by the BamReader's factory when a new object of
128  * type BulletShape is encountered in the Bam file. It should create the
129  * BulletShape and extract its information from the file.
130  */
131 TypedWritable *BulletCapsuleShape::
132 make_from_bam(const FactoryParams &params) {
133  // create a default BulletCapsuleShape
135  DatagramIterator scan;
136  BamReader *manager;
137 
138  parse_params(params, scan, manager);
139  param->fillin(scan, manager);
140 
141  return param;
142 }
143 
144 /**
145  * This internal function is called by make_from_bam to read in all of the
146  * relevant data from the BamFile for the new BulletShape.
147  */
148 void BulletCapsuleShape::
149 fillin(DatagramIterator &scan, BamReader *manager) {
150  BulletShape::fillin(scan, manager);
151  nassertv(_shape == nullptr);
152 
153  PN_stdfloat margin = scan.get_stdfloat();
154 
155  // parameters to serialize: radius, height, up
156  _radius = scan.get_stdfloat();
157  _height = scan.get_stdfloat();
158  _up = (BulletUpAxis) scan.get_int8();
159 
160  switch (_up) {
161  case X_up:
162  _shape = new btCapsuleShapeX(_radius, _height);
163  break;
164  case Y_up:
165  _shape = new btCapsuleShape(_radius, _height);
166  break;
167  case Z_up:
168  _shape = new btCapsuleShapeZ(_radius, _height);
169  break;
170  default:
171  bullet_cat.error() << "invalid up-axis:" << _up << std::endl;
172  break;
173  }
174 
175  nassertv(_shape);
176  _shape->setUserPointer(this);
177  _shape->setMargin(margin);
178 }
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
This implements a solid consisting of a cylinder with hemispherical endcaps, also known as a capsule ...
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.
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 WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
static BulletCapsuleShape * make_from_solid(const CollisionCapsule *solid)
Constructs a new BulletCapsuleShape using the information from a CollisionCapsule from the builtin co...
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
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.