animatable_body_3d.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**************************************************************************/
  2. /* animatable_body_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "animatable_body_3d.h"
  31. Vector3 AnimatableBody3D::get_linear_velocity() const {
  32. return linear_velocity;
  33. }
  34. Vector3 AnimatableBody3D::get_angular_velocity() const {
  35. return angular_velocity;
  36. }
  37. void AnimatableBody3D::set_sync_to_physics(bool p_enable) {
  38. if (sync_to_physics == p_enable) {
  39. return;
  40. }
  41. sync_to_physics = p_enable;
  42. _update_kinematic_motion();
  43. }
  44. bool AnimatableBody3D::is_sync_to_physics_enabled() const {
  45. return sync_to_physics;
  46. }
  47. void AnimatableBody3D::_update_kinematic_motion() {
  48. #ifdef TOOLS_ENABLED
  49. if (Engine::get_singleton()->is_editor_hint()) {
  50. return;
  51. }
  52. #endif
  53. if (sync_to_physics) {
  54. set_only_update_transform_changes(true);
  55. set_notify_local_transform(true);
  56. } else {
  57. set_only_update_transform_changes(false);
  58. set_notify_local_transform(false);
  59. }
  60. }
  61. void AnimatableBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
  62. linear_velocity = p_state->get_linear_velocity();
  63. angular_velocity = p_state->get_angular_velocity();
  64. if (!sync_to_physics) {
  65. return;
  66. }
  67. last_valid_transform = p_state->get_transform();
  68. set_notify_local_transform(false);
  69. set_global_transform(last_valid_transform);
  70. set_notify_local_transform(true);
  71. _on_transform_changed();
  72. }
  73. void AnimatableBody3D::_notification(int p_what) {
  74. #ifdef TOOLS_ENABLED
  75. if (Engine::get_singleton()->is_editor_hint()) {
  76. return;
  77. }
  78. #endif
  79. switch (p_what) {
  80. case NOTIFICATION_ENTER_TREE: {
  81. last_valid_transform = get_global_transform();
  82. _update_kinematic_motion();
  83. } break;
  84. case NOTIFICATION_EXIT_TREE: {
  85. set_only_update_transform_changes(false);
  86. set_notify_local_transform(false);
  87. } break;
  88. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  89. // Used by sync to physics, send the new transform to the physics...
  90. Transform3D new_transform = get_global_transform();
  91. PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_TRANSFORM, new_transform);
  92. // ... but then revert changes.
  93. set_notify_local_transform(false);
  94. set_global_transform(last_valid_transform);
  95. set_notify_local_transform(true);
  96. _on_transform_changed();
  97. } break;
  98. }
  99. }
  100. void AnimatableBody3D::_bind_methods() {
  101. ClassDB::bind_method(D_METHOD("set_sync_to_physics", "enable"), &AnimatableBody3D::set_sync_to_physics);
  102. ClassDB::bind_method(D_METHOD("is_sync_to_physics_enabled"), &AnimatableBody3D::is_sync_to_physics_enabled);
  103. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync_to_physics"), "set_sync_to_physics", "is_sync_to_physics_enabled");
  104. }
  105. AnimatableBody3D::AnimatableBody3D() :
  106. StaticBody3D(PhysicsServer3D::BODY_MODE_KINEMATIC) {
  107. PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &AnimatableBody3D::_body_state_changed));
  108. }