2023-01-10 15:26:54 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* vector3.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "vector3.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
2019-02-09 06:24:02 +01:00
|
|
|
#include "core/math/basis.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-05-05 13:25:04 +02:00
|
|
|
void Vector3::rotate(const Vector3 &p_axis, real_t p_angle) {
|
|
|
|
*this = Basis(p_axis, p_angle).xform(*this);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-05-05 13:25:04 +02:00
|
|
|
Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_angle) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector3 r = *this;
|
2022-05-05 13:25:04 +02:00
|
|
|
r.rotate(p_axis, p_angle);
|
2014-02-10 02:10:30 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Vector3::set_axis(int p_axis, real_t p_value) {
|
|
|
|
ERR_FAIL_INDEX(p_axis, 3);
|
|
|
|
coord[p_axis] = p_value;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
real_t Vector3::get_axis(int p_axis) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_axis, 3, 0);
|
2016-10-01 20:54:31 +02:00
|
|
|
return operator[](p_axis);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 11:31:21 +01:00
|
|
|
void Vector3::snap(const Vector3 &p_val) {
|
2017-06-30 20:47:17 +02:00
|
|
|
x = Math::stepify(x, p_val.x);
|
|
|
|
y = Math::stepify(y, p_val.y);
|
|
|
|
z = Math::stepify(z, p_val.z);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2024-02-18 11:31:21 +01:00
|
|
|
Vector3 Vector3::snapped(const Vector3 &p_val) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 v = *this;
|
2016-10-01 20:54:31 +02:00
|
|
|
v.snap(p_val);
|
|
|
|
return v;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 11:31:21 +01:00
|
|
|
Vector3 Vector3::limit_length(real_t p_len) const {
|
2021-11-05 00:17:41 +01:00
|
|
|
const real_t l = length();
|
|
|
|
Vector3 v = *this;
|
|
|
|
if (l > 0 && p_len < l) {
|
|
|
|
v /= l;
|
|
|
|
v *= p_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2021-04-22 19:59:16 +02:00
|
|
|
Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 p0 = p_pre_a;
|
|
|
|
Vector3 p1 = *this;
|
|
|
|
Vector3 p2 = p_b;
|
|
|
|
Vector3 p3 = p_post_b;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
//normalize
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t ab = p0.distance_to(p1);
|
|
|
|
real_t bc = p1.distance_to(p2);
|
|
|
|
real_t cd = p2.distance_to(p3);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (ab > 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p0 = p1 + (p0 - p1) * (bc / ab);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
|
|
|
if (cd > 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p3 = p2 + (p3 - p2) * (bc / cd);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:59:16 +02:00
|
|
|
real_t t = p_weight;
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t t2 = t * t;
|
|
|
|
real_t t3 = t2 * t;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector3 out;
|
2022-02-24 09:41:35 +01:00
|
|
|
out = 0.5f *
|
|
|
|
((p1 * 2) +
|
2021-10-28 13:23:24 +02:00
|
|
|
(-p0 + p2) * t +
|
2022-02-24 09:41:35 +01:00
|
|
|
(2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
|
|
|
|
(-p0 + 3 * p1 - 3 * p2 + p3) * t3);
|
2014-02-10 02:10:30 +01:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-04-22 19:59:16 +02:00
|
|
|
Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 p0 = p_pre_a;
|
|
|
|
Vector3 p1 = *this;
|
|
|
|
Vector3 p2 = p_b;
|
|
|
|
Vector3 p3 = p_post_b;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-04-22 19:59:16 +02:00
|
|
|
real_t t = p_weight;
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t t2 = t * t;
|
|
|
|
real_t t3 = t2 * t;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector3 out;
|
2022-02-24 09:41:35 +01:00
|
|
|
out = 0.5f *
|
|
|
|
((p1 * 2) +
|
2021-10-28 13:23:24 +02:00
|
|
|
(-p0 + p2) * t +
|
2022-02-24 09:41:35 +01:00
|
|
|
(2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
|
|
|
|
(-p0 + 3 * p1 - 3 * p2 + p3) * t3);
|
2014-02-10 02:10:30 +01:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2024-02-18 11:31:21 +01:00
|
|
|
Vector3 Vector3::move_toward(const Vector3 &p_to, real_t p_delta) const {
|
2019-04-07 23:40:56 +02:00
|
|
|
Vector3 v = *this;
|
|
|
|
Vector3 vd = p_to - v;
|
|
|
|
real_t len = vd.length();
|
2022-02-24 09:41:35 +01:00
|
|
|
return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta;
|
2019-04-07 23:40:56 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:26:27 +02:00
|
|
|
Basis Vector3::outer(const Vector3 &p_b) const {
|
|
|
|
Vector3 row0(x * p_b.x, x * p_b.y, x * p_b.z);
|
|
|
|
Vector3 row1(y * p_b.x, y * p_b.y, y * p_b.z);
|
|
|
|
Vector3 row2(z * p_b.x, z * p_b.y, z * p_b.z);
|
|
|
|
|
|
|
|
return Basis(row0, row1, row2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Basis Vector3::to_diagonal_matrix() const {
|
|
|
|
return Basis(x, 0, 0,
|
|
|
|
0, y, 0,
|
|
|
|
0, 0, z);
|
|
|
|
}
|
|
|
|
|
2019-10-14 22:33:45 +02:00
|
|
|
bool Vector3::is_equal_approx(const Vector3 &p_v) const {
|
|
|
|
return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z);
|
|
|
|
}
|
|
|
|
|
2024-04-10 13:02:42 +02:00
|
|
|
bool Vector3::is_zero_approx() const {
|
|
|
|
return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector3::operator String() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
return (rtos(x) + ", " + rtos(y) + ", " + rtos(z));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|