1
0

Add new flow direction calculating algorithm (#4160)

This commit is contained in:
bionext03 2018-07-27 17:01:53 +08:00 committed by peterbell10
parent cdd8e42587
commit 90d552a702
3 changed files with 80 additions and 133 deletions

View File

@ -1047,51 +1047,23 @@ void cEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
} }
// Get water direction // Get water direction
Direction WaterDir = m_World->GetWaterSimulator()->GetFlowingDirection(BlockX, BlockY, BlockZ); Vector3f WaterDir = m_World->GetWaterSimulator()->GetFlowingDirection(BlockX, BlockY, BlockZ);
m_WaterSpeed *= 0.9; // Reduce speed each tick m_WaterSpeed *= 0.9; // Reduce speed each tick
switch (WaterDir) auto AdjustSpeed = [](double & a_WaterSpeed, float a_WaterDir)
{
case X_PLUS:
{ {
m_WaterSpeed.x = 0.2f; if (std::abs(a_WaterDir) > (0.05f / 0.4f))
m_bOnGround = false; {
break; a_WaterSpeed = 0.4 * a_WaterDir;
} }
case X_MINUS: else if (std::abs(a_WaterSpeed) < 0.05)
{ {
m_WaterSpeed.x = -0.2f; a_WaterSpeed = 0.0;
m_bOnGround = false; }
break; };
} AdjustSpeed(m_WaterSpeed.x, WaterDir.x);
case Z_PLUS: AdjustSpeed(m_WaterSpeed.z, WaterDir.z);
{
m_WaterSpeed.z = 0.2f;
m_bOnGround = false;
break;
}
case Z_MINUS:
{
m_WaterSpeed.z = -0.2f;
m_bOnGround = false;
break;
}
default:
{
break;
}
}
if (fabs(m_WaterSpeed.x) < 0.05)
{
m_WaterSpeed.x = 0;
}
if (fabs(m_WaterSpeed.z) < 0.05)
{
m_WaterSpeed.z = 0;
}
NextSpeed += m_WaterSpeed; NextSpeed += m_WaterSpeed;
@ -1104,35 +1076,49 @@ void cEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
auto isHit = cLineBlockTracer::FirstSolidHitTrace(*GetWorld(), NextPos, wantNextPos, HitCoords, HitBlockCoords, HitBlockFace); auto isHit = cLineBlockTracer::FirstSolidHitTrace(*GetWorld(), NextPos, wantNextPos, HitCoords, HitBlockCoords, HitBlockFace);
if (isHit) if (isHit)
{ {
// Set our position to where the block was hit, minus a bit: // Set our position to where the block was hit:
// TODO: The real entity's m_Width should be taken into account here NextPos = HitCoords;
NextPos = HitCoords - NextSpeed.NormalizeCopy() * 0.1;
if (HitBlockFace == BLOCK_FACE_YP)
{
// We hit the ground, adjust the position to the top of the block:
m_bOnGround = true;
NextPos.y = HitBlockCoords.y + 1;
}
// Avoid movement in the direction of the blockface that has been hit: // Avoid movement in the direction of the blockface that has been hit and correct for collision box:
double HalfWidth = GetWidth() / 2.0;
switch (HitBlockFace) switch (HitBlockFace)
{ {
case BLOCK_FACE_XM: case BLOCK_FACE_XM:
{
NextSpeed.x = 0;
NextPos.x -= HalfWidth;
break;
}
case BLOCK_FACE_XP: case BLOCK_FACE_XP:
{ {
NextSpeed.x = 0; NextSpeed.x = 0;
NextPos.x += HalfWidth;
break; break;
} }
case BLOCK_FACE_YM: case BLOCK_FACE_YM:
{
NextSpeed.y = 0;
NextPos.y -= GetHeight();
break;
}
case BLOCK_FACE_YP: case BLOCK_FACE_YP:
{ {
NextSpeed.y = 0; NextSpeed.y = 0;
// We hit the ground, adjust the position to the top of the block:
m_bOnGround = true;
NextPos.y = HitBlockCoords.y + 1;
break; break;
} }
case BLOCK_FACE_ZM: case BLOCK_FACE_ZM:
{
NextSpeed.z = 0;
NextPos.z -= HalfWidth;
break;
}
case BLOCK_FACE_ZP: case BLOCK_FACE_ZP:
{ {
NextSpeed.z = 0; NextSpeed.z = 0;
NextPos.z += HalfWidth;
break; break;
} }
default: default:

View File

@ -128,100 +128,61 @@ bool cFluidSimulator::IsHigherMeta(NIBBLETYPE a_Meta1, NIBBLETYPE a_Meta2)
// TODO Not working very well yet :s Vector3f cFluidSimulator::GetFlowingDirection(int a_X, int a_Y, int a_Z)
Direction cFluidSimulator::GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over)
{ {
if (!cChunkDef::IsValidHeight(a_Y)) if (!cChunkDef::IsValidHeight(a_Y))
{ {
return NONE; return {};
}
BLOCKTYPE BlockID = m_World.GetBlock(a_X, a_Y, a_Z);
if (!IsAllowedBlock(BlockID)) // No Fluid -> No Flowing direction :D
{
return NONE;
} }
/* if (!IsAllowedBlock(m_World.GetBlock(a_X, a_Y, a_Z))) // No Fluid -> No Flowing direction :D
Disabled because of causing problems and being useless atm
char BlockBelow = m_World.GetBlock(a_X, a_Y - 1, a_Z); // If there is nothing or fluid below it -> dominating flow is down :D
if ((BlockBelow == E_BLOCK_AIR) || IsAllowedBlock(BlockBelow))
{ {
return Y_MINUS; return {};
}
*/
NIBBLETYPE LowestPoint = m_World.GetBlockMeta(a_X, a_Y, a_Z); // Current Block Meta so only lower points will be counted
int X = 0, Z = 0; // Lowest Pos will be stored here
if (IsAllowedBlock(m_World.GetBlock(a_X, a_Y + 1, a_Z)) && a_Over) // check for upper block to flow because this also affects the flowing direction
{
return GetFlowingDirection(a_X, a_Y + 1, a_Z, false);
} }
std::vector< Vector3i * > Points; const auto HeightFromMeta = [](NIBBLETYPE a_BlockMeta) -> NIBBLETYPE
Points.reserve(4); // Already allocate 4 places :D
// add blocks around the checking pos
Points.push_back(new Vector3i(a_X - 1, a_Y, a_Z));
Points.push_back(new Vector3i(a_X + 1, a_Y, a_Z));
Points.push_back(new Vector3i(a_X, a_Y, a_Z + 1));
Points.push_back(new Vector3i(a_X, a_Y, a_Z - 1));
for (auto itr = Points.cbegin(), end = Points.cend(); itr != end; ++itr)
{
Vector3i * Pos = (*itr);
auto PosBlockID = m_World.GetBlock(Pos->x, Pos->y, Pos->z);
if (IsAllowedBlock(PosBlockID))
{ {
NIBBLETYPE Meta = m_World.GetBlockMeta(Pos->x, Pos->y, Pos->z); // Falling water blocks are always full height (0)
return ((a_BlockMeta & 0x08) != 0) ? 0 : a_BlockMeta;
};
if (Meta > LowestPoint) auto BlockMeta = m_World.GetBlockMeta(a_X, a_Y, a_Z);
{ NIBBLETYPE CentralPoint = HeightFromMeta(BlockMeta);
LowestPoint = Meta; NIBBLETYPE LevelPoint[4];
X = Pos->x;
Z = Pos->z; // blocks around the checking pos
} Vector3i Points[]
} {
else if (PosBlockID == E_BLOCK_AIR) { a_X + 1, a_Y, a_Z },
{ a_X, a_Y, a_Z + 1 },
{ a_X - 1, a_Y, a_Z },
{ a_X, a_Y, a_Z - 1 }
};
for (size_t i = 0; i < ARRAYCOUNT(LevelPoint); i++)
{
if (IsAllowedBlock(m_World.GetBlock(Points[i])))
{ {
LowestPoint = 9; // This always dominates LevelPoint[i] = HeightFromMeta(m_World.GetBlockMeta(Points[i]));
X = Pos->x; }
Z = Pos->z; else
{
LevelPoint[i] = CentralPoint;
} }
delete Pos;
Pos = nullptr;
} }
if (LowestPoint == m_World.GetBlockMeta(a_X, a_Y, a_Z)) Vector3f Direction;
// Calculate the flow direction
Direction.x = (LevelPoint[0] - LevelPoint[2]) / 2.0f;
Direction.z = (LevelPoint[1] - LevelPoint[3]) / 2.0f;
if ((BlockMeta & 0x08) != 0) // Test falling bit
{ {
return NONE; Direction.y = -1.0f;
} }
if (a_X - X > 0) return Direction;
{
return X_MINUS;
}
if (a_X - X < 0)
{
return X_PLUS;
}
if (a_Z - Z > 0)
{
return Z_MINUS;
}
if (a_Z - Z < 0)
{
return Z_PLUS;
}
return NONE;
} }

View File

@ -28,7 +28,7 @@ class cFluidSimulatorData
{ {
public: public:
virtual ~cFluidSimulatorData() {} virtual ~cFluidSimulatorData() {}
} ; };
@ -45,8 +45,8 @@ public:
// cSimulator overrides: // cSimulator overrides:
virtual bool IsAllowedBlock(BLOCKTYPE a_BlockType) override; virtual bool IsAllowedBlock(BLOCKTYPE a_BlockType) override;
/** Gets the flowing direction. If a_Over is true also the block over the current block affects the direction (standard) */ /** Returns a unit vector in the direction the fluid is flowing or a zero-vector if not flowing. */
virtual Direction GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over = true); virtual Vector3f GetFlowingDirection(int a_X, int a_Y, int a_Z);
/** Creates a ChunkData object for the simulator to use. The simulator returns the correct object type. */ /** Creates a ChunkData object for the simulator to use. The simulator returns the correct object type. */
virtual cFluidSimulatorData * CreateChunkData(void) { return nullptr; } virtual cFluidSimulatorData * CreateChunkData(void) { return nullptr; }
@ -66,7 +66,7 @@ public:
protected: protected:
BLOCKTYPE m_FluidBlock; // The fluid block type that needs simulating BLOCKTYPE m_FluidBlock; // The fluid block type that needs simulating
BLOCKTYPE m_StationaryFluidBlock; // The fluid block type that indicates no simulation is needed BLOCKTYPE m_StationaryFluidBlock; // The fluid block type that indicates no simulation is needed
} ; };