Panda3D
bulletBoxShape.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 bulletBoxShape.cxx
10  * @author enn0x
11  * @date 2010-01-24
12  */
13 
14 #include "bulletBoxShape.h"
15 
16 #include "bulletWorld.h"
17 
18 #include "bullet_utils.h"
19 
20 TypeHandle BulletBoxShape::_type_handle;
21 
22 /**
23  *
24  */
25 BulletBoxShape::
26 BulletBoxShape(const LVecBase3 &halfExtents) : _half_extents(halfExtents) {
27 
28  btVector3 btHalfExtents = LVecBase3_to_btVector3(halfExtents);
29 
30  _shape = new btBoxShape(btHalfExtents);
31  _shape->setUserPointer(this);
32 }
33 
34 /**
35  *
36  */
37 BulletBoxShape::
38 BulletBoxShape(const BulletBoxShape &copy) {
39  LightMutexHolder holder(BulletWorld::get_global_lock());
40 
41  _half_extents = copy._half_extents;
42  btVector3 btHalfExtents = LVecBase3_to_btVector3(_half_extents);
43 
44  _shape = new btBoxShape(btHalfExtents);
45  _shape->setUserPointer(this);
46 }
47 
48 /**
49  *
50  */
51 btCollisionShape *BulletBoxShape::
52 ptr() const {
53 
54  return _shape;
55 }
56 
57 /**
58  *
59  */
60 LVecBase3 BulletBoxShape::
61 get_half_extents_without_margin() const {
62  LightMutexHolder holder(BulletWorld::get_global_lock());
63 
64  return btVector3_to_LVecBase3(_shape->getHalfExtentsWithoutMargin());
65 }
66 
67 /**
68  *
69  */
70 LVecBase3 BulletBoxShape::
71 get_half_extents_with_margin() const {
72  LightMutexHolder holder(BulletWorld::get_global_lock());
73 
74  return btVector3_to_LVecBase3(_shape->getHalfExtentsWithMargin());
75 }
76 
77 /**
78  *
79  */
80 BulletBoxShape *BulletBoxShape::
81 make_from_solid(const CollisionBox *solid) {
82 
83  LPoint3 p0 = solid->get_min();
84  LPoint3 p1 = solid->get_max();
85 
86  LVecBase3 extents((p1.get_x() - p0.get_x()) / 2.0,
87  (p1.get_y() - p0.get_y()) / 2.0,
88  (p1.get_z() - p0.get_z()) / 2.0);
89 
90  return new BulletBoxShape(extents);
91 }
92 
93 /**
94  * Tells the BamReader how to create objects of type BulletShape.
95  */
98  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
99 }
100 
101 /**
102  * Writes the contents of this object to the datagram for shipping out to a
103  * Bam file.
104  */
106 write_datagram(BamWriter *manager, Datagram &dg) {
107  BulletShape::write_datagram(manager, dg);
108  dg.add_stdfloat(get_margin());
109  _half_extents.write_datagram(dg);
110 }
111 
112 /**
113  * This function is called by the BamReader's factory when a new object of
114  * type BulletShape is encountered in the Bam file. It should create the
115  * BulletShape and extract its information from the file.
116  */
117 TypedWritable *BulletBoxShape::
118 make_from_bam(const FactoryParams &params) {
119  BulletBoxShape *param = new BulletBoxShape;
120  DatagramIterator scan;
121  BamReader *manager;
122 
123  parse_params(params, scan, manager);
124  param->fillin(scan, manager);
125 
126  return param;
127 }
128 
129 /**
130  * This internal function is called by make_from_bam to read in all of the
131  * relevant data from the BamFile for the new BulletShape.
132  */
133 void BulletBoxShape::
134 fillin(DatagramIterator &scan, BamReader *manager) {
135  BulletShape::fillin(scan, manager);
136  nassertv(_shape == nullptr);
137 
138  PN_stdfloat margin = scan.get_stdfloat();
139 
140  _half_extents.read_datagram(scan);
141 
142  _shape = new btBoxShape(LVecBase3_to_btVector3(_half_extents));
143  _shape->setUserPointer(this);
144  _shape->setMargin(margin);
145 }
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.
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 cuboid collision volume or object.
Definition: collisionBox.h:27
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...
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
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...
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
Similar to MutexHolder, but for a light mutex.
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.
Definition: typedWritable.h:35
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.