2020-12-20 12:13:12 +01:00
|
|
|
#include "pch.h"
|
|
|
|
#include "TEdgeManager.h"
|
|
|
|
|
2021-01-08 16:50:12 +01:00
|
|
|
|
|
|
|
#include "maths.h"
|
|
|
|
#include "TBall.h"
|
2021-01-07 17:00:38 +01:00
|
|
|
#include "TEdgeBox.h"
|
2021-01-08 16:50:12 +01:00
|
|
|
#include "TEdgeSegment.h"
|
2021-01-07 17:00:38 +01:00
|
|
|
#include "TTableLayer.h"
|
|
|
|
|
|
|
|
TEdgeManager::TEdgeManager(float posX, float posY, float width, float height)
|
2021-01-06 15:06:13 +01:00
|
|
|
{
|
2021-01-07 17:00:38 +01:00
|
|
|
X = posX;
|
|
|
|
Y = posY;
|
|
|
|
MaxBoxX = 10;
|
|
|
|
MaxBoxY = 15;
|
|
|
|
AdvanceX = width / 10.0f;
|
|
|
|
AdvanceY = height / 15.0f;
|
|
|
|
AdvanceXInv = 1.0f / AdvanceX;
|
|
|
|
AdvanceYInv = 1.0f / AdvanceY;
|
|
|
|
BoxArray = new TEdgeBox[150];
|
2021-01-06 15:06:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-07 17:00:38 +01:00
|
|
|
TEdgeManager::~TEdgeManager()
|
2020-12-20 12:13:12 +01:00
|
|
|
{
|
2021-01-07 17:00:38 +01:00
|
|
|
delete[] BoxArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TEdgeManager::box_x(float x)
|
|
|
|
{
|
2021-09-14 14:33:18 +02:00
|
|
|
return std::max(0, std::min(static_cast<int>(floor((x - X) * AdvanceXInv)), MaxBoxX - 1));
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int TEdgeManager::box_y(float y)
|
|
|
|
{
|
2021-09-14 14:33:18 +02:00
|
|
|
return std::max(0, std::min(static_cast<int>(floor((y - Y) * AdvanceYInv)), MaxBoxY - 1));
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int TEdgeManager::increment_box_x(int x)
|
|
|
|
{
|
2021-09-14 14:33:18 +02:00
|
|
|
return std::min(x + 1, MaxBoxX - 1);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int TEdgeManager::increment_box_y(int y)
|
|
|
|
{
|
2021-09-14 14:33:18 +02:00
|
|
|
return std::min(y + 1, MaxBoxY - 1);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TEdgeManager::add_edge_to_box(int x, int y, TEdgeSegment* edge)
|
|
|
|
{
|
2021-10-01 17:55:44 +02:00
|
|
|
BoxArray[x + y * MaxBoxX].EdgeList.push_back(edge);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TEdgeManager::add_field_to_box(int x, int y, field_effect_type* field)
|
|
|
|
{
|
2021-10-01 17:55:44 +02:00
|
|
|
BoxArray[x + y * MaxBoxX].FieldList.push_back(field);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int TEdgeManager::TestGridBox(int x, int y, float* distPtr, TEdgeSegment** edgeDst, ray_type* ray, TBall* ball,
|
|
|
|
int edgeIndex)
|
|
|
|
{
|
|
|
|
if (x >= 0 && x < 10 && y >= 0 && y < 15)
|
|
|
|
{
|
|
|
|
TEdgeBox* edgeBox = &BoxArray[x + y * MaxBoxX];
|
|
|
|
TEdgeSegment** edgePtr = &EdgeArray[edgeIndex];
|
2021-10-01 17:55:44 +02:00
|
|
|
for (auto it = edgeBox->EdgeList.rbegin(); it != edgeBox->EdgeList.rend(); ++it)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-10-01 17:55:44 +02:00
|
|
|
auto edge = *it;
|
2021-01-28 16:01:26 +01:00
|
|
|
if (!edge->ProcessedFlag && *edge->ActiveFlag && (edge->CollisionGroup & ray->FieldFlag))
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
|
|
|
if (!ball->already_hit(edge))
|
|
|
|
{
|
|
|
|
++edgeIndex;
|
|
|
|
*edgePtr = edge;
|
|
|
|
++edgePtr;
|
|
|
|
edge->ProcessedFlag = 1;
|
|
|
|
auto dist = edge->FindCollisionDistance(ray);
|
|
|
|
if (dist < *distPtr)
|
|
|
|
{
|
|
|
|
*distPtr = dist;
|
|
|
|
*edgeDst = edge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return edgeIndex;
|
|
|
|
}
|
|
|
|
|
2022-05-11 15:47:13 +02:00
|
|
|
void TEdgeManager::FieldEffects(TBall* ball, vector2* dstVec)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2022-05-11 15:47:13 +02:00
|
|
|
vector2 vec{};
|
2021-01-07 17:00:38 +01:00
|
|
|
TEdgeBox* edgeBox = &BoxArray[box_x(ball->Position.X) + box_y(ball->Position.Y) *
|
|
|
|
MaxBoxX];
|
|
|
|
|
2021-10-01 17:55:44 +02:00
|
|
|
for (auto it = edgeBox->FieldList.rbegin(); it != edgeBox->FieldList.rend(); ++it)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-10-01 17:55:44 +02:00
|
|
|
auto field = *it;
|
2021-01-07 17:00:38 +01:00
|
|
|
if (*field->Flag2Ptr && ball->FieldFlag & field->Mask)
|
|
|
|
{
|
|
|
|
if (field->CollisionComp->FieldEffect(ball, &vec))
|
|
|
|
{
|
|
|
|
maths::vector_add(dstVec, &vec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 12:13:12 +01:00
|
|
|
}
|
2021-01-06 15:06:13 +01:00
|
|
|
|
|
|
|
float TEdgeManager::FindCollisionDistance(ray_type* ray, TBall* ball, TEdgeSegment** edge)
|
|
|
|
{
|
2021-01-07 17:00:38 +01:00
|
|
|
auto distance = 1000000000.0f;
|
|
|
|
auto edgeIndex = 0;
|
|
|
|
|
|
|
|
auto rayX = ray->Origin.X;
|
|
|
|
auto rayY = ray->Origin.Y;
|
|
|
|
auto rayBoxX = box_x(rayX);
|
|
|
|
auto rayBoxY = box_y(rayY);
|
|
|
|
|
|
|
|
auto rayEndX = ray->Direction.X * ray->MaxDistance + ray->Origin.X;
|
|
|
|
auto rayEndY = ray->Direction.Y * ray->MaxDistance + ray->Origin.Y;
|
|
|
|
auto rayEndBoxX = box_x(rayEndX);
|
|
|
|
auto rayEndBoxY = box_y(rayEndY);
|
|
|
|
|
|
|
|
auto rayDirX = rayX >= rayEndX ? -1 : 1;
|
|
|
|
auto rayDirY = rayY >= rayEndY ? -1 : 1;
|
|
|
|
|
|
|
|
if (rayBoxY == rayEndBoxY)
|
|
|
|
{
|
|
|
|
if (rayDirX == 1)
|
|
|
|
{
|
|
|
|
for (auto indexX = rayBoxX; indexX <= rayEndBoxX; indexX++)
|
|
|
|
{
|
|
|
|
edgeIndex = TestGridBox(indexX, rayBoxY, &distance, edge, ray, ball, edgeIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto indexX = rayBoxX; indexX >= rayEndBoxX; indexX--)
|
|
|
|
{
|
|
|
|
edgeIndex = TestGridBox(indexX, rayBoxY, &distance, edge, ray, ball, edgeIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
else if (rayBoxX == rayEndBoxX)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
if (rayDirY == 1)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
for (auto indexY = rayBoxY; indexY <= rayEndBoxY; indexY++)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
edgeIndex = TestGridBox(rayBoxX, indexY, &distance, edge, ray, ball, edgeIndex);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto indexY = rayBoxY; indexY >= rayEndBoxY; indexY--)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
edgeIndex = TestGridBox(rayBoxX, indexY, &distance, edge, ray, ball, edgeIndex);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto rayDyDX = (rayY - rayEndY) / (rayX - rayEndX);
|
|
|
|
auto indexX = rayBoxX;
|
|
|
|
auto indexY = rayBoxY;
|
|
|
|
auto bresIndexX = rayBoxX + 1;
|
|
|
|
auto bresIndexY = rayBoxY + 1;
|
|
|
|
auto bresXAdd = rayY - rayDyDX * rayX;
|
|
|
|
edgeIndex = TestGridBox(rayBoxX, rayBoxY, &distance, edge, ray, ball, 0);
|
|
|
|
if (rayDirX == 1)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
if (rayDirY == 1)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
auto yCoord = bresIndexY * AdvanceY + Y;
|
|
|
|
auto xCoord = (bresIndexX * AdvanceX + X) * rayDyDX + bresXAdd;
|
|
|
|
if (xCoord >= yCoord)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
if (xCoord == yCoord)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
|
|
|
++indexX;
|
|
|
|
++bresIndexX;
|
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
++indexY;
|
|
|
|
++bresIndexY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++indexX;
|
|
|
|
++bresIndexX;
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
edgeIndex = TestGridBox(indexX, indexY, &distance, edge, ray, ball, edgeIndex);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
while (indexX < rayEndBoxX || indexY < rayEndBoxY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
auto yCoord = indexY * AdvanceY + Y;
|
|
|
|
auto xCoord = (bresIndexX * AdvanceX + X) * rayDyDX + bresXAdd;
|
|
|
|
if (xCoord <= yCoord)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
if (xCoord == yCoord)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
|
|
|
++indexX;
|
|
|
|
++bresIndexX;
|
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
--indexY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++indexX;
|
|
|
|
++bresIndexX;
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
edgeIndex = TestGridBox(indexX, indexY, &distance, edge, ray, ball, edgeIndex);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
while (indexX < rayEndBoxX || indexY > rayEndBoxY);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (rayDirY == 1)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
do
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
auto yCoord = bresIndexY * AdvanceY + Y;
|
|
|
|
auto xCoord = (indexX * AdvanceX + X) * rayDyDX + bresXAdd;
|
|
|
|
if (xCoord >= yCoord)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
if (xCoord == yCoord)
|
2021-01-07 17:00:38 +01:00
|
|
|
--indexX;
|
2021-01-08 16:50:12 +01:00
|
|
|
++indexY;
|
|
|
|
++bresIndexY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--indexX;
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
edgeIndex = TestGridBox(indexX, indexY, &distance, edge, ray, ball, edgeIndex);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
while (indexX > rayEndBoxX || indexY < rayEndBoxY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
auto yCoord = indexY * AdvanceY + Y;
|
|
|
|
auto xCoord = (indexX * AdvanceX + X) * rayDyDX + bresXAdd;
|
|
|
|
if (xCoord <= yCoord)
|
2021-01-07 17:00:38 +01:00
|
|
|
{
|
2021-01-08 16:50:12 +01:00
|
|
|
if (xCoord == yCoord)
|
2021-01-07 17:00:38 +01:00
|
|
|
--indexX;
|
2021-01-08 16:50:12 +01:00
|
|
|
--indexY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--indexX;
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
edgeIndex = TestGridBox(indexX, indexY, &distance, edge, ray, ball, edgeIndex);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
2021-01-08 16:50:12 +01:00
|
|
|
while (indexX > rayEndBoxX || indexY > rayEndBoxY);
|
2021-01-07 17:00:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (auto edgePtr = EdgeArray; edgeIndex > 0; --edgeIndex, ++edgePtr)
|
|
|
|
{
|
|
|
|
(*edgePtr)->ProcessedFlag = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return distance;
|
2021-01-06 15:06:13 +01:00
|
|
|
}
|