forceNode.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Filename: forceNode.cxx
  2. // Created by: charles (02Aug00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "forceNode.h"
  19. #include "config_physics.h"
  20. TypeHandle ForceNode::_type_handle;
  21. ////////////////////////////////////////////////////////////////////
  22. // Function : ForceNode
  23. // Access : public
  24. // Description : default constructor
  25. ////////////////////////////////////////////////////////////////////
  26. ForceNode::
  27. ForceNode(const string &name) :
  28. PandaNode(name) {
  29. }
  30. ////////////////////////////////////////////////////////////////////
  31. // Function : ForceNode
  32. // Access : protected
  33. // Description : copy constructor
  34. ////////////////////////////////////////////////////////////////////
  35. ForceNode::
  36. ForceNode(const ForceNode &copy) :
  37. PandaNode(copy), _forces(copy._forces) {
  38. }
  39. ////////////////////////////////////////////////////////////////////
  40. // Function : ~ForceNode
  41. // Access : public, virtual
  42. // Description : destructor
  43. ////////////////////////////////////////////////////////////////////
  44. ForceNode::
  45. ~ForceNode() {
  46. }
  47. ////////////////////////////////////////////////////////////////////
  48. // Function : make_copy
  49. // Access : public, virtual
  50. // Description : dynamic child copy
  51. ////////////////////////////////////////////////////////////////////
  52. PandaNode *ForceNode::
  53. make_copy() const {
  54. return new ForceNode(*this);
  55. }
  56. ////////////////////////////////////////////////////////////////////
  57. // Function : add_forces_from
  58. // Access : public
  59. // Description : append operation
  60. ////////////////////////////////////////////////////////////////////
  61. void ForceNode::
  62. add_forces_from(const ForceNode &other) {
  63. pvector< PT(BaseForce) >::iterator last = _forces.end() - 1;
  64. _forces.insert(_forces.end(),
  65. other._forces.begin(), other._forces.end());
  66. NodePath node_path(this);
  67. for (; last != _forces.end(); last++) {
  68. (*last)->_force_node = this;
  69. (*last)->_force_node_path = node_path;
  70. }
  71. }
  72. ////////////////////////////////////////////////////////////////////
  73. // Function : remove_force
  74. // Access : public
  75. // Description : remove operation
  76. ////////////////////////////////////////////////////////////////////
  77. void ForceNode::
  78. remove_force(BaseForce *f) {
  79. pvector< PT(BaseForce) >::iterator found;
  80. PT(BaseForce) ptbf = f;
  81. found = find(_forces.begin(), _forces.end(), ptbf);
  82. if (found == _forces.end())
  83. return;
  84. _forces.erase(found);
  85. }
  86. ////////////////////////////////////////////////////////////////////
  87. // Function : remove_force
  88. // Access : public
  89. // Description : remove operation
  90. ////////////////////////////////////////////////////////////////////
  91. void ForceNode::
  92. remove_force(int index) {
  93. nassertv(index >= 0 && index <= (int)_forces.size());
  94. pvector< PT(BaseForce) >::iterator remove;
  95. remove = _forces.begin() + index;
  96. (*remove)->_force_node = (ForceNode *) NULL;
  97. (*remove)->_force_node_path = NodePath();
  98. _forces.erase(remove);
  99. }
  100. ////////////////////////////////////////////////////////////////////
  101. // Function : output
  102. // Access : Public
  103. // Description : Write a string representation of this instance to
  104. // <out>.
  105. ////////////////////////////////////////////////////////////////////
  106. void ForceNode::
  107. output(ostream &out) const {
  108. PandaNode::output(out);
  109. out<<" ("<<_forces.size()<<" forces)";
  110. }
  111. ////////////////////////////////////////////////////////////////////
  112. // Function : write_linear_forces
  113. // Access : Public
  114. // Description : Write a string representation of this instance to
  115. // <out>.
  116. ////////////////////////////////////////////////////////////////////
  117. void ForceNode::
  118. write_forces(ostream &out, unsigned int indent) const {
  119. #ifndef NDEBUG //[
  120. out.width(indent); out<<""<<"_forces ("<<_forces.size()<<" forces)"<<"\n";
  121. for (ForceVector::const_iterator i=_forces.begin();
  122. i != _forces.end();
  123. ++i) {
  124. out.width(indent+2); out<<""; out<<"(id "<<&(*i)<<" "<<(*i)->is_linear()<<")\n";
  125. //#*#(*i)->write(out, indent+2);
  126. }
  127. #endif //] NDEBUG
  128. }
  129. ////////////////////////////////////////////////////////////////////
  130. // Function : write
  131. // Access : Public
  132. // Description : Write a string representation of this instance to
  133. // <out>.
  134. ////////////////////////////////////////////////////////////////////
  135. void ForceNode::
  136. write(ostream &out, unsigned int indent) const {
  137. #ifndef NDEBUG //[
  138. out.width(indent); out<<""; out<<"ForceNode (id "<<this<<") ";
  139. //#*#PandaNode::output(out);
  140. out<<"\n";
  141. //#*#write_forces(out, indent+2);
  142. PandaNode::write(out, indent+4);
  143. #endif //] NDEBUG
  144. }