Joint solver basic implementation (axis aligned)

Does not handle rotation at all. solveWorkspaceSnaps is called in the debug game creation (Application.cpp)
Added isA to instance class
This commit is contained in:
NT_x86
2023-03-28 21:05:08 +03:00
parent 85e84bfdf3
commit abb1f76c71
5 changed files with 68 additions and 1 deletions

View File

@@ -16,6 +16,7 @@ public:
virtual void update();
std::vector<Instance*> children; // All children.
std::string getClassName();
bool Instance::isA(std::string name);
Instance* findFirstChild(std::string);
std::vector<Instance* > getChildren();
std::vector<Instance* > getAllChildren();

View File

@@ -10,4 +10,6 @@ public:
~JointsService(void);
void createSnap(PartInstance* Part1, PartInstance* Part2);
void destroyPartSnap(PartInstance* Part);
void JointsService::solvePartSnap(PartInstance* Part, PartInstance* Part2);
void JointsService::solveWorkspaceSnaps();
};

View File

@@ -210,7 +210,7 @@ void Application::onInit() {
test->setParent(_dataModel->getWorkspace());
test->color = Color3(0.2F,0.3F,1);
test->setSize(Vector3(24,1,24));
test->setPosition(Vector3(0,0,0));
test->setPosition(Vector3(0,-10,0));
test->setCFrame(test->getCFrame() * Matrix3::fromEulerAnglesXYZ(0,toRadians(0),toRadians(0)));
test->setSurface(TOP, Enum::SurfaceType::Bumps);
test->setAnchored(true);
@@ -266,6 +266,7 @@ void Application::onInit() {
test->setPosition(Vector3(1,4,0));
test->setSurface(TOP, Enum::SurfaceType::Bumps);
JointsSvc->createSnap(test, test3);
//JointsSvc->solvePartSnap(test3, test);
test = makePart();
test->setParent(_dataModel->getWorkspace());
@@ -294,6 +295,8 @@ void Application::onInit() {
test->setSize(Vector3(4,1,2));
test->setPosition(Vector3(2,7,0));
test->setSurface(TOP, Enum::SurfaceType::Bumps);
JointsSvc->solveWorkspaceSnaps();
#else
_dataModel->debugGetOpen();
#endif

View File

@@ -100,6 +100,13 @@ std::string Instance::getClassName()
return className;
}
bool Instance::isA(std::string name)
{
if (className == name)
return true;
return false;
}
std::vector<Instance* > Instance::getChildren()
{
return children;

View File

@@ -31,4 +31,58 @@ void JointsService::destroyPartSnap(PartInstance* Part)
Snap->remove();
}
}
}
void JointsService::solvePartSnap(PartInstance* Part, PartInstance* Part2)
{
float MoE = 0.05f;
float TopSurface = (Part->getPosition().y + Part->getSize().y / 2);
float RightRECT = (Part->getPosition().x + Part->getSize().x / 2) - MoE;
float LeftRECT = (Part->getPosition().x - Part->getSize().x / 2) + MoE;
float TopRECT = (Part->getPosition().z - Part->getSize().z / 2) + MoE;
float BottomRECT = (Part->getPosition().z + Part->getSize().z / 2) - MoE;
float BottomSurface2 = (Part2->getPosition().y - Part2->getSize().y / 2);
float RightRECT2 = (Part2->getPosition().x + Part2->getSize().x / 2);
float LeftRECT2 = (Part2->getPosition().x - Part2->getSize().x / 2);
float TopRECT2 = (Part2->getPosition().z - Part2->getSize().z / 2);
float BottomRECT2 = (Part2->getPosition().z + Part2->getSize().z / 2);
std::cout << "TopSurface: " << TopSurface << std::endl;
std::cout << "BottomSurface2: " << BottomSurface2 << std::endl;
if (TopSurface == BottomSurface2)
{
std::cout << "Same Y cordinate!!!" << std::endl;
//https://gamedev.stackexchange.com/questions/586/what-is-the-fastest-way-to-work-out-2d-bounding-box-intersection
bool Intersect = !(LeftRECT2 > RightRECT
|| RightRECT2 < LeftRECT
|| TopRECT2 > BottomRECT
|| BottomRECT2 < TopRECT);
if (Intersect){
std::cout << "Intersects!!!" << std::endl;
createSnap(Part, Part2);
}else{
std::cout << "Does not Intersect!!!" << std::endl;
}
}else{
std::cout << "Y cordinate does not match!!!" << std::endl;
}
}
void JointsService::solveWorkspaceSnaps()
{
std::vector<Instance* > children = g_dataModel->getWorkspace()->getAllChildren();
for(size_t i = 0; i < children.size(); i++)
{
PartInstance* Part1 = (PartInstance*)children.at(i);
if (Part1->isA("Part"))
{
for(size_t c = 0; c < children.size(); c++) //horrible
{
PartInstance* Part2 = (PartInstance*)children.at(c);
if (Part1 != Part2)
solvePartSnap(Part1, Part2);
}
}
}
}