localToWorld vs getWorldPosition

Hiya, this is more of a general question as I’m still relatively new to 3d programming and game dev.

I’ve seen this being used interchangeable to get the world position of an object. ChatGPT said that:
" Yes, that’s correct! If you need the position of an object in the world space, it’s generally better to use GetWorldPosition. This method directly gives you the global position of the object, which is often what you’re looking for when interacting with or placing objects in a scene.

LocalToWorld is more useful when you’re working with specific points or vertices within an object’s local space and need to transform them into world space. But for straightforward world position retrieval, GetWorldPosition is the more direct and easier choice."

Which kind of makes sense, but not exactly, as I still see these being used interchangeable, when just trying to find an objects world position. So is it true that, the localToWorld is often used when you’re dealing with transformations at a lower level, such as matrices and vectors, on their respectable objects localPosition in comparison to the getWorldPosition, that deals with a higher level of the object as a whole in comparison to its world position?

Both calculate global positions. There are two differences:

  • versatility: localToWorld works on user-provided local point, while getWorldPosition works only on a predefined point – the origin of an object (i.e. its (0,0,0) point)
  • performance: localToWorld is slightly slower than getWorldPosition, because the latter just extract the position from a matrix, instead of making a full-scale matrix multiplication

Disclaimer: this is how I think these functions work, it is good to check this with some small example. Generally, use localToWorld when you query for a point which is not (0,0,0), otherwise you can use both functions, but getWorldPosition is faster.

3 Likes

Thank you!