ARTICLE AD BOX
I'm currently using Unity (and C#) in a direction comparison system I'm currently making. For a cut down example, these are some directions I have:
{Up, {0,1,0}}, {UpRight, {1,1,0}}, {UpRightForward, {1,1,1}}, {Right, {1,0,0}}, {Down, {0,-1,0}}
Going from Up, I want there to be an equal angle between Up-UpRight and Up-UpRightForward, however using simple angles that isn't the case. Up-UpRIght is 45 degrees, but Up-UpRightForward is ~54.7 degrees. Likewise going from UpRight-UpRightForward is ~35.3 degrees. I understand that this is normal maths but, for the purposes of what I'm doing, I need these to be equal.
Initially I move towards an axis-step system, causing each 'adjacent' direction to be 1 step away using this:
float GetAxisDistance(Vector3 direction, Vector3 targetDirection) { direction = AxisNormalise(direction); targetDirection = AxisNormalise(targetDirection); return Mathf.Abs(direction.x - targetDirection.x) + Mathf.Abs(direction.y - targetDirection.y) + Mathf.Abs(direction.z - targetDirection.z); } Vector3 AxisNormalise(Vector3 direction) { if (direction == Vector3.zero) return Vector3.zero; return direction / Mathf.Max(Mathf.Abs(direction.x), Mathf.Abs(direction.y), Mathf.Abs(direction.z)); }This mostly works, however there are a couple of problems that I'm still unsatisfied with. Firstly Up-UpRightForward remains 2 steps (should be 1), and Up-Down is 2 steps (should be 4).
I'm currently looking into ways to rework this system using adjacent direction pathfinding (so to get from Up-Down it would have to pathfind through adjacent directions until it reaches it, then return the quickest path, resulting in 4 steps), but I am struggling to fully work out the maths for this.
Any help or suggestions would be greatly appreciated. I'm not opposed to changing how my directions are defined since I know that {0,1,0} is shorter than {1,1,0}.
