bool EntityManager::IsMoveLegal(Entity* input) { Sprite* entity = static_cast<Sprite*>(input); BoundingBox<s16> entitybb; entity->getBoundingBox(entitybb); // Make player walk through everything if( input->getType() != PLAYERENTITY) { for(LinkedList* node = mEntities; node != NULL; node = node->next) { Entity* entity_to_check = (Entity*) node->data; // Ugly casting but it's needed... if(entity_to_check->getType() != LIVINGENTITY || input == entity_to_check || entity_to_check->hidden) continue; Sprite* current = static_cast<Sprite*>(entity_to_check); BoundingBox<s16> bb; current->getBoundingBox(bb); if(entitybb.Intersects(bb)) return false; } } return true; /* Check the map walk/nowalk flag */ //return (gpMapEngine->collisionAbsolute(entity->position.x() + 16, entity->position.y() + 16)); }
bool SolidComposite::BoxIntersects(const BoundingBox& b) const { if (!b.Intersects(m_bsphere)) { return false; } const int size = m_children.size(); for (int i = 0; i < size; i++) { const SolidComponent* pc = m_children[i].GetPtr(); if (pc->BoxIntersects(b)) { return true; } } return false; }
//Update collectables positions each frame void Collectable::update(float dt){ //make bounding boxes BoundingBox *playerbb = new BoundingBox(XMFLOAT3(player->player->getPosition()._41, player->player->getPosition()._42, player->player->getPosition()._43), XMFLOAT3(2.0f, 2.0f, 0.0f)); //moves collectables across screen (right to left) and respawns them when they leave the screen for (unsigned int i = 0; i < 1; i++){ collectables[i]->translate(XMFLOAT3(-8.0f * dt, 0.0f, 0.0f)); //._41 is the x value for the position matrix of game entities if (collectables[i]->getPosition()._41 < -30) collectables[i]->setPosition(XMFLOAT3(30.0f, (rand() % 40) - 19.0f, 0.0f));} // Tests for collisions between the collectables and the player for (int i = 0; i < 1; i++){ BoundingBox *collectablebb = new BoundingBox(XMFLOAT3(collectables[i]->getPosition()._41, collectables[i]->getPosition()._42, collectables[i]->getPosition()._43), XMFLOAT3(2.0f, 2.0f, 0.0f)); //check for intersections if (collectablebb->Intersects(*playerbb)) { collectables[i]->setPosition(XMFLOAT3(30.0f, (rand() % 40) - 19.0f, 0.0f)); gameReference->pickUp(); //elimate spawning on each other for (int g = 0; g < 1; g++) { BoundingBox *collectablebb2 = new BoundingBox(XMFLOAT3(collectables[g]->getPosition()._41, collectables[g]->getPosition()._42, collectables[g]->getPosition()._43), XMFLOAT3(2.0f, 2.0f, 0.0f)); if (collectablebb2->Intersects(*collectablebb)) { collectables[i]->setPosition(XMFLOAT3(30.0f, (rand() % 40) - 19.0f, 0.0f)); } } } break; } }
bool Scene::Intersect(DirectX::FXMVECTOR start, DirectX::FXMVECTOR end, XMVECTOR& intersection) const { using namespace DirectX; const auto recScale = XMVectorReciprocal(XMLoadFloat3(&m_Scale)); const auto origin = XMVectorMultiply(start, recScale); const auto endPoint = XMVectorMultiply(end, recScale); unsigned blocksCnt = m_PolygonSurface->GetBlocksForLevelCount(0); auto direction = DirectX::XMVector3Normalize(end - start); typedef std::pair<float, const Voxels::BlockPolygons*> HitBlock; std::vector<HitBlock> hitBlocks; for (auto blockId = 0u; blockId < blocksCnt; ++blockId) { BoundingBox aabb; auto block = m_PolygonSurface->GetBlockForLevel(0, blockId); const auto minCorner = block->GetMinimalCorner(); const auto maxCorner = block->GetMaximalCorner(); aabb.Extents = XMFLOAT3((maxCorner.x - minCorner.x) / 2.f, (maxCorner.y - minCorner.y) / 2.f, (maxCorner.z - minCorner.z) / 2.f); aabb.Center = XMFLOAT3(minCorner.x + aabb.Extents.x, minCorner.y + aabb.Extents.y, minCorner.z + aabb.Extents.z); float distance = 0; if (aabb.Intersects(start, direction, distance)) { hitBlocks.push_back(std::make_pair(distance, block)); } } std::sort(hitBlocks.begin(), hitBlocks.end(), [](HitBlock& block1, HitBlock& block2) { return std::abs(block1.first) < std::abs(block2.first); }); float distance = 0; float nearest = std::numeric_limits<float>::max(); bool found = false; XMVECTOR V0, V1, V2; for (auto block = hitBlocks.cbegin(); block != hitBlocks.cend(); ++block) { const auto blockRef = block->second; unsigned indicesCnt = 0; auto indices = blockRef->GetIndices(&indicesCnt); for (auto triangle = 0u; triangle < indicesCnt; triangle += 3) { auto vertices = blockRef->GetVertices(nullptr); V0 = XMLoadFloat3(reinterpret_cast<const DirectX::XMFLOAT3*>(&vertices[indices[triangle]].Position)); V1 = XMLoadFloat3(reinterpret_cast<const DirectX::XMFLOAT3*>(&vertices[indices[triangle + 1]].Position)); V2 = XMLoadFloat3(reinterpret_cast<const DirectX::XMFLOAT3*>(&vertices[indices[triangle + 2]].Position)); if (TriangleTests::Intersects(start, direction, V0, V1, V2, distance)) { nearest = std::min(nearest, distance); found = true; } } if (found) break; } intersection = start + nearest * direction; return found; }
std::unique_ptr<float> Ray::Intersects(const BoundingBox& bb) { return bb.Intersects(*this); }