spatial_velocity_tracker.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*************************************************************************/
  2. /* spatial_velocity_tracker.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 "spatial_velocity_tracker.h"
  31. #include "core/engine.h"
  32. void SpatialVelocityTracker::set_track_physics_step(bool p_track_physics_step) {
  33. physics_step = p_track_physics_step;
  34. }
  35. bool SpatialVelocityTracker::is_tracking_physics_step() const {
  36. return physics_step;
  37. }
  38. void SpatialVelocityTracker::update_position(const Vector3 &p_position) {
  39. PositionHistory ph;
  40. ph.position = p_position;
  41. if (physics_step) {
  42. ph.frame = Engine::get_singleton()->get_physics_frames();
  43. } else {
  44. ph.frame = Engine::get_singleton()->get_idle_frame_ticks();
  45. }
  46. if (position_history_len == 0 || position_history[0].frame != ph.frame) { //in same frame, use latest
  47. position_history_len = MIN(position_history.size(), position_history_len + 1);
  48. for (int i = position_history_len - 1; i > 0; i--) {
  49. position_history.write[i] = position_history[i - 1];
  50. }
  51. }
  52. position_history.write[0] = ph;
  53. }
  54. Vector3 SpatialVelocityTracker::get_tracked_linear_velocity() const {
  55. Vector3 linear_velocity;
  56. float max_time = 1 / 5.0; //maximum time to interpolate a velocity
  57. Vector3 distance_accum;
  58. float time_accum = 0.0;
  59. float base_time = 0.0;
  60. if (position_history_len) {
  61. if (physics_step) {
  62. uint64_t base = Engine::get_singleton()->get_physics_frames();
  63. base_time = float(base - position_history[0].frame) / Engine::get_singleton()->get_iterations_per_second();
  64. } else {
  65. uint64_t base = Engine::get_singleton()->get_idle_frame_ticks();
  66. base_time = double(base - position_history[0].frame) / 1000000.0;
  67. }
  68. }
  69. for (int i = 0; i < position_history_len - 1; i++) {
  70. float delta = 0.0;
  71. uint64_t diff = position_history[i].frame - position_history[i + 1].frame;
  72. Vector3 distance = position_history[i].position - position_history[i + 1].position;
  73. if (physics_step) {
  74. delta = float(diff) / Engine::get_singleton()->get_iterations_per_second();
  75. } else {
  76. delta = double(diff) / 1000000.0;
  77. }
  78. if (base_time + time_accum + delta > max_time)
  79. break;
  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 SpatialVelocityTracker::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_idle_frame_ticks();
  95. }
  96. position_history.write[0] = ph;
  97. position_history_len = 1;
  98. }
  99. void SpatialVelocityTracker::_bind_methods() {
  100. ClassDB::bind_method(D_METHOD("set_track_physics_step", "enable"), &SpatialVelocityTracker::set_track_physics_step);
  101. ClassDB::bind_method(D_METHOD("is_tracking_physics_step"), &SpatialVelocityTracker::is_tracking_physics_step);
  102. ClassDB::bind_method(D_METHOD("update_position", "position"), &SpatialVelocityTracker::update_position);
  103. ClassDB::bind_method(D_METHOD("get_tracked_linear_velocity"), &SpatialVelocityTracker::get_tracked_linear_velocity);
  104. ClassDB::bind_method(D_METHOD("reset", "position"), &SpatialVelocityTracker::reset);
  105. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "track_physics_step"), "set_track_physics_step", "is_tracking_physics_step");
  106. }
  107. SpatialVelocityTracker::SpatialVelocityTracker() {
  108. position_history.resize(4); // should be configurable
  109. position_history_len = 0;
  110. physics_step = false;
  111. }