2018-01-05 00:50:27 +01:00
|
|
|
/**************************************************************************/
|
2020-03-26 22:49:16 +01:00
|
|
|
/* velocity_tracker_3d.cpp */
|
2018-01-05 00:50:27 +01:00
|
|
|
/**************************************************************************/
|
2017-07-15 06:23:10 +02:00
|
|
|
/* This file is part of: */
|
2018-01-05 00:50:27 +01:00
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
2017-07-15 06:23:10 +02:00
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
2018-01-05 00:50:27 +01:00
|
|
|
/* */
|
2017-07-15 06:23:10 +02:00
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
2018-01-05 00:50:27 +01:00
|
|
|
/* */
|
2017-07-15 06:23:10 +02:00
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
2018-01-05 00:50:27 +01:00
|
|
|
/* */
|
2017-07-15 06:23:10 +02:00
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
2018-01-05 00:50:27 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
#include "velocity_tracker_3d.h"
|
2017-07-15 06:23:10 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void VelocityTracker3D::set_track_physics_step(bool p_track_physics_step) {
|
2017-09-30 16:19:07 +02:00
|
|
|
physics_step = p_track_physics_step;
|
2017-07-15 06:23:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bool VelocityTracker3D::is_tracking_physics_step() const {
|
2017-09-30 16:19:07 +02:00
|
|
|
return physics_step;
|
2017-07-15 06:23:10 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void VelocityTracker3D::update_position(const Vector3 &p_position) {
|
2017-07-15 06:23:10 +02:00
|
|
|
PositionHistory ph;
|
|
|
|
ph.position = p_position;
|
2017-09-30 16:19:07 +02:00
|
|
|
if (physics_step) {
|
|
|
|
ph.frame = Engine::get_singleton()->get_physics_frames();
|
2017-07-15 06:23:10 +02:00
|
|
|
} else {
|
2020-12-22 10:50:29 +01:00
|
|
|
ph.frame = Engine::get_singleton()->get_frame_ticks();
|
2017-07-15 06:23:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (position_history_len == 0 || position_history[0].frame != ph.frame) { //in same frame, use latest
|
|
|
|
position_history_len = MIN(position_history.size(), position_history_len + 1);
|
|
|
|
for (int i = position_history_len - 1; i > 0; i--) {
|
2018-07-25 03:11:03 +02:00
|
|
|
position_history.write[i] = position_history[i - 1];
|
2017-07-15 06:23:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
position_history.write[0] = ph;
|
2017-07-15 06:23:10 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
Vector3 VelocityTracker3D::get_tracked_linear_velocity() const {
|
2017-07-15 06:23:10 +02:00
|
|
|
Vector3 linear_velocity;
|
|
|
|
|
2021-02-02 03:16:37 +01:00
|
|
|
double max_time = 1 / 5.0; //maximum time to interpolate a velocity
|
2017-07-15 06:23:10 +02:00
|
|
|
|
|
|
|
Vector3 distance_accum;
|
2021-02-02 03:16:37 +01:00
|
|
|
double time_accum = 0.0;
|
|
|
|
double base_time = 0.0;
|
2017-07-15 06:23:10 +02:00
|
|
|
|
|
|
|
if (position_history_len) {
|
2017-09-30 16:19:07 +02:00
|
|
|
if (physics_step) {
|
|
|
|
uint64_t base = Engine::get_singleton()->get_physics_frames();
|
2021-08-11 02:35:16 +02:00
|
|
|
base_time = double(base - position_history[0].frame) / Engine::get_singleton()->get_physics_ticks_per_second();
|
2017-07-15 06:23:10 +02:00
|
|
|
} else {
|
2020-12-22 10:50:29 +01:00
|
|
|
uint64_t base = Engine::get_singleton()->get_frame_ticks();
|
2017-07-15 06:23:10 +02:00
|
|
|
base_time = double(base - position_history[0].frame) / 1000000.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < position_history_len - 1; i++) {
|
2021-02-02 03:16:37 +01:00
|
|
|
double delta = 0.0;
|
2017-07-15 06:23:10 +02:00
|
|
|
uint64_t diff = position_history[i].frame - position_history[i + 1].frame;
|
|
|
|
Vector3 distance = position_history[i].position - position_history[i + 1].position;
|
|
|
|
|
2017-09-30 16:19:07 +02:00
|
|
|
if (physics_step) {
|
2021-08-11 02:35:16 +02:00
|
|
|
delta = double(diff) / Engine::get_singleton()->get_physics_ticks_per_second();
|
2017-07-15 06:23:10 +02:00
|
|
|
} else {
|
|
|
|
delta = double(diff) / 1000000.0;
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (base_time + time_accum + delta > max_time) {
|
2017-07-15 06:23:10 +02:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-07-15 06:23:10 +02:00
|
|
|
|
|
|
|
distance_accum += distance;
|
|
|
|
time_accum += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_accum) {
|
|
|
|
linear_velocity = distance_accum / time_accum;
|
|
|
|
}
|
|
|
|
|
|
|
|
return linear_velocity;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void VelocityTracker3D::reset(const Vector3 &p_new_pos) {
|
2017-07-15 06:23:10 +02:00
|
|
|
PositionHistory ph;
|
|
|
|
ph.position = p_new_pos;
|
2017-09-30 16:19:07 +02:00
|
|
|
if (physics_step) {
|
|
|
|
ph.frame = Engine::get_singleton()->get_physics_frames();
|
2017-07-15 06:23:10 +02:00
|
|
|
} else {
|
2020-12-22 10:50:29 +01:00
|
|
|
ph.frame = Engine::get_singleton()->get_frame_ticks();
|
2017-07-15 06:23:10 +02:00
|
|
|
}
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
position_history.write[0] = ph;
|
2017-07-15 06:23:10 +02:00
|
|
|
position_history_len = 1;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void VelocityTracker3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_track_physics_step", "enable"), &VelocityTracker3D::set_track_physics_step);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_tracking_physics_step"), &VelocityTracker3D::is_tracking_physics_step);
|
|
|
|
ClassDB::bind_method(D_METHOD("update_position", "position"), &VelocityTracker3D::update_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_tracked_linear_velocity"), &VelocityTracker3D::get_tracked_linear_velocity);
|
|
|
|
ClassDB::bind_method(D_METHOD("reset", "position"), &VelocityTracker3D::reset);
|
2018-01-11 23:35:12 +01:00
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "track_physics_step"), "set_track_physics_step", "is_tracking_physics_step");
|
2017-07-15 06:23:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
VelocityTracker3D::VelocityTracker3D() {
|
2017-07-15 06:23:10 +02:00
|
|
|
position_history.resize(4); // should be configurable
|
|
|
|
}
|