Optimise collision detection

This commit is contained in:
UnknownShadow200 2017-02-03 17:30:21 +11:00
parent 479d8ec5cf
commit 61ef392639
4 changed files with 14 additions and 11 deletions

View File

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

View File

@ -51,9 +51,7 @@ namespace ClassicalSharp.Entities {
}
/// <summary> Returns the size of the model that is used for collision detection. </summary>
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) {

View File

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

View File

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