DEV Community

Thibaut Andrieu
Thibaut Andrieu

Posted on

Tiny Tip : How to name your variable when dealing with 3D spaces.

When dealing with 3D, we often have to do space change. And we often end up with this kind of code:

var pointPos = new Vector3(...)
var transform = new Matrix4x4(...)
var newPos = tranform * pointPos;
Enter fullscreen mode Exit fullscreen mode

A good way to avoid bugs is to always specify in which space variable are expressed:

var posInPlayerSpace = new Vector3(...)
var playerSpaceToWorldSpace = new Matrix4x4(...)
var posInWorldSpace = playerSpaceToWorldSpace * posInPlayerSpace;
Enter fullscreen mode Exit fullscreen mode

Now, spot the bug in the line below πŸ˜‰

var posInWorldSpace = playerSpaceToWorldSpace * posInViewSpace;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)