0 votes

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!

in Engine by (227 points)

1 Answer

+1 vote

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!

by (181 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.