Skip to content
0.2.0-dev Download ZIP
122 lines ·4.7 KB Raw
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
31#include "velocity_tracker_3d.h"
32
33#include "core/config/engine.h"
34
35void VelocityTracker3D::set_track_physics_step(bool p_track_physics_step) {
36 physics_step = p_track_physics_step;
37}
38
39bool VelocityTracker3D::is_tracking_physics_step() const {
40 return physics_step;
41}
42
43void VelocityTracker3D::update_position(const Vector3 &p_position) {
44 PositionHistory ph;
45 ph.position = p_position;
46 if (physics_step) {
47 ph.frame = Engine::get_singleton()->get_physics_frames();
48 } else {
49 ph.frame = Engine::get_singleton()->get_frame_ticks();
50 }
51
52 if (position_history_len == 0 || position_history[0].frame != ph.frame) { //in same frame, use latest
53 position_history_len = MIN(position_history.size(), position_history_len + 1);
54 for (int i = position_history_len - 1; i > 0; i--) {
55 position_history.write[i] = position_history[i - 1];
56 }
57 }
58
59 position_history.write[0] = ph;
60}
61
62Vector3 VelocityTracker3D::get_tracked_linear_velocity() const {
63 Vector3 linear_velocity;
64
65 double max_time = 1 / 5.0; //maximum time to interpolate a velocity
66
67 Vector3 distance_accum;
68 double time_accum = 0.0;
69 double base_time = 0.0;
70
71 if (position_history_len) {
72 if (physics_step) {
73 uint64_t base = Engine::get_singleton()->get_physics_frames();
74 base_time = double(base - position_history[0].frame) / Engine::get_singleton()->get_user_physics_ticks_per_second();
75 } else {
76 uint64_t base = Engine::get_singleton()->get_frame_ticks();
77 base_time = double(base - position_history[0].frame) / 1000000.0;
78 }
79 }
80
81 for (int i = 0; i < position_history_len - 1; i++) {
82 double delta = 0.0;
83 uint64_t diff = position_history[i].frame - position_history[i + 1].frame;
84 Vector3 distance = position_history[i].position - position_history[i + 1].position;
85
86 if (physics_step) {
87 delta = double(diff) / Engine::get_singleton()->get_user_physics_ticks_per_second();
88 } else {
89 delta = double(diff) / 1000000.0;
90 }
91
92 if (base_time + time_accum + delta > max_time) {
93 break;
94 }
95
96 distance_accum += distance;
97 time_accum += delta;
98 }
99
100 if (time_accum) {
101 linear_velocity = distance_accum / time_accum;
102 }
103
104 return linear_velocity;
105}
106
107void VelocityTracker3D::reset(const Vector3 &p_new_pos) {
108 PositionHistory ph;
109 ph.position = p_new_pos;
110 if (physics_step) {
111 ph.frame = Engine::get_singleton()->get_physics_frames();
112 } else {
113 ph.frame = Engine::get_singleton()->get_frame_ticks();
114 }
115
116 position_history.write[0] = ph;
117 position_history_len = 1;
118}
119
120VelocityTracker3D::VelocityTracker3D() {
121 position_history.resize(4); // should be configurable
122}