0 votes

On a spatial node calling get_global_transform().basis.get_scale() and scale both return the same values regardless of the global rotation of the node. I want to be able to scale a node by a specific amount along a global axis even if it has been rotated. How do a find a Spatial's scale values in global XYZ so I can do this?

Thank you!!

Godot version 3.3
in Engine by (237 points)

2 Answers

0 votes
Best answer

Here is how I solved this for anyone who may find this in the future.

# scales the Spatial by the amount in the given global direction
func scaleDirection(amount: Vector3, camRot: Vector3):

    var selfQuat = transform.basis.get_rotation_quat()  # get the global rotation
    var sclXform = Transform(selfQuat) # make it a transform so we can apply it to a vector

    var globalScale = sclXform.xform(scale) # convert local scale to scale on global axis'
    globalScale = globalScale.abs() # scale should always be positive but rotation can make it negative when converted to global

    # when camera is behind the object axis direction must be inverted:
    if(camRot.y < 0):  
        amount.x = -amount.x 
        pass
    if(camRot.y > 90 or camRot.y < -90):
        amount.z = -amount.z 
        pass

    # calculate new scale amount because we are not setting the scale directly
    var newScale = -amount + globalScale  #  invert amount  here to change side which moves

    # actually apply changes
    global_scale(newScale)

With this function applied to a Spatial you can set the input value amount to how many units you would like to scale the Spatial in global coordinates. It is not a ratio, for example if your Spatial was already 5 units long on the global X axis and you set amount to (2,0,0) it would scale your Spatial so it was now 7 units long on the global X axis. It should work no matter the Spatials starting rotation or scale. The camera stuff is optional depending on your use case.

Please let me know if there is a simpler way to do this, I have a feeling I have over thought it, but hey, it works!

by (237 points)
0 votes

You can just use your_node.global_scale(scaling_vector) on a spatial node to scale it in world coordinates. Replace yournode with the actual node and scalingvector with a vector with the scaling amount for each axis.

by (1,051 points)

The problem is that I need to know the starting global scale value to know how much to scale by. Say I have a Spatial Node that has already been scaled and rotated some amount. I want to scale it to be 5 units long on the global X axis. I need to know the starting scale on the global X axis to know how much to scale it by to get it to 5 units long.

Thanks for the help!

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 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 webmaster@godotengine.org with your username.