1
0
Fork 0
mirror of https://github.com/k4zmu2a/SpaceCadetPinball.git synced 2024-11-17 23:20:19 +01:00
SpaceCadetPinball/SpaceCadetPinball/TEdgeManager.cpp

276 lines
6 KiB
C++
Raw Normal View History

2020-12-20 12:13:12 +01:00
#include "pch.h"
#include "TEdgeManager.h"
#include "maths.h"
#include "TBall.h"
2021-01-07 17:00:38 +01:00
#include "TEdgeBox.h"
#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-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-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)
{
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)
{
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)
{
return std::min(x + 1, MaxBoxX - 1);
2021-01-07 17:00:38 +01:00
}
int TEdgeManager::increment_box_y(int y)
{
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)
{
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)
{
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];
for (auto it = edgeBox->EdgeList.rbegin(); it != edgeBox->EdgeList.rend(); ++it)
2021-01-07 17:00:38 +01: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;
}
void TEdgeManager::FieldEffects(TBall* ball, vector2* dstVec)
2021-01-07 17:00:38 +01: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];
for (auto it = edgeBox->FieldList.rbegin(); it != edgeBox->FieldList.rend(); ++it)
2021-01-07 17:00:38 +01: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);
2021-01-07 17:00:38 +01:00
}
}
}
2020-12-20 12:13:12 +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);
}
}
}
else if (rayBoxX == rayEndBoxX)
2021-01-07 17:00:38 +01:00
{
if (rayDirY == 1)
2021-01-07 17:00:38 +01:00
{
for (auto indexY = rayBoxY; indexY <= rayEndBoxY; indexY++)
2021-01-07 17:00:38 +01:00
{
edgeIndex = TestGridBox(rayBoxX, indexY, &distance, edge, ray, ball, edgeIndex);
2021-01-07 17:00:38 +01:00
}
}
else
{
for (auto indexY = rayBoxY; indexY >= rayEndBoxY; indexY--)
2021-01-07 17:00:38 +01:00
{
edgeIndex = TestGridBox(rayBoxX, indexY, &distance, edge, ray, ball, edgeIndex);
2021-01-07 17:00:38 +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
{
if (rayDirY == 1)
2021-01-07 17:00:38 +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
{
if (xCoord == yCoord)
2021-01-07 17:00:38 +01:00
{
++indexX;
++bresIndexX;
}
++indexY;
++bresIndexY;
}
else
{
++indexX;
++bresIndexX;
2021-01-07 17:00:38 +01:00
}
edgeIndex = TestGridBox(indexX, indexY, &distance, edge, ray, ball, edgeIndex);
2021-01-07 17:00:38 +01:00
}
while (indexX < rayEndBoxX || indexY < rayEndBoxY);
}
else
{
do
2021-01-07 17:00:38 +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
{
if (xCoord == yCoord)
2021-01-07 17:00:38 +01:00
{
++indexX;
++bresIndexX;
}
--indexY;
}
else
{
++indexX;
++bresIndexX;
2021-01-07 17:00:38 +01:00
}
edgeIndex = TestGridBox(indexX, indexY, &distance, edge, ray, ball, edgeIndex);
2021-01-07 17:00:38 +01:00
}
while (indexX < rayEndBoxX || indexY > rayEndBoxY);
2021-01-07 17:00:38 +01:00
}
}
else
{
if (rayDirY == 1)
2021-01-07 17:00:38 +01:00
{
do
2021-01-07 17:00:38 +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
{
if (xCoord == yCoord)
2021-01-07 17:00:38 +01:00
--indexX;
++indexY;
++bresIndexY;
}
else
{
--indexX;
2021-01-07 17:00:38 +01:00
}
edgeIndex = TestGridBox(indexX, indexY, &distance, edge, ray, ball, edgeIndex);
2021-01-07 17:00:38 +01:00
}
while (indexX > rayEndBoxX || indexY < rayEndBoxY);
}
else
{
do
2021-01-07 17:00:38 +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
{
if (xCoord == yCoord)
2021-01-07 17:00:38 +01:00
--indexX;
--indexY;
}
else
{
--indexX;
2021-01-07 17:00:38 +01:00
}
edgeIndex = TestGridBox(indexX, indexY, &distance, edge, ray, ball, edgeIndex);
2021-01-07 17:00:38 +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;
}