spatial_velocity_tracker.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "spatial_velocity_tracker.h"
  2. #include "engine.h"
  3. void SpatialVelocityTracker::set_track_physics_step(bool p_track_physics_step) {
  4. physics_step = p_track_physics_step;
  5. }
  6. bool SpatialVelocityTracker::is_tracking_physics_step() const {
  7. return physics_step;
  8. }
  9. void SpatialVelocityTracker::update_position(const Vector3 &p_position) {
  10. PositionHistory ph;
  11. ph.position = p_position;
  12. if (physics_step) {
  13. ph.frame = Engine::get_singleton()->get_physics_frames();
  14. } else {
  15. ph.frame = Engine::get_singleton()->get_idle_frame_ticks();
  16. }
  17. if (position_history_len == 0 || position_history[0].frame != ph.frame) { //in same frame, use latest
  18. position_history_len = MIN(position_history.size(), position_history_len + 1);
  19. for (int i = position_history_len - 1; i > 0; i--) {
  20. position_history[i] = position_history[i - 1];
  21. }
  22. }
  23. position_history[0] = ph;
  24. }
  25. Vector3 SpatialVelocityTracker::get_tracked_linear_velocity() const {
  26. Vector3 linear_velocity;
  27. float max_time = 1 / 5.0; //maximum time to interpolate a velocity
  28. Vector3 distance_accum;
  29. float time_accum = 0.0;
  30. float base_time = 0.0;
  31. if (position_history_len) {
  32. if (physics_step) {
  33. uint64_t base = Engine::get_singleton()->get_physics_frames();
  34. base_time = float(base - position_history[0].frame) / Engine::get_singleton()->get_iterations_per_second();
  35. } else {
  36. uint64_t base = Engine::get_singleton()->get_idle_frame_ticks();
  37. base_time = double(base - position_history[0].frame) / 1000000.0;
  38. }
  39. }
  40. for (int i = 0; i < position_history_len - 1; i++) {
  41. float delta = 0.0;
  42. uint64_t diff = position_history[i].frame - position_history[i + 1].frame;
  43. Vector3 distance = position_history[i].position - position_history[i + 1].position;
  44. if (physics_step) {
  45. delta = float(diff) / Engine::get_singleton()->get_iterations_per_second();
  46. } else {
  47. delta = double(diff) / 1000000.0;
  48. }
  49. if (base_time + time_accum + delta > max_time)
  50. break;
  51. distance_accum += distance;
  52. time_accum += delta;
  53. }
  54. if (time_accum) {
  55. linear_velocity = distance_accum / time_accum;
  56. }
  57. return linear_velocity;
  58. }
  59. void SpatialVelocityTracker::reset(const Vector3 &p_new_pos) {
  60. PositionHistory ph;
  61. ph.position = p_new_pos;
  62. if (physics_step) {
  63. ph.frame = Engine::get_singleton()->get_physics_frames();
  64. } else {
  65. ph.frame = Engine::get_singleton()->get_idle_frame_ticks();
  66. }
  67. position_history[0] = ph;
  68. position_history_len = 1;
  69. }
  70. void SpatialVelocityTracker::_bind_methods() {
  71. ClassDB::bind_method(D_METHOD("set_track_physics_step", "enable"), &SpatialVelocityTracker::set_track_physics_step);
  72. ClassDB::bind_method(D_METHOD("is_tracking_physics_step"), &SpatialVelocityTracker::is_tracking_physics_step);
  73. ClassDB::bind_method(D_METHOD("update_position", "position"), &SpatialVelocityTracker::update_position);
  74. ClassDB::bind_method(D_METHOD("get_tracked_linear_velocity"), &SpatialVelocityTracker::get_tracked_linear_velocity);
  75. ClassDB::bind_method(D_METHOD("reset", "position"), &SpatialVelocityTracker::reset);
  76. }
  77. SpatialVelocityTracker::SpatialVelocityTracker() {
  78. position_history.resize(4); // should be configurable
  79. position_history_len = 0;
  80. physics_step = false;
  81. }