From 61ef392639ca31b5bb3f4335ca92adebee4467b6 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 3 Feb 2017 17:30:21 +1100 Subject: [PATCH] Optimise collision detection --- .../Entities/Components/CollisionsComponent.cs | 8 +++++--- ClassicalSharp/Entities/Entity.cs | 9 +++++---- ClassicalSharp/Math/Physics/AABB.cs | 4 ++-- ClassicalSharp/Math/Physics/Searcher.cs | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/ClassicalSharp/Entities/Components/CollisionsComponent.cs b/ClassicalSharp/Entities/Components/CollisionsComponent.cs index ea2544b93..73de5f7c2 100644 --- a/ClassicalSharp/Entities/Components/CollisionsComponent.cs +++ b/ClassicalSharp/Entities/Components/CollisionsComponent.cs @@ -44,13 +44,15 @@ namespace ClassicalSharp.Entities { hitXMin = false; hitYMin = false; hitZMin = false; hitXMax = false; hitYMax = false; hitZMax = false; AABB blockBB = default(AABB); + Vector3 bPos; for (int i = 0; i < count; i++) { State state = Searcher.stateCache[i]; - Vector3 blockPos = new Vector3(state.X >> 3, state.Y >> 3, state.Z >> 3); + bPos.X = state.X >> 3; bPos.Y = state.Y >> 3; bPos.Z = state.Z >> 3; + int block = (state.X & 0x7) | (state.Y & 0x7) << 3 | (state.Z & 0x7) << 6; - blockBB.Min = blockPos + info.MinBB[block]; - blockBB.Max = blockPos + info.MaxBB[block]; + blockBB.Min = bPos + info.MinBB[block]; + blockBB.Max = bPos + info.MaxBB[block]; if (!entityExtentBB.Intersects(blockBB)) continue; float tx = 0, ty = 0, tz = 0; diff --git a/ClassicalSharp/Entities/Entity.cs b/ClassicalSharp/Entities/Entity.cs index b12ea8b70..8d29e41f4 100644 --- a/ClassicalSharp/Entities/Entity.cs +++ b/ClassicalSharp/Entities/Entity.cs @@ -51,9 +51,7 @@ namespace ClassicalSharp.Entities { } /// Returns the size of the model that is used for collision detection. - public Vector3 Size { - get { UpdateModel(); return Model.CollisionSize * ModelScale; } - } + public Vector3 Size; void UpdateModel() { BlockModel model = Model as BlockModel; @@ -109,7 +107,10 @@ namespace ClassicalSharp.Entities { Model = game.ModelCache.Get(ModelName); ParseScale(scale); lastModelChange = DateTime.UtcNow; - MobTextureId = -1; + MobTextureId = -1; + + UpdateModel(); + Size = Model.CollisionSize * ModelScale; } void ParseScale(string scale) { diff --git a/ClassicalSharp/Math/Physics/AABB.cs b/ClassicalSharp/Math/Physics/AABB.cs index a0bfaef35..ddc042619 100644 --- a/ClassicalSharp/Math/Physics/AABB.cs +++ b/ClassicalSharp/Math/Physics/AABB.cs @@ -14,8 +14,8 @@ namespace ClassicalSharp.Physics { public float Length { get { return Max.Z - Min.Z; } } public AABB(float x1, float y1, float z1, float x2, float y2, float z2) { - Min = new Vector3(x1, y1, z1); - Max = new Vector3(x2, y2, z2); + Min.X = x1; Min.Y = y1; Min.Z = z1; + Max.X = x2; Max.Y = y2; Max.Z = z2; } public AABB(Vector3 min, Vector3 max) { diff --git a/ClassicalSharp/Math/Physics/Searcher.cs b/ClassicalSharp/Math/Physics/Searcher.cs index 2c5b72cef..e7e991d0f 100644 --- a/ClassicalSharp/Math/Physics/Searcher.cs +++ b/ClassicalSharp/Math/Physics/Searcher.cs @@ -44,12 +44,12 @@ namespace ClassicalSharp.Physics { int elements = (max.X + 1 - min.X) * (max.Y + 1 - min.Y) * (max.Z + 1 - min.Z); if (elements > stateCache.Length) { stateCache = new State[elements]; - } - + } AABB blockBB = default(AABB); BlockInfo info = game.BlockInfo; int count = 0; + // Order loops so that we minimise cache misses for (int y = min.Y; y <= max.Y; y++) for (int z = min.Z; z <= max.Z; z++)