2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Tracer.h"
|
|
|
|
#include "World.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "Entities/Entity.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2014-07-17 13:13:23 -04:00
|
|
|
#include <stdlib.h>
|
2012-06-14 09:06:06 -04:00
|
|
|
#endif
|
|
|
|
|
2013-04-06 12:12:52 -04:00
|
|
|
|
|
|
|
|
2015-04-27 20:51:21 -04:00
|
|
|
const float FLOAT_EPSILON = 0.0001f; // TODO: Stash this in some header where it can be reused
|
|
|
|
|
2013-04-06 12:12:52 -04:00
|
|
|
|
2015-04-28 18:14:42 -04:00
|
|
|
const std::array<const Vector3f, 6>& cTracer::m_NormalTable(void)
|
|
|
|
{
|
|
|
|
static std::array<const Vector3f, 6>* table =
|
|
|
|
new std::array<const Vector3f, 6>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Vector3f(-1, 0, 0), // 1: -x
|
|
|
|
Vector3f( 0, 0, -1), // 2: -z
|
|
|
|
Vector3f( 1, 0, 0), // 3: +x
|
|
|
|
Vector3f( 0, 0, 1), // 4: +z
|
|
|
|
Vector3f( 0, 1, 0), // 5: +y
|
|
|
|
Vector3f( 0, -1, 0) // 6: -y
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return *table;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-04-06 12:12:52 -04:00
|
|
|
|
2014-12-05 06:56:53 -05:00
|
|
|
cTracer::cTracer(cWorld * a_World):
|
|
|
|
m_World(a_World)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-06 12:12:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
cTracer::~cTracer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-06 12:12:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-27 20:51:21 -04:00
|
|
|
int cTracer::SigNum(float a_Num)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-12-05 10:59:11 -05:00
|
|
|
if (a_Num < 0.f)
|
|
|
|
{
|
2015-05-08 18:32:02 -04:00
|
|
|
return -1;
|
2014-12-05 10:59:11 -05:00
|
|
|
}
|
|
|
|
if (a_Num > 0.f)
|
|
|
|
{
|
2015-05-08 18:32:02 -04:00
|
|
|
return 1;
|
2014-12-05 10:59:11 -05:00
|
|
|
}
|
2015-05-08 18:32:02 -04:00
|
|
|
return 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-04-06 12:12:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cTracer::SetValues(const Vector3f & a_Start, const Vector3f & a_Direction)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-04-27 20:47:36 -04:00
|
|
|
// Since this method should only be called by Trace, zero length vectors should already have been taken care of
|
|
|
|
ASSERT(a_Direction.HasNonZeroLength());
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// calculate the direction of the ray (linear algebra)
|
2018-05-06 13:07:34 -04:00
|
|
|
m_Dir = a_Direction;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// decide which direction to start walking in
|
2018-05-06 13:07:34 -04:00
|
|
|
m_Step.x = SigNum(m_Dir.x);
|
|
|
|
m_Step.y = SigNum(m_Dir.y);
|
|
|
|
m_Step.z = SigNum(m_Dir.z);
|
2015-04-27 20:51:21 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// normalize the direction vector
|
2018-05-06 13:07:34 -04:00
|
|
|
m_Dir.Normalize();
|
2015-04-27 20:47:36 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// how far we must move in the ray direction before
|
|
|
|
// we encounter a new voxel in x-direction
|
|
|
|
// same but y-direction
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Dir.x != 0.f)
|
2013-04-06 12:12:52 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tDelta.x = 1 / std::abs(m_Dir.x);
|
2013-04-06 12:12:52 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tDelta.x = 0;
|
2013-04-06 12:12:52 -04:00
|
|
|
}
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Dir.y != 0.f)
|
2013-04-06 12:12:52 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tDelta.y = 1 / std::abs(m_Dir.y);
|
2013-04-06 12:12:52 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tDelta.y = 0;
|
2013-04-06 12:12:52 -04:00
|
|
|
}
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Dir.z != 0.f)
|
2013-04-06 12:12:52 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tDelta.z = 1 / std::abs(m_Dir.z);
|
2013-04-06 12:12:52 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tDelta.z = 0;
|
2013-04-06 12:12:52 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-04-27 20:51:21 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// start voxel coordinates
|
2018-05-06 13:07:34 -04:00
|
|
|
m_Pos.x = static_cast<int>(floorf(a_Start.x));
|
|
|
|
m_Pos.y = static_cast<int>(floorf(a_Start.y));
|
|
|
|
m_Pos.z = static_cast<int>(floorf(a_Start.z));
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// calculate distance to first intersection in the voxel we start from
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Dir.x < 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tMax.x = (static_cast<float>(m_Pos.x) - a_Start.x) / m_Dir.x;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tMax.x = (static_cast<float>(m_Pos.x + 1) - a_Start.x) / m_Dir.x; // TODO: Possible division by zero
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Dir.y < 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tMax.y = (static_cast<float>(m_Pos.y) - a_Start.y) / m_Dir.y;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tMax.y = (static_cast<float>(m_Pos.y + 1) - a_Start.y) / m_Dir.y; // TODO: Possible division by zero
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Dir.z < 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tMax.z = (static_cast<float>(m_Pos.z) - a_Start.z) / m_Dir.z;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tMax.z = (static_cast<float>(m_Pos.z + 1) - a_Start.z) / m_Dir.z; // TODO: Possible division by zero
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-30 06:54:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-05 06:56:53 -05:00
|
|
|
bool cTracer::Trace(const Vector3f & a_Start, const Vector3f & a_Direction, int a_Distance, bool a_LineOfSight)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-04-27 20:47:36 -04:00
|
|
|
if (!a_Direction.HasNonZeroLength())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-24 15:42:11 -04:00
|
|
|
if ((a_Start.y < 0) || (a_Start.y >= cChunkDef::Height))
|
2013-03-30 06:54:05 -04:00
|
|
|
{
|
2013-08-24 16:48:19 -04:00
|
|
|
LOGD("%s: Start Y is outside the world (%.2f), not tracing.", __FUNCTION__, a_Start.y);
|
2013-11-05 16:01:51 -05:00
|
|
|
return false;
|
2013-03-30 06:54:05 -04:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-03-30 06:54:05 -04:00
|
|
|
SetValues(a_Start, a_Direction);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2018-05-06 13:07:34 -04:00
|
|
|
Vector3f End = a_Start + (m_Dir * static_cast<float>(a_Distance));
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-03-30 06:54:05 -04:00
|
|
|
if (End.y < 0)
|
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
float dist = -a_Start.y / m_Dir.y; // No division by 0 possible
|
|
|
|
End = a_Start + (m_Dir * dist);
|
2013-03-30 06:54:05 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// end voxel coordinates
|
2018-05-06 13:07:34 -04:00
|
|
|
m_End1.x = static_cast<int>(floorf(End.x));
|
|
|
|
m_End1.y = static_cast<int>(floorf(End.y));
|
|
|
|
m_End1.z = static_cast<int>(floorf(End.z));
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// check if first is occupied
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Pos.Equals(m_End1))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-11-05 16:01:51 -05:00
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool reachedX = false, reachedY = false, reachedZ = false;
|
|
|
|
|
|
|
|
int Iterations = 0;
|
2013-04-06 12:12:52 -04:00
|
|
|
while (Iterations < a_Distance)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Iterations++;
|
2018-05-06 13:07:34 -04:00
|
|
|
if ((m_tMax.x < m_tMax.y) && (m_tMax.x < m_tMax.z))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tMax.x += m_tDelta.x;
|
|
|
|
m_Pos.x += m_Step.x;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2018-05-06 13:07:34 -04:00
|
|
|
else if (m_tMax.y < m_tMax.z)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tMax.y += m_tDelta.y;
|
|
|
|
m_Pos.y += m_Step.y;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
m_tMax.z += m_tDelta.z;
|
|
|
|
m_Pos.z += m_Step.z;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Step.x > 0.0f)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Pos.x >= m_End1.x)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
reachedX = true;
|
|
|
|
}
|
|
|
|
}
|
2018-05-06 13:07:34 -04:00
|
|
|
else if (m_Pos.x <= m_End1.x)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
reachedX = true;
|
|
|
|
}
|
|
|
|
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Step.y > 0.0f)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Pos.y >= m_End1.y)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
reachedY = true;
|
|
|
|
}
|
|
|
|
}
|
2018-05-06 13:07:34 -04:00
|
|
|
else if (m_Pos.y <= m_End1.y)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
reachedY = true;
|
|
|
|
}
|
|
|
|
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Step.z > 0.0f)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
if (m_Pos.z >= m_End1.z)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
reachedZ = true;
|
|
|
|
}
|
|
|
|
}
|
2018-05-06 13:07:34 -04:00
|
|
|
else if (m_Pos.z <= m_End1.z)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
reachedZ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reachedX && reachedY && reachedZ)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2018-05-06 13:07:34 -04:00
|
|
|
if ((m_Pos.y < 0) || (m_Pos.y >= cChunkDef::Height))
|
2014-04-23 16:06:46 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-06 13:07:34 -04:00
|
|
|
BLOCKTYPE BlockID = m_World->GetBlock(m_Pos);
|
2013-11-05 16:11:13 -05:00
|
|
|
// Block is counted as a collision if we are not doing a line of sight and it is solid,
|
|
|
|
// or if the block is not air and not water. That way mobs can still see underwater.
|
2014-03-01 14:34:19 -05:00
|
|
|
if ((!a_LineOfSight && cBlockInfo::IsSolid(BlockID)) || (a_LineOfSight && (BlockID != E_BLOCK_AIR) && !IsBlockWater(BlockID)))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
BlockHitPosition = m_Pos;
|
|
|
|
int Normal = GetHitNormal(a_Start, End, m_Pos);
|
2014-04-23 16:06:46 -04:00
|
|
|
if (Normal > 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
HitNormal = m_NormalTable()[static_cast<size_t>(Normal - 1)];
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-11-04 22:10:29 -05:00
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
2013-11-05 16:01:51 -05:00
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-03-30 06:54:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// return 1 = hit, other is not hit
|
2015-05-19 14:32:10 -04:00
|
|
|
static int LinesCross(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-07-17 13:13:23 -04:00
|
|
|
// float linx, liny;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-12-05 06:56:53 -05:00
|
|
|
float d = (x1 - x0) * (y3 - y2) - (y1 - y0) * (x3 - x2);
|
|
|
|
if (std::abs(d) < 0.001)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-12-05 06:56:53 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AB = ((y0 - y2) * (x3 - x2) - (x0 - x2) * (y3 - y2)) / d;
|
|
|
|
if ((AB >= 0.0) && (AB <= 1.0))
|
|
|
|
{
|
|
|
|
float CD = ((y0 - y2) * (x1 - x0) - (x0 - x2) * (y1 - y0)) / d;
|
|
|
|
if ((CD >= 0.0) && (CD <= 1.0))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-12-05 06:56:53 -05:00
|
|
|
// linx = x0 + AB * (x1 - x0);
|
|
|
|
// liny = y0 + AB * (y1 - y0);
|
2012-06-14 09:06:06 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-05 06:56:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-26 17:24:36 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// intersect3D_SegmentPlane(): intersect a segment and a plane
|
|
|
|
// Input: a_Ray = a segment, and a_Plane = a plane = {Point V0; Vector n;}
|
|
|
|
// Output: *I0 = the intersect point (when it exists)
|
|
|
|
// Return: 0 = disjoint (no intersection)
|
|
|
|
// 1 = intersection in the unique point *I0
|
|
|
|
// 2 = the segment lies in the plane
|
2014-12-05 06:56:53 -05:00
|
|
|
int cTracer::intersect3D_SegmentPlane(const Vector3f & a_Origin, const Vector3f & a_End, const Vector3f & a_PlanePos, const Vector3f & a_PlaneNormal)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-07-17 16:15:34 -04:00
|
|
|
Vector3f u = a_End - a_Origin; // a_Ray.P1 - S.P0;
|
|
|
|
Vector3f w = a_Origin - a_PlanePos; // S.P0 - Pn.V0;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-12-05 06:56:53 -05:00
|
|
|
float D = a_PlaneNormal.Dot(u); // dot(Pn.n, u);
|
|
|
|
float N = -(a_PlaneNormal.Dot(w)); // -dot(a_Plane.n, w);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-04-27 20:51:21 -04:00
|
|
|
if (std::abs(D) < FLOAT_EPSILON)
|
2014-07-17 16:15:34 -04:00
|
|
|
{
|
|
|
|
// segment is parallel to plane
|
2014-09-22 18:23:56 -04:00
|
|
|
if (N == 0.0)
|
2014-07-17 16:15:34 -04:00
|
|
|
{
|
|
|
|
// segment lies in plane
|
2012-06-14 09:06:06 -04:00
|
|
|
return 2;
|
2014-07-17 16:15:34 -04:00
|
|
|
}
|
|
|
|
return 0; // no intersection
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// they are not parallel
|
|
|
|
// compute intersect param
|
|
|
|
float sI = N / D;
|
2014-12-05 06:56:53 -05:00
|
|
|
if ((sI < 0) || (sI > 1))
|
2014-07-17 16:15:34 -04:00
|
|
|
{
|
|
|
|
return 0; // no intersection
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-12-05 06:56:53 -05:00
|
|
|
// Vector3f I (a_Ray->GetOrigin() + sI * u);// S.P0 + sI * u; // compute segment intersect point
|
2012-06-14 09:06:06 -04:00
|
|
|
RealHit = a_Origin + u * sI;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-05-06 13:07:34 -04:00
|
|
|
int cTracer::GetHitNormal(const Vector3f & a_Start, const Vector3f & a_End, const Vector3i & a_BlockPos)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Vector3i SmallBlockPos = a_BlockPos;
|
2015-07-29 11:04:03 -04:00
|
|
|
BLOCKTYPE BlockID = static_cast<BLOCKTYPE>(m_World->GetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z));
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-12-05 06:56:53 -05:00
|
|
|
if ((BlockID == E_BLOCK_AIR) || IsBlockWater(BlockID))
|
2014-07-17 16:15:34 -04:00
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
return 0;
|
2014-07-17 16:15:34 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
Vector3f BlockPos;
|
|
|
|
BlockPos = Vector3f(SmallBlockPos);
|
|
|
|
|
2018-05-06 13:07:34 -04:00
|
|
|
Vector3f Look = (a_End - a_Start);
|
2012-06-14 09:06:06 -04:00
|
|
|
Look.Normalize();
|
|
|
|
|
2014-12-05 06:56:53 -05:00
|
|
|
float dot = Look.Dot(Vector3f(-1, 0, 0)); // first face normal is x -1
|
2014-07-17 13:13:23 -04:00
|
|
|
if (dot < 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
int Lines = LinesCross(a_Start.x, a_Start.y, a_End.x, a_End.y, BlockPos.x, BlockPos.y, BlockPos.x, BlockPos.y + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
Lines = LinesCross(a_Start.x, a_Start.z, a_End.x, a_End.z, BlockPos.x, BlockPos.z, BlockPos.x, BlockPos.z + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
intersect3D_SegmentPlane(a_Start, a_End, BlockPos, Vector3f(-1, 0, 0));
|
2012-06-14 09:06:06 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 06:56:53 -05:00
|
|
|
dot = Look.Dot(Vector3f(0, 0, -1)); // second face normal is z -1
|
2014-07-20 17:10:31 -04:00
|
|
|
if (dot < 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
int Lines = LinesCross(a_Start.z, a_Start.y, a_End.z, a_End.y, BlockPos.z, BlockPos.y, BlockPos.z, BlockPos.y + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
Lines = LinesCross(a_Start.z, a_Start.x, a_End.z, a_End.x, BlockPos.z, BlockPos.x, BlockPos.z, BlockPos.x + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
intersect3D_SegmentPlane(a_Start, a_End, BlockPos, Vector3f(0, 0, -1));
|
2012-06-14 09:06:06 -04:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 06:56:53 -05:00
|
|
|
dot = Look.Dot(Vector3f(1, 0, 0)); // third face normal is x 1
|
2014-07-20 17:10:31 -04:00
|
|
|
if (dot < 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
int Lines = LinesCross(a_Start.x, a_Start.y, a_End.x, a_End.y, BlockPos.x + 1, BlockPos.y, BlockPos.x + 1, BlockPos.y + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
Lines = LinesCross(a_Start.x, a_Start.z, a_End.x, a_End.z, BlockPos.x + 1, BlockPos.z, BlockPos.x + 1, BlockPos.z + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
intersect3D_SegmentPlane(a_Start, a_End, BlockPos + Vector3f(1, 0, 0), Vector3f(1, 0, 0));
|
2012-06-14 09:06:06 -04:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 06:56:53 -05:00
|
|
|
dot = Look.Dot(Vector3f(0, 0, 1)); // fourth face normal is z 1
|
2014-07-20 17:10:31 -04:00
|
|
|
if (dot < 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
int Lines = LinesCross(a_Start.z, a_Start.y, a_End.z, a_End.y, BlockPos.z + 1, BlockPos.y, BlockPos.z + 1, BlockPos.y + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
Lines = LinesCross(a_Start.z, a_Start.x, a_End.z, a_End.x, BlockPos.z + 1, BlockPos.x, BlockPos.z + 1, BlockPos.x + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
intersect3D_SegmentPlane(a_Start, a_End, BlockPos + Vector3f(0, 0, 1), Vector3f(0, 0, 1));
|
2012-06-14 09:06:06 -04:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 06:56:53 -05:00
|
|
|
dot = Look.Dot(Vector3f(0, 1, 0)); // fifth face normal is y 1
|
2014-07-20 17:10:31 -04:00
|
|
|
if (dot < 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
int Lines = LinesCross(a_Start.y, a_Start.x, a_End.y, a_End.x, BlockPos.y + 1, BlockPos.x, BlockPos.y + 1, BlockPos.x + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
Lines = LinesCross(a_Start.y, a_Start.z, a_End.y, a_End.z, BlockPos.y + 1, BlockPos.z, BlockPos.y + 1, BlockPos.z + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
intersect3D_SegmentPlane(a_Start, a_End, BlockPos + Vector3f(0, 1, 0), Vector3f(0, 1, 0));
|
2012-06-14 09:06:06 -04:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 06:56:53 -05:00
|
|
|
dot = Look.Dot(Vector3f(0, -1, 0)); // sixth face normal is y -1
|
2014-07-20 17:10:31 -04:00
|
|
|
if (dot < 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
int Lines = LinesCross(a_Start.y, a_Start.x, a_End.y, a_End.x, BlockPos.y, BlockPos.x, BlockPos.y, BlockPos.x + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
Lines = LinesCross(a_Start.y, a_Start.z, a_End.y, a_End.z, BlockPos.y, BlockPos.z, BlockPos.y, BlockPos.z + 1);
|
2014-07-20 17:10:31 -04:00
|
|
|
if (Lines == 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2018-05-06 13:07:34 -04:00
|
|
|
intersect3D_SegmentPlane(a_Start, a_End, BlockPos, Vector3f(0, -1, 0));
|
2012-06-14 09:06:06 -04:00
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|