Panda3D
bulletPlaneShape.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 bulletPlaneShape.cxx
10  * @author enn0x
11  * @date 2010-01-23
12  */
13 
14 #include "bulletPlaneShape.h"
15 
16 #include "bulletWorld.h"
17 
18 TypeHandle BulletPlaneShape::_type_handle;
19 
20 /**
21  * Creates a plane shape from a plane definition.
22  */
23 BulletPlaneShape::
24 BulletPlaneShape(LPlane plane) {
25 
26  btVector3 btNormal = LVecBase3_to_btVector3(plane.get_normal());
27 
28  _shape = new btStaticPlaneShape(btNormal, plane.get_w());
29  _shape->setUserPointer(this);
30 }
31 
32 /**
33  *
34  */
35 BulletPlaneShape::
36 BulletPlaneShape(const LVector3 &normal, PN_stdfloat constant) {
37 
38  btVector3 btNormal = LVecBase3_to_btVector3(normal);
39 
40  _shape = new btStaticPlaneShape(btNormal, constant);
41  _shape->setUserPointer(this);
42 }
43 
44 /**
45  *
46  */
47 BulletPlaneShape::
48 BulletPlaneShape(const BulletPlaneShape &copy) {
49  LightMutexHolder holder(BulletWorld::get_global_lock());
50 
51  btVector3 btNormal = copy._shape->getPlaneNormal();
52  PN_stdfloat constant = (PN_stdfloat)_shape->getPlaneConstant();
53 
54  _shape = new btStaticPlaneShape(btNormal, constant);
55  _shape->setUserPointer(this);
56 }
57 
58 /**
59  *
60  */
61 btCollisionShape *BulletPlaneShape::
62 ptr() const {
63 
64  return _shape;
65 }
66 
67 /**
68  *
69  */
70 LPlane BulletPlaneShape::
71 get_plane() const {
72  LightMutexHolder holder(BulletWorld::get_global_lock());
73 
74  btVector3 normal = _shape->getPlaneNormal();
75  return LPlane(normal[0], normal[1], normal[2], (PN_stdfloat)_shape->getPlaneConstant());
76 }
77 
78 /**
79  *
80  */
81 PN_stdfloat BulletPlaneShape::
82 get_plane_constant() const {
83  LightMutexHolder holder(BulletWorld::get_global_lock());
84 
85  return (PN_stdfloat)_shape->getPlaneConstant();
86 }
87 
88 /**
89  *
90  */
91 LVector3 BulletPlaneShape::
92 get_plane_normal() const {
93  LightMutexHolder holder(BulletWorld::get_global_lock());
94 
95  return btVector3_to_LVector3(_shape->getPlaneNormal());
96 }
97 
98 /**
99  *
100  */
101 BulletPlaneShape *BulletPlaneShape::
102 make_from_solid(const CollisionPlane *solid) {
103 
104  LVector3 normal = solid->get_normal();
105  PN_stdfloat constant = solid->dist_to_plane(LPoint3(0, 0, 0));
106 
107  return new BulletPlaneShape(normal, constant);
108 }
109 
110 /**
111  * Tells the BamReader how to create objects of type BulletShape.
112  */
115  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
116 }
117 
118 /**
119  * Writes the contents of this object to the datagram for shipping out to a
120  * Bam file.
121  */
124  BulletShape::write_datagram(manager, dg);
125 
126  dg.add_stdfloat(get_margin());
127  get_plane_normal().write_datagram(dg);
128  dg.add_stdfloat(get_plane_constant());
129 }
130 
131 /**
132  * This function is called by the BamReader's factory when a new object of
133  * type BulletShape is encountered in the Bam file. It should create the
134  * BulletShape and extract its information from the file.
135  */
136 TypedWritable *BulletPlaneShape::
137 make_from_bam(const FactoryParams &params) {
138  BulletPlaneShape *param = new BulletPlaneShape;
139  DatagramIterator scan;
140  BamReader *manager;
141 
142  parse_params(params, scan, manager);
143  param->fillin(scan, manager);
144 
145  return param;
146 }
147 
148 /**
149  * This internal function is called by make_from_bam to read in all of the
150  * relevant data from the BamFile for the new BulletShape.
151  */
152 void BulletPlaneShape::
153 fillin(DatagramIterator &scan, BamReader *manager) {
154  BulletShape::fillin(scan, manager);
155  nassertv(_shape == nullptr);
156 
157  PN_stdfloat margin = scan.get_stdfloat();
158 
159  LVector3 normal;
160  normal.read_datagram(scan);
161 
162  PN_stdfloat constant = scan.get_stdfloat();
163 
164  _shape = new btStaticPlaneShape(LVecBase3_to_btVector3(normal), constant);
165  _shape->setUserPointer(this);
166  _shape->setMargin(margin);
167 }
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.
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
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
static void register_with_read_factory()
Tells the BamReader how to create objects of type BulletShape.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38