Curve points to global position or transforming any postion to global

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By websterek

I’m looking for possibility to find global position of curve points after rotation or even better: for method to transform any position to global pos.

I need to rotate and move my path along the level but also I need global position of curve points and of course getting curve points pos after rotating path result with vector relative to the parent

Best!

:bust_in_silhouette: Reply From: Kanor

This is necro-ing an old question, but for people who see this question in the future:

Basic Global Position of Points:

“Global_position” is an object’s position compared against root. The calculation for it is adding up positions so you know what a node’s TOTAL position is when you ignore all the children-ing and parent-ing:

a.global_position = a.position + a.parent.position + a.parent.parent.position + ... + root.position

If you were to calculate the parent’s global position it would also be:

a.parent.global_position = a.parent.position + a.parent.parent.position + ... + root.position

Using this, we can calculate any position by just adding the position with the global position of the parent! So:

a.global_position = a.position + a.parent.global_position

For this question, if we have a curve2D on a Path2D node, the points as global variables can be calculated as:

Path2D.curve.get_point_position(index) + Path2D.global_position

Yay!

Global Rotation:

The rotation quirk muddies this up a bit, since the curve points wouldn’t rotate with the Path2D (probably. I haven’t tested it). The probable solution would be to add the curve’s position AFTER rotating it using Vector2.rotated:

Path2D.curve.get_point_position(index).rotated(deg2rad(Path2D.rotation_degrees)) + Path2D.global_position

For getting the entire curve, do a quick for loop over the curve and do this calculation on each one.

Happy coding!

1 Like