Skip to content
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

Fix invalid 2D global position when read outside tree #75509

Conversation

KoBeWi
Copy link
Member

@KoBeWi KoBeWi commented Mar 30, 2023

MRP for the bug:
repro.tscn.txt

When you get global_position of a CanvasItem outside tree, it will assign its position to global position (roughly speaking). When the node enters tree, its global transform is not invalidated, so global_position will still return its position. This also leads to some abnormalities, like sprite not displaying at its real position (as observed in the MRP).

Bugsquad edit: Fixes #75676. Fixes #76327.

@KoBeWi KoBeWi added this to the 4.1 milestone Mar 30, 2023
@KoBeWi KoBeWi requested a review from a team as a code owner March 30, 2023 20:42
@akien-mga akien-mga requested a review from lawnjelly April 3, 2023 09:42
@kleonc
Copy link
Member

kleonc commented Apr 5, 2023

The global transform can still be not invalidated when changing top level for a CanvasItem outside the tree (it would need to be invalidated for the whole branch):

void CanvasItem::set_as_top_level(bool p_top_level) {
if (top_level == p_top_level) {
return;
}
if (!is_inside_tree()) {
top_level = p_top_level;
return;
}
_exit_canvas();
top_level = p_top_level;
_top_level_changed();
_enter_canvas();
_notify_transform();
}

@KoBeWi
Copy link
Member Author

KoBeWi commented Apr 18, 2023

@kleonc Does it lead to any issue? I tried bugging it, but it prints a correct global position.

@kleonc
Copy link
Member

kleonc commented Apr 18, 2023

@kleonc Does it lead to any issue? I tried bugging it, but it prints a correct global position.

Here's an example:

func foo() -> void:
	var a := Node2D.new()
	a.position = Vector2(1, 1)

	var b := Node2D.new()
	b.position = Vector2(2, 2)

	a.add_child(b)

#	b.global_position
	b.top_level = true
	print(b.global_position)

With the b.global_position line commented out it correctly outputs (2, 2).
With the b.global_position line uncommented it incorrectly outputs (3, 3).

@KoBeWi KoBeWi force-pushed the In_physics,_the_observer_effect_is_the_disturbance_of_an_observed_system_by_the_act_of_observation._A_common_example_is_checking_CanvasItem's_global_position,_which_causes_its_transform_to_be_re-calculated- branch from 2c9caf0 to df5d509 Compare April 18, 2023 16:24
@KoBeWi
Copy link
Member Author

KoBeWi commented Apr 18, 2023

Ok fixed.

scene/main/canvas_item.cpp Outdated Show resolved Hide resolved
@KoBeWi KoBeWi force-pushed the In_physics,_the_observer_effect_is_the_disturbance_of_an_observed_system_by_the_act_of_observation._A_common_example_is_checking_CanvasItem's_global_position,_which_causes_its_transform_to_be_re-calculated- branch from df5d509 to 727a4ed Compare April 18, 2023 20:14
Copy link
Member

@kleonc kleonc left a comment

Choose a reason for hiding this comment

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

LGTM.
(haven't actually tested it after the changes though 🙃)

Copy link
Member

@lawnjelly lawnjelly left a comment

Choose a reason for hiding this comment

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

Also looks fine to me.

@akien-mga akien-mga merged commit 3695dfb into godotengine:master Apr 19, 2023
@akien-mga
Copy link
Member

Thanks!

@KoBeWi KoBeWi deleted the In_physics,_the_observer_effect_is_the_disturbance_of_an_observed_system_by_the_act_of_observation._A_common_example_is_checking_CanvasItem's_global_position,_which_causes_its_transform_to_be_re-calculated- branch April 19, 2023 08:41
@YuriSizov
Copy link
Contributor

Cherry-picked for 4.0.3.

@akien-mga akien-mga changed the title Fix invalid global position when read outside tree Fix invalid 2D global position when read outside tree Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants