velocity_tracker_3d.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**************************************************************************/
  2. /* velocity_tracker_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 "velocity_tracker_3d.h"
  31. void VelocityTracker3D::set_track_physics_step(bool p_track_physics_step) {
  32. physics_step = p_track_physics_step;
  33. }
  34. bool VelocityTracker3D::is_tracking_physics_step() const {
  35. return physics_step;
  36. }
  37. void VelocityTracker3D::update_position(const Vector3 &p_position) {
  38. PositionHistory ph;
  39. ph.position = p_position;
  40. if (physics_step) {
  41. ph.frame = Engine::get_singleton()->get_physics_frames();
  42. } else {
  43. ph.frame = Engine::get_singleton()->get_frame_ticks();
  44. }
  45. if (position_history_len == 0 || position_history[0].frame != ph.frame) { //in same frame, use latest
  46. position_history_len = MIN(position_history.size(), position_history_len + 1);
  47. for (int i = position_history_len - 1; i > 0; i--) {
  48. position_history.write[i] = position_history[i - 1];
  49. }
  50. }
  51. position_history.write[0] = ph;
  52. }
  53. Vector3 VelocityTracker3D::get_tracked_linear_velocity() const {
  54. Vector3 linear_velocity;
  55. double max_time = 1 / 5.0; //maximum time to interpolate a velocity
  56. Vector3 distance_accum;
  57. double time_accum = 0.0;
  58. double base_time = 0.0;
  59. if (position_history_len) {
  60. if (physics_step) {
  61. uint64_t base = Engine::get_singleton()->get_physics_frames();
  62. base_time = double(base - position_history[0].frame) / Engine::get_singleton()->get_physics_ticks_per_second();
  63. } else {
  64. uint64_t base = Engine::get_singleton()->get_frame_ticks();
  65. base_time = double(base - position_history[0].frame) / 1000000.0;
  66. }
  67. }
  68. for (int i = 0; i < position_history_len - 1; i++) {
  69. double delta = 0.0;
  70. uint64_t diff = position_history[i].frame - position_history[i + 1].frame;
  71. Vector3 distance = position_history[i].position - position_history[i + 1].position;
  72. if (physics_step) {
  73. delta = double(diff) / Engine::get_singleton()->get_physics_ticks_per_second();
  74. } else {
  75. delta = double(diff) / 1000000.0;
  76. }
  77. if (base_time + time_accum + delta > max_time) {
  78. break;
  79. }
  80. distance_accum += distance;
  81. time_accum += delta;
  82. }
  83. if (time_accum) {
  84. linear_velocity = distance_accum / time_accum;
  85. }
  86. return linear_velocity;
  87. }
  88. void VelocityTracker3D::reset(const Vector3 &p_new_pos) {
  89. PositionHistory ph;
  90. ph.position = p_new_pos;
  91. if (physics_step) {
  92. ph.frame = Engine::get_singleton()->get_physics_frames();
  93. } else {
  94. ph.frame = Engine::get_singleton()->get_frame_ticks();
  95. }
  96. position_history.write[0] = ph;
  97. position_history_len = 1;
  98. }
  99. void VelocityTracker3D::_bind_methods() {
  100. ClassDB::bind_method(D_METHOD("set_track_physics_step", "enable"), &VelocityTracker3D::set_track_physics_step);
  101. ClassDB::bind_method(D_METHOD("is_tracking_physics_step"), &VelocityTracker3D::is_tracking_physics_step);
  102. ClassDB::bind_method(D_METHOD("update_position", "position"), &VelocityTracker3D::update_position);
  103. ClassDB::bind_method(D_METHOD("get_tracked_linear_velocity"), &VelocityTracker3D::get_tracked_linear_velocity);
  104. ClassDB::bind_method(D_METHOD("reset", "position"), &VelocityTracker3D::reset);
  105. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "track_physics_step"), "set_track_physics_step", "is_tracking_physics_step");
  106. }
  107. VelocityTracker3D::VelocityTracker3D() {
  108. position_history.resize(4); // should be configurable
  109. }