From ffc49d163e91527619219f7219c33fdfc6a82a98 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 27 Jun 2015 12:11:47 +1000 Subject: [PATCH] Better selection box sorting. --- Physics/Picking.cs | 3 +-- Selections/SelectionBoxComparer.cs | 23 ++++++++++++----------- Selections/SelectionManager.cs | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Physics/Picking.cs b/Physics/Picking.cs index d1b814529..a814e53f8 100644 --- a/Physics/Picking.cs +++ b/Physics/Picking.cs @@ -72,9 +72,8 @@ namespace ClassicalSharp { // which voxel boundary is nearest) and walk that way. while( iterations < 10000 ) { byte block = map.IsValidPos( x, y, z ) ? map.GetBlock( x, y, z ) : (byte)0; - float height = block == 0 ? 1 : info.BlockHeight( block ); Vector3 min = new Vector3( x, y, z ); - Vector3 max = new Vector3( x + 1, y + height, z + 1 ); + Vector3 max = min + new Vector3( 1, block == 0 ? 1 : info.BlockHeight( block ), 1 ); float dx = Math.Min( Math.Abs( origin.X - min.X ), Math.Abs( origin.X - max.X ) ); float dy = Math.Min( Math.Abs( origin.Y - min.Y ), Math.Abs( origin.Y - max.Y ) ); diff --git a/Selections/SelectionBoxComparer.cs b/Selections/SelectionBoxComparer.cs index a41c64d60..fadbb4514 100644 --- a/Selections/SelectionBoxComparer.cs +++ b/Selections/SelectionBoxComparer.cs @@ -13,27 +13,28 @@ namespace ClassicalSharp.Selections { float maxDistA = float.NegativeInfinity, maxDistB = float.NegativeInfinity; Intersect( a, pos, ref minDistA, ref maxDistA ); Intersect( b, pos, ref minDistB, ref maxDistB ); - return maxDistA == maxDistB ? minDistA.CompareTo( minDistB ) : maxDistA.CompareTo( maxDistB ); + // Reversed comparison order because we need to render back to front for alpha blending. + return minDistA == minDistB ? maxDistB.CompareTo( maxDistA ) : minDistB.CompareTo( minDistA ); } void Intersect( SelectionBox box, Vector3 origin, ref float closest, ref float furthest ) { Vector3I min = box.Min; Vector3I max = box.Max; // Bottom corners - UpdateDist( pos.X, pos.Y, pos.Z, min.X, min.Y, min.Z, ref closest, ref furthest ); - UpdateDist( pos.X, pos.Y, pos.Z, max.X, min.Y, min.Z, ref closest, ref furthest ); - UpdateDist( pos.X, pos.Y, pos.Z, max.X, min.Y, max.Z, ref closest, ref furthest ); - UpdateDist( pos.X, pos.Y, pos.Z, min.X, min.Y, max.Z, ref closest, ref furthest ); + UpdateDist( pos, min.X, min.Y, min.Z, ref closest, ref furthest ); + UpdateDist( pos, max.X, min.Y, min.Z, ref closest, ref furthest ); + UpdateDist( pos, max.X, min.Y, max.Z, ref closest, ref furthest ); + UpdateDist( pos, min.X, min.Y, max.Z, ref closest, ref furthest ); // top corners - UpdateDist( pos.X, pos.Y, pos.Z, min.X, max.Y, min.Z, ref closest, ref furthest ); - UpdateDist( pos.X, pos.Y, pos.Z, max.X, max.Y, min.Z, ref closest, ref furthest ); - UpdateDist( pos.X, pos.Y, pos.Z, max.X, max.Y, max.Z, ref closest, ref furthest ); - UpdateDist( pos.X, pos.Y, pos.Z, min.X, max.Y, max.Z, ref closest, ref furthest ); + UpdateDist( pos, min.X, max.Y, min.Z, ref closest, ref furthest ); + UpdateDist( pos, max.X, max.Y, min.Z, ref closest, ref furthest ); + UpdateDist( pos, max.X, max.Y, max.Z, ref closest, ref furthest ); + UpdateDist( pos, min.X, max.Y, max.Z, ref closest, ref furthest ); } - static void UpdateDist( float x1, float y1, float z1, float x2, float y2, float z2, + static void UpdateDist( Vector3 p, float x2, float y2, float z2, ref float closest, ref float furthest ) { - float dist = Utils.DistanceSquared( x1, y1, z1, x2, y2, z2 ); + float dist = Utils.DistanceSquared( p.X, p.Y, p.Z, x2, y2, z2 ); if( dist < closest ) closest = dist; if( dist > furthest ) furthest = dist; } diff --git a/Selections/SelectionManager.cs b/Selections/SelectionManager.cs index 4e7fa9f47..86c10632f 100644 --- a/Selections/SelectionManager.cs +++ b/Selections/SelectionManager.cs @@ -39,8 +39,8 @@ namespace ClassicalSharp.Selections { Player player = Window.LocalPlayer; pos = player.Position; if( selections.Count == 0 ) return; - // Basically sorts selection boxes back to front for better transparency. - // TODO: Proper selection box sorting. + // TODO: Proper selection box sorting. But this is very difficult because + // we can have boxes within boxes, intersecting boxes, etc.. comparer.pos = pos; selections.Sort( comparer );