Skip to content

Fix selecting wrong vertices in the Polygon2D editor when adding a polygon #108249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
8 changes: 3 additions & 5 deletions editor/scene/2d/polygon_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,14 +640,12 @@ void Polygon2DEditor::_canvas_input(const Ref<InputEvent> &p_input) {

if (current_action == ACTION_ADD_POLYGON) {
int closest = -1;
real_t closest_dist = 1e20;

for (int i = 0; i < editing_points.size(); i++) {
for (int i = editing_points.size() - 1; i >= 0; i--) {
Vector2 tuv = mtx.xform(editing_points[i]);
real_t dist = tuv.distance_to(mb->get_position());
if (dist < 8 && dist < closest_dist) {
if (tuv.distance_to(mb->get_position()) < 8) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (tuv.distance_to(mb->get_position()) < 8) {
if (tuv.distance_squared_to(mb->get_position()) < 64.0) {

Better to avoid the square root if we don't need the result here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the only place where it makes sense. I can change it everywhere here or create another pull request with these optimizations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Been looking into that myself so might make a PR soon, just to avoid double work

closest = i;
closest_dist = dist;
break;
}
}

Expand Down