Better selection box sorting.

This commit is contained in:
UnknownShadow200 2015-06-27 12:11:47 +10:00
parent a6f5ba2f3b
commit ffc49d163e
3 changed files with 15 additions and 15 deletions

View file

@ -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 ) );

View file

@ -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;
}

View file

@ -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 );