From dddf8fd5b9bee61f13436d22577f0fedcc2ebb91 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 27 Aug 2016 13:13:32 +1000 Subject: [PATCH] Add some ASCII drawings explaining yaw/pitch, move some direction calculation into a common utils class. --- Blocks/Behaviour/DeleteBehaviour.cs | 13 ++---- Commands/building/CmdDrill.cs | 5 +-- MCGalaxy_.csproj | 4 +- util/Math/DirUtils.cs | 67 +++++++++++++++++++++++++++++ util/{ => Math}/Vectors.cs | 2 +- 5 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 util/Math/DirUtils.cs rename util/{ => Math}/Vectors.cs (99%) diff --git a/Blocks/Behaviour/DeleteBehaviour.cs b/Blocks/Behaviour/DeleteBehaviour.cs index 5c6b11a4f..1990c965a 100644 --- a/Blocks/Behaviour/DeleteBehaviour.cs +++ b/Blocks/Behaviour/DeleteBehaviour.cs @@ -27,22 +27,15 @@ namespace MCGalaxy.BlockBehaviour { int dx = 0, dy = 0, dz = 0; p.RevertBlock(x, y, z); - if ( p.rot[0] < 48 || p.rot[0] > ( 256 - 48 ) ) - dz = -1; - else if ( p.rot[0] > ( 128 - 48 ) && p.rot[0] < ( 128 + 48 ) ) - dz = 1; - - if ( p.rot[0] > ( 64 - 48 ) && p.rot[0] < ( 64 + 48 ) ) - dx = 1; - else if ( p.rot[0] > ( 192 - 48 ) && p.rot[0] < ( 192 + 48 ) ) - dx = -1; + DirUtils.EightYaw(p.rot[0], out dx, out dz); if ( p.rot[1] >= 192 && p.rot[1] <= ( 192 + 32 ) ) dy = 1; else if ( p.rot[1] <= 64 && p.rot[1] >= 32 ) dy = -1; - if ( 192 <= p.rot[1] && p.rot[1] <= 196 || 60 <= p.rot[1] && p.rot[1] <= 64 ) { dx = 0; dz = 0; } + // Looking straight up or down + if (p.rot[1] >= 192 && p.rot[1] <= 196 || p.rot[1] >= 60 && p.rot[1] <= 64) { dx = 0; dz = 0; } byte b1 = p.level.GetTile((ushort)( x + dx * 2 ), (ushort)( y + dy * 2 ), (ushort)( z + dz * 2 )); byte b2 = p.level.GetTile((ushort)( x + dx ), (ushort)( y + dy ), (ushort)( z + dz )); diff --git a/Commands/building/CmdDrill.cs b/Commands/building/CmdDrill.cs index 7bbd8f5bf..8c7b772f4 100644 --- a/Commands/building/CmdDrill.cs +++ b/Commands/building/CmdDrill.cs @@ -40,10 +40,7 @@ namespace MCGalaxy.Commands.Building { int dist = (int)state; int dx = 0, dz = 0; - if (p.rot[0] <= 32 || p.rot[0] >= 224) { dz = -1; } - else if (p.rot[0] <= 96) { dx = 1; } - else if (p.rot[0] <= 160) { dz = 1; } - else dx = -1; + DirUtils.FourYaw(p.rot[0], out dx, out dz); List buffer = new List(); int depth = 0; diff --git a/MCGalaxy_.csproj b/MCGalaxy_.csproj index 73e4bcc86..3b00b9721 100644 --- a/MCGalaxy_.csproj +++ b/MCGalaxy_.csproj @@ -607,10 +607,11 @@ + + - @@ -720,6 +721,7 @@ + diff --git a/util/Math/DirUtils.cs b/util/Math/DirUtils.cs new file mode 100644 index 000000000..f684650db --- /dev/null +++ b/util/Math/DirUtils.cs @@ -0,0 +1,67 @@ +/* + Copyright 2015 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; + +namespace MCGalaxy { + + public static class DirUtils { + + /* How yaw works: * How pitch works: + * 64 | +X * 192 | +Y + * ___|___ * | + * / | \ * flipped | + * / | \ * heads | + * 128 | | | 0 * 128 | 0 + *------------+----------- * ------------+------------ + * +Z | | | -Z * Y=0 | Y=0 + * \ | / * flipped | + * \___|___/ * heads | + * | * | + * 192 | -X * 64 | -Y + * */ + + public static void EightYaw(byte yaw, out int dirX, out int dirZ) { + dirX = 0; dirZ = 0; + const byte extent = 48; + + if (yaw < (0 + extent) || yaw > (256 - extent)) + dirZ = -1; + else if (yaw > (128 - extent) && yaw < (128 + extent)) + dirZ = 1; + + if (yaw > (64 - extent) && yaw < (64 + extent)) + dirX = 1; + else if (yaw > (192 - extent) && yaw < (192 + extent)) + dirX = -1; + } + + public static void FourYaw(byte yaw, out int dirX, out int dirZ) { + dirX = 0; dirZ = 0; + const byte quadrant = 32; + + if (yaw <= (0 + quadrant) || yaw >= (256 - quadrant)) + dirZ = -1; + else if (yaw <= (128 - quadrant)) + dirX = 1; + else if (yaw <= (128 + quadrant)) + dirZ = 1; + else + dirX = -1; + } + } +} diff --git a/util/Vectors.cs b/util/Math/Vectors.cs similarity index 99% rename from util/Vectors.cs rename to util/Math/Vectors.cs index 1da9c1b2b..edf6cb40f 100644 --- a/util/Vectors.cs +++ b/util/Math/Vectors.cs @@ -18,7 +18,7 @@ using System; namespace MCGalaxy { - public struct Vec3U16 : IEquatable { + public struct Vec3U16 : IEquatable { public ushort X, Y, Z; public static Vec3U16 Zero = new Vec3U16(0); public static Vec3U16 MinVal = new Vec3U16(ushort.MinValue);