Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/math/geometry_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes
for (const Vector3 &vertex : vertices) {
int idx = -1;
for (uint32_t k = 0; k < mesh.vertices.size(); k++) {
if (mesh.vertices[k].distance_to(vertex) < 0.001f) {
if (mesh.vertices[k].distance_squared_to(vertex) < 0.000001f) {
idx = k;
break;
}
Expand Down
7 changes: 4 additions & 3 deletions core/math/geometry_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,14 @@ class Geometry3D {

real_t sphere_d = normal.dot(sphere_pos);

real_t ray_distance = sphere_pos.distance_to(normal * sphere_d);
const real_t ray_distance_squared = sphere_pos.distance_squared_to(normal * sphere_d);
const real_t sphere_radius_squared = p_sphere_radius * p_sphere_radius;

if (ray_distance >= p_sphere_radius) {
if (ray_distance_squared >= sphere_radius_squared) {
return false;
}

real_t inters_d2 = p_sphere_radius * p_sphere_radius - ray_distance * ray_distance;
real_t inters_d2 = sphere_radius_squared - ray_distance_squared;
real_t inters_d = sphere_d;

if (inters_d2 >= (real_t)CMP_EPSILON) {
Expand Down
4 changes: 2 additions & 2 deletions editor/animation/animation_blend_space_2d_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void AnimationNodeBlendSpace2DEditor::_blend_space_gui_input(const Ref<InputEven
_update_tool_erase();

for (int i = 0; i < points.size(); i++) {
if (points[i].distance_to(mb->get_position()) < 10 * EDSCALE) {
if (points[i].distance_squared_to(mb->get_position()) < (10.0f * EDSCALE) * (10.0f * EDSCALE)) {
selected_point = i;
Ref<AnimationNode> node = blend_space->get_blend_point_node(i);
EditorNode::get_singleton()->push_item(node.ptr(), "", true);
Expand Down Expand Up @@ -215,7 +215,7 @@ void AnimationNodeBlendSpace2DEditor::_blend_space_gui_input(const Ref<InputEven
continue;
}

if (points[i].distance_to(mb->get_position()) < 10 * EDSCALE) {
if (points[i].distance_squared_to(mb->get_position()) < (10.0f * EDSCALE) * (10.0f * EDSCALE)) {
making_triangle.push_back(i);
if (making_triangle.size() == 3) {
//add triangle!
Expand Down
24 changes: 13 additions & 11 deletions editor/scene/2d/abstract_polygon_2d_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
} else {
const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");

if (!_is_line() && wip.size() > 1 && xform.xform(wip[0]).distance_to(xform.xform(cpoint)) < grab_threshold) {
if (!_is_line() && wip.size() > 1 && xform.xform(wip[0]).distance_squared_to(xform.xform(cpoint)) < (grab_threshold * grab_threshold)) {
//wip closed
_wip_close();

Expand All @@ -496,15 +496,15 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)

// Center drag.
if (edit_origin_and_center) {
real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");

if (mb->get_button_index() == MouseButton::LEFT) {
if (mb->is_meta_pressed() || mb->is_ctrl_pressed() || mb->is_shift_pressed() || mb->is_alt_pressed()) {
return false;
}
if (mb->is_pressed() && !center_drag) {
Vector2 center_point = xform.xform(_get_geometric_center());
if ((gpoint - center_point).length() < grab_threshold) {
if ((gpoint - center_point).length_squared() < (grab_threshold * grab_threshold)) {
pre_center_move_edit.clear();
int n_polygons = _get_polygon_count();
for (int i = 0; i < n_polygons; i++) {
Expand Down Expand Up @@ -821,12 +821,13 @@ AbstractPolygon2DEditor::Vertex AbstractPolygon2DEditor::get_active_point() cons

AbstractPolygon2DEditor::PosVertex AbstractPolygon2DEditor::closest_point(const Vector2 &p_pos) const {
const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
const real_t grab_threshold_squared = grab_threshold * grab_threshold;

const int n_polygons = _get_polygon_count();
const Transform2D xform = canvas_item_editor->get_canvas_transform() * _get_node()->get_screen_transform();

PosVertex closest;
real_t closest_dist = 1e10;
real_t closest_dist_squared = 1e20;

for (int j = 0; j < n_polygons; j++) {
Vector<Vector2> points = _get_polygon(j);
Expand All @@ -836,9 +837,9 @@ AbstractPolygon2DEditor::PosVertex AbstractPolygon2DEditor::closest_point(const
for (int i = 0; i < n_points; i++) {
Vector2 cp = xform.xform(points[i] + offset);

real_t d = cp.distance_to(p_pos);
if (d < closest_dist && d < grab_threshold) {
closest_dist = d;
const real_t dist_squared = cp.distance_squared_to(p_pos);
if (dist_squared < closest_dist_squared && dist_squared < grab_threshold_squared) {
closest_dist_squared = dist_squared;
closest = PosVertex(j, i, cp);
}
}
Expand All @@ -849,14 +850,15 @@ AbstractPolygon2DEditor::PosVertex AbstractPolygon2DEditor::closest_point(const

AbstractPolygon2DEditor::PosVertex AbstractPolygon2DEditor::closest_edge_point(const Vector2 &p_pos) const {
const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
const real_t grab_threshold_squared = grab_threshold * grab_threshold;
const real_t eps = grab_threshold * 2;
const real_t eps2 = eps * eps;

const int n_polygons = _get_polygon_count();
const Transform2D xform = canvas_item_editor->get_canvas_transform() * _get_node()->get_screen_transform();

PosVertex closest;
real_t closest_dist = 1e10;
real_t closest_dist_squared = 1e20;

for (int j = 0; j < n_polygons; j++) {
Vector<Vector2> points = _get_polygon(j);
Expand All @@ -874,9 +876,9 @@ AbstractPolygon2DEditor::PosVertex AbstractPolygon2DEditor::closest_edge_point(c
continue; //not valid to reuse point
}

real_t d = cp.distance_to(p_pos);
if (d < closest_dist && d < grab_threshold) {
closest_dist = d;
const real_t dist_squared = cp.distance_squared_to(p_pos);
if (dist_squared < closest_dist_squared && dist_squared < grab_threshold_squared) {
closest_dist_squared = dist_squared;
closest = PosVertex(j, i, cp);
}
}
Expand Down
29 changes: 15 additions & 14 deletions editor/scene/2d/path_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
return false;
}

real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
const real_t grab_threshold_squared = grab_threshold * grab_threshold;

Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid()) {
Expand All @@ -95,13 +96,13 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
original_mouse_pos = gpoint;

for (int i = 0; i < curve->get_point_count(); i++) {
real_t dist_to_p = gpoint.distance_to(xform.xform(curve->get_point_position(i)));
real_t dist_to_p_out = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_out(i)));
real_t dist_to_p_in = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_in(i)));
real_t dist_to_p_squared = gpoint.distance_squared_to(xform.xform(curve->get_point_position(i)));
real_t dist_to_p_out_squared = gpoint.distance_squared_to(xform.xform(curve->get_point_position(i) + curve->get_point_out(i)));
real_t dist_to_p_in_squared = gpoint.distance_squared_to(xform.xform(curve->get_point_position(i) + curve->get_point_in(i)));

// Check for point movement start (for point + in/out controls).
if (mb->get_button_index() == MouseButton::LEFT) {
if (mode == MODE_EDIT && !mb->is_shift_pressed() && dist_to_p < grab_threshold) {
if (mode == MODE_EDIT && !mb->is_shift_pressed() && dist_to_p_squared < grab_threshold_squared) {
// Points can only be moved in edit mode.

action = ACTION_MOVING_POINT;
Expand All @@ -112,15 +113,15 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
} else if (mode == MODE_EDIT || mode == MODE_EDIT_CURVE) {
control_points_in_range = 0;
// In/out controls can be moved in multiple modes.
if (dist_to_p_out < grab_threshold && i < (curve->get_point_count() - 1)) {
if (dist_to_p_out_squared < grab_threshold_squared && i < (curve->get_point_count() - 1)) {
action = ACTION_MOVING_OUT;
action_point = i;
moving_from = curve->get_point_out(i);
moving_screen_from = gpoint;
orig_in_length = curve->get_point_in(action_point).length();
control_points_in_range += 1;
}
if (dist_to_p_in < grab_threshold && i > 0) {
if (dist_to_p_in_squared < grab_threshold_squared && i > 0) {
action = ACTION_MOVING_IN;
action_point = i;
moving_from = curve->get_point_in(i);
Expand All @@ -137,23 +138,23 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
// Check for point deletion.
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
if ((mb->get_button_index() == MouseButton::RIGHT && (mode == MODE_EDIT || mode == MODE_CREATE)) || (mb->get_button_index() == MouseButton::LEFT && mode == MODE_DELETE)) {
if (dist_to_p < grab_threshold) {
if (dist_to_p_squared < grab_threshold_squared) {
undo_redo->create_action(TTR("Remove Point from Curve"));
undo_redo->add_do_method(curve.ptr(), "remove_point", i);
undo_redo->add_undo_method(curve.ptr(), "add_point", curve->get_point_position(i), curve->get_point_in(i), curve->get_point_out(i), i);
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
undo_redo->commit_action();
return true;
} else if (dist_to_p_out < grab_threshold) {
} else if (dist_to_p_out_squared < grab_threshold_squared) {
undo_redo->create_action(TTR("Remove Out-Control from Curve"));
undo_redo->add_do_method(curve.ptr(), "set_point_out", i, Vector2());
undo_redo->add_undo_method(curve.ptr(), "set_point_out", i, curve->get_point_out(i));
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
undo_redo->commit_action();
return true;
} else if (dist_to_p_in < grab_threshold) {
} else if (dist_to_p_in_squared < grab_threshold_squared) {
undo_redo->create_action(TTR("Remove In-Control from Curve"));
undo_redo->add_do_method(curve.ptr(), "set_point_in", i, Vector2());
undo_redo->add_undo_method(curve.ptr(), "set_point_in", i, curve->get_point_in(i));
Expand Down Expand Up @@ -342,25 +343,25 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
// Find edge
edge_point = xform.xform(curve->get_closest_point(xform.affine_inverse().xform(mm->get_position())));
on_edge = false;
if (edge_point.distance_to(gpoint) <= grab_threshold) {
if (edge_point.distance_squared_to(gpoint) <= grab_threshold_squared) {
on_edge = true;
}
// However, if near a control point or its in-out handles then not on edge
int len = curve->get_point_count();
for (int i = 0; i < len; i++) {
Vector2 pp = curve->get_point_position(i);
Vector2 p = xform.xform(pp);
if (p.distance_to(gpoint) <= grab_threshold) {
if (p.distance_squared_to(gpoint) <= grab_threshold_squared) {
on_edge = false;
break;
}
p = xform.xform(pp + curve->get_point_in(i));
if (p.distance_to(gpoint) <= grab_threshold) {
if (p.distance_squared_to(gpoint) <= grab_threshold_squared) {
on_edge = false;
break;
}
p = xform.xform(pp + curve->get_point_out(i));
if (p.distance_to(gpoint) <= grab_threshold) {
if (p.distance_squared_to(gpoint) <= grab_threshold_squared) {
on_edge = false;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion editor/scene/2d/physics/cast_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool Cast2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
Vector2 gpoint = mb->get_position();

if (mb->is_pressed()) {
if (xform.xform(target_position).distance_to(gpoint) < 8) {
if (xform.xform(target_position).distance_squared_to(gpoint) < 64.0f) {
pressed = true;
original_target_position = target_position;
original_mouse_pos = gpoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_e
if (mb->get_button_index() == MouseButton::LEFT) {
if (mb->is_pressed()) {
for (int i = 0; i < handles.size(); i++) {
if (xform.xform(handles[i]).distance_to(gpoint) < grab_threshold) {
if (xform.xform(handles[i]).distance_squared_to(gpoint) < (grab_threshold * grab_threshold)) {
edit_handle = i;

break;
Expand Down
3 changes: 2 additions & 1 deletion editor/scene/2d/polygon_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ void Polygon2DEditor::_canvas_input(const Ref<InputEvent> &p_input) {
int pc = painted_weights.size();
real_t amount = bone_paint_strength->get_value();
real_t radius = bone_paint_radius->get_value() * EDSCALE;
real_t radius_squared = radius * radius;

if (selected_action == ACTION_CLEAR_WEIGHT) {
amount = -amount;
Expand All @@ -891,7 +892,7 @@ void Polygon2DEditor::_canvas_input(const Ref<InputEvent> &p_input) {
const Vector2 *rv = editing_points.ptr();

for (int i = 0; i < pc; i++) {
if (mtx.xform(rv[i]).distance_to(bone_paint_pos) < radius) {
if (mtx.xform(rv[i]).distance_squared_to(bone_paint_pos) < radius_squared) {
w[i] = CLAMP(r[i] + amount, 0, 1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion editor/scene/2d/sprite_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, float
Vector2 prev = Vector2(p2[lasti].x, p2[lasti].y);
for (uint64_t i = 0; i < p2.size(); i++) {
Vector2 cur = Vector2(p2[i].x, p2[i].y);
if (cur.distance_to(prev) > 0.5) {
if (cur.distance_squared_to(prev) > 0.25f) {
outPoints.push_back(cur);
prev = cur;
}
Expand Down
14 changes: 8 additions & 6 deletions editor/scene/2d/tiles/tile_data_editors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,18 @@ void GenericTilePolygonEditor::_advanced_menu_item_pressed(int p_item_pressed) {

void GenericTilePolygonEditor::_grab_polygon_point(Vector2 p_pos, const Transform2D &p_polygon_xform, int &r_polygon_index, int &r_point_index) {
const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
const real_t grab_threshold_squared = grab_threshold * grab_threshold;
r_polygon_index = -1;
r_point_index = -1;
float closest_distance = grab_threshold + 1.0;
real_t closest_distance_squared = (grab_threshold + 1.0) * (grab_threshold + 1.0);
for (unsigned int i = 0; i < polygons.size(); i++) {
const Vector<Vector2> &polygon = polygons[i];
for (int j = 0; j < polygon.size(); j++) {
float distance = p_pos.distance_to(p_polygon_xform.xform(polygon[j]));
if (distance < grab_threshold && distance < closest_distance) {
const real_t distance_squared = p_pos.distance_squared_to(p_polygon_xform.xform(polygon[j]));
if (distance_squared < grab_threshold_squared && distance_squared < closest_distance_squared) {
r_polygon_index = i;
r_point_index = j;
closest_distance = distance;
closest_distance_squared = distance_squared;
}
}
}
Expand Down Expand Up @@ -514,7 +515,8 @@ void GenericTilePolygonEditor::_base_control_gui_input(Ref<InputEvent> p_event)
undo_redo = memnew(EditorUndoRedoManager);
}

real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
const real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
const real_t grab_threshold_squared = grab_threshold * grab_threshold;

hovered_polygon_index = -1;
hovered_point_index = -1;
Expand Down Expand Up @@ -582,7 +584,7 @@ void GenericTilePolygonEditor::_base_control_gui_input(Ref<InputEvent> p_event)
}
if (tools_button_group->get_pressed_button() == button_create) {
// Create points.
if (in_creation_polygon.size() >= 3 && mb->get_position().distance_to(xform.xform(in_creation_polygon[0])) < grab_threshold) {
if (in_creation_polygon.size() >= 3 && mb->get_position().distance_squared_to(xform.xform(in_creation_polygon[0])) < grab_threshold_squared) {
// Closes and create polygon.
if (!multiple_polygon_mode) {
clear_polygons();
Expand Down
4 changes: 2 additions & 2 deletions editor/scene/2d/tiles/tile_set_atlas_source_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ void TileSetAtlasSourceEditor::_tile_atlas_control_gui_input(const Ref<InputEven
_update_current_tile_data_editor();
}
} else if (drag_type == DRAG_TYPE_MAY_POPUP_MENU) {
if (Vector2(drag_start_mouse_pos).distance_to(tile_atlas_control->get_local_mouse_position()) > 5.0 * EDSCALE) {
if (Vector2(drag_start_mouse_pos).distance_squared_to(tile_atlas_control->get_local_mouse_position()) > (5.0f * EDSCALE) * (5.0f * EDSCALE)) {
drag_type = DRAG_TYPE_NONE;
}
} else if (drag_type >= DRAG_TYPE_RESIZE_TOP_LEFT && drag_type <= DRAG_TYPE_RESIZE_LEFT) {
Expand Down Expand Up @@ -1949,7 +1949,7 @@ void TileSetAtlasSourceEditor::_tile_alternatives_control_gui_input(const Ref<In
alternative_tiles_control_unscaled->queue_redraw();

if (drag_type == DRAG_TYPE_MAY_POPUP_MENU) {
if (Vector2(drag_start_mouse_pos).distance_to(alternative_tiles_control->get_local_mouse_position()) > 5.0 * EDSCALE) {
if (Vector2(drag_start_mouse_pos).distance_squared_to(alternative_tiles_control->get_local_mouse_position()) > (5.0f * EDSCALE) * (5.0f * EDSCALE)) {
drag_type = DRAG_TYPE_NONE;
}
}
Expand Down
5 changes: 3 additions & 2 deletions editor/scene/3d/node_3d_editor_gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "scene/resources/3d/primitive_meshes.h"

#define HANDLE_HALF_SIZE 9.5
#define HANDLE_HALF_SIZE_SQUARED (HANDLE_HALF_SIZE * HANDLE_HALF_SIZE)

bool EditorNode3DGizmo::is_editable() const {
ERR_FAIL_NULL_V(spatial_node, false);
Expand Down Expand Up @@ -611,7 +612,7 @@ void EditorNode3DGizmo::handles_intersect_ray(Camera3D *p_camera, const Vector2
Vector3 hpos = t.xform(secondary_handles[i]);
Vector2 p = p_camera->unproject_position(hpos);

if (p.distance_to(p_point) < HANDLE_HALF_SIZE) {
if (p.distance_squared_to(p_point) < HANDLE_HALF_SIZE_SQUARED) {
real_t dp = p_camera->get_transform().origin.distance_to(hpos);
if (dp < min_d) {
min_d = dp;
Expand All @@ -635,7 +636,7 @@ void EditorNode3DGizmo::handles_intersect_ray(Camera3D *p_camera, const Vector2
Vector3 hpos = t.xform(handles[i]);
Vector2 p = p_camera->unproject_position(hpos);

if (p.distance_to(p_point) < HANDLE_HALF_SIZE) {
if (p.distance_squared_to(p_point) < HANDLE_HALF_SIZE_SQUARED) {
real_t dp = p_camera->get_transform().origin.distance_to(hpos);
if (dp < min_d) {
min_d = dp;
Expand Down
Loading
Loading