From 7ca1e00fcd4872f90418a2f23e38eb6d2bbef435 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 8 Jul 2018 21:53:13 +1000 Subject: [PATCH] minor code cleanup --- ClassicalSharp/2D/Screens/ChatScreen.cs | 15 +- .../Math/Physics/IntersectionUtils.cs | 156 +++++++++--------- src/Client/Physics.c | 2 +- src/Client/Screens.c | 13 +- 4 files changed, 94 insertions(+), 92 deletions(-) diff --git a/ClassicalSharp/2D/Screens/ChatScreen.cs b/ClassicalSharp/2D/Screens/ChatScreen.cs index b858fe75c..5eeb7265d 100644 --- a/ClassicalSharp/2D/Screens/ChatScreen.cs +++ b/ClassicalSharp/2D/Screens/ChatScreen.cs @@ -182,14 +182,15 @@ namespace ClassicalSharp.Gui.Screens { static FastColour backColour = new FastColour(0, 0, 0, 127); public void RenderBackground() { - int usedHeight = normalChat.GetUsedHeight(); - int y = normalChat.Y + normalChat.Height - usedHeight - 5; - int x = normalChat.X - 5; - int width = Math.Max(clientStatus.Width, normalChat.Width) + 10; + int chatHeight = normalChat.GetUsedHeight(); + int x = normalChat.X; + int y = normalChat.Y + normalChat.Height - chatHeight; - int boxHeight = usedHeight + clientStatus.GetUsedHeight(); - if (boxHeight > 0) { - game.Graphics.Draw2DQuad(x, y, width, boxHeight + 10, backColour); + int width = Math.Max(clientStatus.Width, normalChat.Width) + 10; + int height = chatHeight + clientStatus.GetUsedHeight(); + + if (height > 0) { + game.Graphics.Draw2DQuad(x - 5, y - 5, width + 10, height + 10, backColour); } } diff --git a/ClassicalSharp/Math/Physics/IntersectionUtils.cs b/ClassicalSharp/Math/Physics/IntersectionUtils.cs index 5931251ed..55e93bfe3 100644 --- a/ClassicalSharp/Math/Physics/IntersectionUtils.cs +++ b/ClassicalSharp/Math/Physics/IntersectionUtils.cs @@ -1,78 +1,78 @@ -// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using ClassicalSharp.Entities; -using OpenTK; - -namespace ClassicalSharp.Physics { - - /// Contains various methods for intersecting geometry. - public static class Intersection { - - /// Calculates the intersection points of a ray and a rotated bounding box. - public static bool RayIntersectsRotatedBox(Vector3 origin, Vector3 dir, Entity target, out float tMin, out float tMax) { - // This is the rotated AABB of the model we want to test for intersection - // * - // / \ we then perform a counter *---* and we can then do - // ====>* x * rotation to the rotated AABB | x | a standard AABB test - // \ / and ray origin and direction *---* with the rotated ray - // * / - // / - origin = InverseRotate(origin - target.Position, target) + target.Position; - dir = InverseRotate(dir, target); - AABB bb = target.PickingBounds; - return RayIntersectsBox(origin, dir, bb.Min, bb.Max, out tMin, out tMax); - } - - static Vector3 InverseRotate(Vector3 pos, Entity target) { - pos = Utils.RotateY(pos, -target.RotY * Utils.Deg2Rad); - pos = Utils.RotateZ(pos, -target.RotZ * Utils.Deg2Rad); - pos = Utils.RotateX(pos, -target.RotX * Utils.Deg2Rad); - return pos; - } - - //http://www.cs.utah.edu/~awilliam/box/box.pdf - /// Calculates the intersection points of a ray and a bounding box. - public static bool RayIntersectsBox(Vector3 origin, Vector3 dir, Vector3 min, Vector3 max, - out float t0, out float t1) { - t0 = t1 = 0; - float tmin, tmax, tymin, tymax, tzmin, tzmax; - float invDirX = 1 / dir.X; - if (invDirX >= 0) { - tmin = (min.X - origin.X) * invDirX; - tmax = (max.X - origin.X) * invDirX; - } else { - tmin = (max.X - origin.X) * invDirX; - tmax = (min.X - origin.X) * invDirX; - } - - float invDirY = 1 / dir.Y; - if (invDirY >= 0) { - tymin = (min.Y - origin.Y) * invDirY; - tymax = (max.Y - origin.Y) * invDirY; - } else { - tymin = (max.Y - origin.Y) * invDirY; - tymax = (min.Y - origin.Y) * invDirY; - } - if (tmin > tymax || tymin > tmax) return false; - if (tymin > tmin) tmin = tymin; - if (tymax < tmax) tmax = tymax; - - float invDirZ = 1 / dir.Z; - if (invDirZ >= 0) { - tzmin = (min.Z - origin.Z) * invDirZ; - tzmax = (max.Z - origin.Z) * invDirZ; - } else { - tzmin = (max.Z - origin.Z) * invDirZ; - tzmax = (min.Z - origin.Z) * invDirZ; - } - - if (tmin > tzmax || tzmin > tmax) return false; - if (tzmin > tmin) tmin = tzmin; - if (tzmax < tmax) tmax = tzmax; - - t0 = tmin; - t1 = tmax; - return t0 >= 0; - } - } -} +// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 +using System; +using ClassicalSharp.Entities; +using OpenTK; + +namespace ClassicalSharp.Physics { + + /// Contains various methods for intersecting geometry. + public static class Intersection { + + /// Calculates the intersection points of a ray and a rotated bounding box. + public static bool RayIntersectsRotatedBox(Vector3 origin, Vector3 dir, Entity target, out float tMin, out float tMax) { + // This is the rotated AABB of the model we want to test for intersection + // * + // / \ we then perform a counter *---* and we can then do + // ====>* x * rotation to the rotated AABB | x | a standard AABB test + // \ / and ray origin and direction *---* with the rotated ray + // * / + // / + origin = InverseRotate(origin - target.Position, target) + target.Position; + dir = InverseRotate(dir, target); + AABB bb = target.PickingBounds; + return RayIntersectsBox(origin, dir, bb.Min, bb.Max, out tMin, out tMax); + } + + static Vector3 InverseRotate(Vector3 pos, Entity target) { + pos = Utils.RotateY(pos, -target.RotY * Utils.Deg2Rad); + pos = Utils.RotateZ(pos, -target.RotZ * Utils.Deg2Rad); + pos = Utils.RotateX(pos, -target.RotX * Utils.Deg2Rad); + return pos; + } + + //http://www.cs.utah.edu/~awilliam/box/box.pdf + /// Calculates the intersection points of a ray and a bounding box. + public static bool RayIntersectsBox(Vector3 origin, Vector3 dir, Vector3 min, Vector3 max, + out float t0, out float t1) { + t0 = t1 = 0; + float tmin, tmax, tymin, tymax, tzmin, tzmax; + float invDirX = 1 / dir.X; + if (invDirX >= 0) { + tmin = (min.X - origin.X) * invDirX; + tmax = (max.X - origin.X) * invDirX; + } else { + tmin = (max.X - origin.X) * invDirX; + tmax = (min.X - origin.X) * invDirX; + } + + float invDirY = 1 / dir.Y; + if (invDirY >= 0) { + tymin = (min.Y - origin.Y) * invDirY; + tymax = (max.Y - origin.Y) * invDirY; + } else { + tymin = (max.Y - origin.Y) * invDirY; + tymax = (min.Y - origin.Y) * invDirY; + } + if (tmin > tymax || tymin > tmax) return false; + if (tymin > tmin) tmin = tymin; + if (tymax < tmax) tmax = tymax; + + float invDirZ = 1 / dir.Z; + if (invDirZ >= 0) { + tzmin = (min.Z - origin.Z) * invDirZ; + tzmax = (max.Z - origin.Z) * invDirZ; + } else { + tzmin = (max.Z - origin.Z) * invDirZ; + tzmax = (min.Z - origin.Z) * invDirZ; + } + + if (tmin > tzmax || tzmin > tmax) return false; + if (tzmin > tmin) tmin = tzmin; + if (tzmax < tmax) tmax = tzmax; + + t0 = tmin; + t1 = tmax; + return tmin >= 0; + } + } +} diff --git a/src/Client/Physics.c b/src/Client/Physics.c index 33a32b541..1a48750e4 100644 --- a/src/Client/Physics.c +++ b/src/Client/Physics.c @@ -120,7 +120,7 @@ bool Intersection_RayIntersectsBox(Vector3 origin, Vector3 dir, Vector3 min, Vec if (tzmax < tmax) tmax = tzmax; *t0 = tmin; *t1 = tmax; - return *t0 >= 0; + return tmin >= 0.0f; } diff --git a/src/Client/Screens.c b/src/Client/Screens.c index 4a77fd92b..3a3fa19fc 100644 --- a/src/Client/Screens.c +++ b/src/Client/Screens.c @@ -846,14 +846,15 @@ static void ChatScreen_CheckOtherStatuses(ChatScreen* screen) { static void ChatScreen_RenderBackground(ChatScreen* screen) { Int32 usedHeight = TextGroupWidget_UsedHeight(&screen->Chat); - Int32 y = screen->Chat.Y + screen->Chat.Height - usedHeight - 5; - Int32 x = screen->Chat.X - 5; - Int32 width = max(screen->ClientStatus.Width, screen->Chat.Width) + 10; + Int32 x = screen->Chat.X; + Int32 y = screen->Chat.Y + screen->Chat.Height - usedHeight; - Int32 boxHeight = usedHeight + TextGroupWidget_UsedHeight(&screen->ClientStatus); - if (boxHeight > 0) { + Int32 width = max(screen->ClientStatus.Width, screen->Chat.Width); + Int32 height = usedHeight + TextGroupWidget_UsedHeight(&screen->ClientStatus); + + if (height > 0) { PackedCol backCol = PACKEDCOL_CONST(0, 0, 0, 127); - GfxCommon_Draw2DFlat(x, y, width, boxHeight + 10, backCol); + GfxCommon_Draw2DFlat(x - 5, y - 5, width + 10, height + 10, backCol); } }