physicsManager.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Filename: physicsManager.cxx
  2. // Created by: charles (14Jun00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, 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://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "physicsManager.h"
  19. #include "actorNode.h"
  20. #include <algorithm>
  21. #include "pvector.h"
  22. ////////////////////////////////////////////////////////////////////
  23. // Function : PhysicsManager
  24. // Access : Public
  25. // Description : Default Constructor. NOTE: EulerIntegrator is
  26. // the standard default.
  27. ////////////////////////////////////////////////////////////////////
  28. PhysicsManager::
  29. PhysicsManager(void) {
  30. _linear_integrator.clear();
  31. _angular_integrator.clear();
  32. }
  33. ////////////////////////////////////////////////////////////////////
  34. // Function : ~PhysicsManager
  35. // Access : Public
  36. // Description : Simple Destructor
  37. ////////////////////////////////////////////////////////////////////
  38. PhysicsManager::
  39. ~PhysicsManager(void) {
  40. }
  41. ////////////////////////////////////////////////////////////////////
  42. // Function : remove_linear_force
  43. // Access : Public
  44. // Description : takes a linear force out of the physics list
  45. ////////////////////////////////////////////////////////////////////
  46. void PhysicsManager::
  47. remove_linear_force(LinearForce *f) {
  48. pvector< PT(LinearForce) >::iterator found;
  49. PT(LinearForce) ptbf = f;
  50. found = find(_linear_forces.begin(), _linear_forces.end(), ptbf);
  51. if (found == _linear_forces.end())
  52. return;
  53. _linear_forces.erase(found);
  54. }
  55. ////////////////////////////////////////////////////////////////////
  56. // Function : remove_angular_force
  57. // Access : Public
  58. // Description : takes an angular force out of the physics list
  59. ////////////////////////////////////////////////////////////////////
  60. void PhysicsManager::
  61. remove_angular_force(AngularForce *f) {
  62. pvector< PT(AngularForce) >::iterator found;
  63. PT(BaseForce) ptbf = f;
  64. found = find(_angular_forces.begin(), _angular_forces.end(), ptbf);
  65. if (found == _angular_forces.end())
  66. return;
  67. _angular_forces.erase(found);
  68. }
  69. ////////////////////////////////////////////////////////////////////
  70. // Function : remove_physical
  71. // Access : Public
  72. // Description : takes a physical out of the object list
  73. ////////////////////////////////////////////////////////////////////
  74. void PhysicsManager::
  75. remove_physical(Physical *p) {
  76. pvector< Physical * >::iterator found;
  77. found = find(_physicals.begin(), _physicals.end(), p);
  78. if (found == _physicals.end())
  79. return;
  80. p->_physics_manager = (PhysicsManager *) NULL;
  81. _physicals.erase(found);
  82. }
  83. ////////////////////////////////////////////////////////////////////
  84. // Function : DoPhysics
  85. // Access : Public
  86. // Description : This is the main high-level API call. Performs
  87. // integration on every attached Physical.
  88. ////////////////////////////////////////////////////////////////////
  89. void PhysicsManager::
  90. do_physics(float dt) {
  91. pvector< Physical * >::iterator p_cur;
  92. // now, run through each physics object in the set.
  93. p_cur = _physicals.begin();
  94. for (; p_cur != _physicals.end(); p_cur++) {
  95. Physical *physical = *p_cur;
  96. // do linear
  97. if (_linear_integrator.is_null() == false) {
  98. _linear_integrator->integrate(physical, _linear_forces, dt);
  99. }
  100. // do angular
  101. if (_angular_integrator.is_null() == false) {
  102. _angular_integrator->integrate(physical, _angular_forces, dt);
  103. }
  104. // if it's an actor node, tell it to update itself.
  105. PhysicalNode *pn = physical->get_physical_node();
  106. if (pn->is_of_type(ActorNode::get_class_type())) {
  107. ActorNode *an = (ActorNode *) pn;
  108. an->update_arc();
  109. }
  110. }
  111. }