Panda3D
Loading...
Searching...
No Matches
physicalNode.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 physicalNode.cxx
10 * @author charles
11 * @date 2000-08-01
12 */
13
14#include "physicalNode.h"
15#include "physicsManager.h"
16
17#ifdef PHAVE_ATOMIC
18#include <atomic>
19#endif
20
21// static stuff.
22static std::atomic_flag warned_copy_physical_node = ATOMIC_FLAG_INIT;
23
24TypeHandle PhysicalNode::_type_handle;
25
26/**
27 * default constructor
28 */
30PhysicalNode(const std::string &name) :
31 PandaNode(name)
32{
33}
34
35/**
36 * copy constructor
37 */
39PhysicalNode(const PhysicalNode &copy) :
40 PandaNode(copy), _physicals(copy._physicals) {
41}
42
43/**
44 * destructor
45 */
48 for (Physical *physical : _physicals) {
49 if (physical->_physical_node == this) {
50 physical->_physical_node = nullptr;
51 if (physical->_physics_manager != nullptr) {
52 physical->_physics_manager->remove_physical(physical);
53 }
54 }
55 }
56}
57
58/**
59 * dynamic child copy
60 */
62make_copy() const {
63 if (!_physicals.empty() && !warned_copy_physical_node.test_and_set()) {
64 // This is a problem, because a Physical can only be on one PhysicalNode.
65 //FIXME: Figure out a solution.
66 physics_cat.warning()
67 << "Detected attempt to copy PhysicalNode object with physicals.\n";
68 }
69 return new PhysicalNode(*this);
70}
71
72/**
73 * append operation
74 */
76add_physicals_from(const PhysicalNode &other) {
77 size_t num_physicals = _physicals.size();
78 _physicals.insert(_physicals.end(),
79 other._physicals.begin(), other._physicals.end());
80
81 for (size_t i = num_physicals; i < _physicals.size(); ++i) {
82 _physicals[i]->_physical_node = this;
83 }
84}
85
86/**
87 * replace operation
88 */
90set_physical(size_t index, Physical *physical) {
91 nassertv(index < _physicals.size());
92
93 _physicals[index]->_physical_node = nullptr;
94 _physicals[index] = physical;
95 physical->_physical_node = this;
96}
97
98/**
99 * insert operation
100 */
102insert_physical(size_t index, Physical *physical) {
103 if (index > _physicals.size()) {
104 index = _physicals.size();
105 }
106
107 _physicals.insert(_physicals.begin() + index, physical);
108 physical->_physical_node = this;
109}
110
111/**
112 * remove operation
113 */
115remove_physical(Physical *physical) {
116 pvector< PT(Physical) >::iterator found;
117 PT(Physical) ptp = physical;
118 found = find(_physicals.begin(), _physicals.end(), ptp);
119 if (found == _physicals.end()) {
120 return;
121 }
122 _physicals.erase(found);
123
124 nassertv(ptp->_physical_node == this);
125 ptp->_physical_node = nullptr;
126}
127
128/**
129 * remove operation
130 */
132remove_physical(size_t index) {
133 nassertv(index <= _physicals.size());
134
135 pvector< PT(Physical) >::iterator remove;
136 remove = _physicals.begin() + index;
137 (*remove)->_physical_node = nullptr;
138
139 _physicals.erase(remove);
140}
141
142/**
143 * Write a string representation of this instance to <out>.
144 */
146write(std::ostream &out, int indent) const {
147 #ifndef NDEBUG //[
148 out.width(indent); out<<""; out<<"PhysicalNode:\n";
149 // PandaNode::write(out, indent+2);
150 #endif //] NDEBUG
151}
A basic node of the scene graph or data graph.
Definition pandaNode.h:65
Graph node that encapsulated a series of physical objects.
virtual PandaNode * make_copy() const
dynamic child copy
remove_physical
remove operation
void add_physicals_from(const PhysicalNode &other)
append operation
PhysicalNode(const std::string &name)
default constructor
virtual ~PhysicalNode()
destructor
insert_physical
insert operation
set_physical
replace operation
virtual void write(std::ostream &out, int indent=0) const
Write a string representation of this instance to <out>.
Defines a set of physically modeled attributes.
Definition physical.h:37
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
This is our own Panda specialization on the default STL vector.
Definition pvector.h:42
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.