RESULT DebugRenderer::Quad( const AABB& bounds, Color color, float fOpacity ) { RESULT rval = S_OK; // Draw screen-aligned 2D rectangular outline for the AABB. // TODO: have solid mode using a quad. WORLD_POSITION corner1; WORLD_POSITION corner2; WORLD_POSITION corner3; WORLD_POSITION corner4; corner1.x = bounds.GetMin().x; corner1.y = bounds.GetMin().y; corner1.z = 0.0f; corner2.x = bounds.GetMin().x; corner2.y = corner1.y + bounds.GetHeight(); corner2.z = 0.0f; corner3.x = bounds.GetMax().x; corner3.y = bounds.GetMax().y; corner3.z = 0.0f; corner4.x = bounds.GetMax().x; corner4.y = corner3.y - bounds.GetHeight(); corner4.z = 0.0f; CHR( Line(corner1, vec3(0, 1, 0), Color::Green(), fOpacity, bounds.GetHeight(), 5.0f) ); CHR( Line(corner2, vec3(1, 0, 0), Color::Green(), fOpacity, bounds.GetWidth(), 5.0f) ); CHR( Line(corner3, vec3(0, -1, 0), Color::Green(), fOpacity, bounds.GetHeight(), 5.0f) ); CHR( Line(corner4, vec3(-1, 0, 0), Color::Green(), fOpacity, bounds.GetWidth(), 5.0f) ); // CHR( Point( bounds.GetCenter(), Color::Black(), 1.0f ) ); Exit: return rval; }
bool AABB::IsInside(const AABB& box) const { if(box.GetMin(0)>GetMin(0)) return false; if(box.GetMin(1)>GetMin(1)) return false; if(box.GetMin(2)>GetMin(2)) return false; if(box.GetMax(0)<GetMax(0)) return false; if(box.GetMax(1)<GetMax(1)) return false; if(box.GetMax(2)<GetMax(2)) return false; return true; }
bool Intersects(const AABB& aabb, const Capsule& cap) { float r = cap.m_radius; AABB ab( aabb.GetMin(0) - r, aabb.GetMax(0) + r, aabb.GetMin(1) - r, aabb.GetMax(1) + r, aabb.GetMin(2) - r, aabb.GetMax(2) + r); return Clip(cap.m_lineseg, ab, nullptr); }
//---------------------------------------------------------------- /// AABB vs Ray //---------------------------------------------------------------- bool Intersects(const AABB& inAABB, const Ray& inRay, f32 &outfT1, f32 &outfT2) { //Using the slab intersection method we check for intersection //against the 3 planes (slabs). If the ray fails itersection with any //slab then it does not intersect the box f32 t2 = std::numeric_limits<f32>::infinity(); f32 t1 = -t2; Vector3 vRayOrigin = inRay.vOrigin; Vector3 vRayDir = inRay.vDirection * inRay.fLength; Vector3 vAABBMin = inAABB.GetMin(); Vector3 vAABBMax = inAABB.GetMax(); //----X Slab if(!RaySlabIntersect(vRayOrigin.x, vRayDir.x, vAABBMin.x, vAABBMax.x, t1, t2)) return false; //----Y Slab if(!RaySlabIntersect(vRayOrigin.y, vRayDir.y, vAABBMin.y, vAABBMax.y, t1, t2)) return false; //----Z Slab if(!RaySlabIntersect(vRayOrigin.z, vRayDir.z, vAABBMin.z, vAABBMax.z, t1, t2)) return false; //We haven't failed intersection against any slab therefore we must have hit //t1 and t2 will give us our entry and exit point on the parametric ray outfT1 = t1; outfT2 = t2; return true; }
AABB Node::GetAABB() { AABB box; for (int i = 0; i < child_array_->Size(); i++) { Node * node = (Node*)child_array_->At(i); AABB tbox = node->GetAABB(); vec3 v = tbox.GetMax(); box.Add(v.x, v.y, v.z); v = tbox.GetMin(); box.Add(v.x, v.y, v.z); } vec3 v = aabb_.GetMax(); box.Add(v.x, v.y, v.z); v = aabb_.GetMin(); box.Add(v.x,v.y, v.z); mat4 t = this->GetTransform(); if (parents_) { t = parents_->GetTransform() * t; } box.SetToTransformedBox(t); return box; }
void UnCollide(GameObject* go, const Vec3f& oldPos, const AABB& aabb) { /* Move back so not interpenetrating: Boxes now intersect in all axes. Moving away by penetration depth in any axis will resolve the intersection. To choose which axis, use the previous position. E.g. if previously not intersecting in X, move away in X axis. */ // Intersecton region AABB ir = aabb.Intersection(go->GetAABB()); Vec3f goPos = go->GetPos(); AABB oldBox = go->GetAABB(); Vec3f move = oldPos - goPos; oldBox.Translate(move); // Oldbox should NOT be intersecting in one or more axes Vec3f penDist( ir.GetMax(0) - ir.GetMin(0), ir.GetMax(1) - ir.GetMin(1), ir.GetMax(2) - ir.GetMin(2)); penDist *= 1.01f; // move away a bit more to be sure of clearing the collision if (oldBox.GetMax(0) < aabb.GetMin(0)) { // Old box to left of box, so move away to the left goPos.x -= penDist.x; } else if (oldBox.GetMin(0) > aabb.GetMax(0)) { goPos.x += penDist.x; } /* else if (oldBox.GetMax(1) < aabb.GetMin(1)) { goPos.y -= penDist.y; } else if (oldBox.GetMin(1) > aabb.GetMax(1)) { goPos.y += penDist.y; } */ else if (oldBox.GetMax(2) < aabb.GetMin(2)) { goPos.z -= penDist.z; } else if (oldBox.GetMin(2) > aabb.GetMax(2)) { goPos.z += penDist.z; } go->SetPos(goPos); }
_Use_decl_annotations_ void BIH::Query(const AABB& test, uint32_t nodeIndex, uint32_t* numTriangles) { Node& node = _nodes[nodeIndex]; if (node.Axis == 3) { // Leaf *numTriangles += node.NumTriangles; } else { if (*(&test.GetMin().x + node.Axis) < node.Max) { Query(test, nodeIndex + 1, numTriangles); } if (*(&test.GetMax().x + node.Axis) > node.Min) { Query(test, node.MaxNode, numTriangles); } } }
_Use_decl_annotations_ void BIH::AppendVisibleIndices(const AABB& test, uint32_t nodeIndex, uint32_t* indices, uint32_t maxIndices, uint32_t* numIndices, uint32_t* materials, uint32_t maxMaterials, uint32_t* numMaterials) { if (*numIndices + 3 > maxIndices) { // No more room return; } Node& node = _nodes[nodeIndex]; if (node.Axis == 3) { // Leaf Triangle* t = &_triangles[node.StartTriangle]; for (uint32_t i = 0; (i < node.NumTriangles) && (*numIndices + 3 <= maxIndices); ++i, ++t) { indices[*numIndices] = t->i0; ++(*numIndices); indices[*numIndices] = t->i1; ++(*numIndices); indices[*numIndices] = t->i2; ++(*numIndices); materials[*numMaterials] = t->MaterialId; ++(*numMaterials); } } else { // Inner node if (*(&test.GetMin().x + node.Axis) < node.Max) { AppendVisibleIndices(test, nodeIndex + 1, indices, maxIndices, numIndices, materials, maxMaterials, numMaterials); } if (*(&test.GetMax().x + node.Axis) > node.Min) { AppendVisibleIndices(test, node.MaxNode, indices, maxIndices, numIndices, materials, maxMaterials, numMaterials); } } }
//---------------------------------------------------------------- /// AABB vs AABB //---------------------------------------------------------------- bool Intersects(const AABB& inAABBLHS, const AABB& inAABBRHS) { return (inAABBLHS.GetMax().x > inAABBRHS.GetMin().x && inAABBLHS.GetMin().x < inAABBRHS.GetMax().x) && (inAABBLHS.GetMax().y > inAABBRHS.GetMin().y && inAABBLHS.GetMin().y < inAABBRHS.GetMax().y) && (inAABBLHS.GetMax().z > inAABBRHS.GetMin().z && inAABBLHS.GetMin().z < inAABBRHS.GetMax().z); }
bool Ray::Intersects(AABB& box) { switch (m_classification) { case RayType::MMM: if ((m_origin.x < box.GetMin().x) || (m_origin.y < box.GetMin().y) || (m_origin.z < box.GetMin().z) || (m_jbyi * box.GetMin().x - box.GetMax().y + m_cxy > 0) || (m_ibyj * box.GetMin().y - box.GetMax().x + m_cyx > 0) || (m_jbyk * box.GetMin().z - box.GetMax().y + m_czy > 0) || (m_kbyj * box.GetMin().y - box.GetMax().z + m_cyz > 0) || (m_kbyi * box.GetMin().x - box.GetMax().z + m_cxz > 0) || (m_ibyk * box.GetMin().z - box.GetMax().x + m_czx > 0)) return false; return true; case RayType::MMP: if ((m_origin.x < box.GetMin().x) || (m_origin.y < box.GetMin().y) || (m_origin.z > box.GetMax().z) || (m_jbyi * box.GetMin().x - box.GetMax().y + m_cxy > 0) || (m_ibyj * box.GetMin().y - box.GetMax().x + m_cyx > 0) || (m_jbyk * box.GetMax().z - box.GetMax().y + m_czy > 0) || (m_kbyj * box.GetMin().y - box.GetMin().z + m_cyz < 0) || (m_kbyi * box.GetMin().x - box.GetMin().z + m_cxz < 0) || (m_ibyk * box.GetMax().z - box.GetMax().x + m_czx > 0)) return false; return true; case RayType::MPM: if ((m_origin.x < box.GetMin().x) || (m_origin.y > box.GetMax().y) || (m_origin.z < box.GetMin().z) || (m_jbyi * box.GetMin().x - box.GetMin().y + m_cxy < 0) || (m_ibyj * box.GetMax().y - box.GetMax().x + m_cyx > 0) || (m_jbyk * box.GetMin().z - box.GetMin().y + m_czy < 0) || (m_kbyj * box.GetMax().y - box.GetMax().z + m_cyz > 0) || (m_kbyi * box.GetMin().x - box.GetMax().z + m_cxz > 0) || (m_ibyk * box.GetMin().z - box.GetMax().x + m_czx > 0)) return false; return true; case RayType::MPP: if ((m_origin.x < box.GetMin().x) || (m_origin.y > box.GetMax().y) || (m_origin.z > box.GetMax().z) || (m_jbyi * box.GetMin().x - box.GetMin().y + m_cxy < 0) || (m_ibyj * box.GetMax().y - box.GetMax().x + m_cyx > 0) || (m_jbyk * box.GetMax().z - box.GetMin().y + m_czy < 0) || (m_kbyj * box.GetMax().y - box.GetMin().z + m_cyz < 0) || (m_kbyi * box.GetMin().x - box.GetMin().z + m_cxz < 0) || (m_ibyk * box.GetMax().z - box.GetMax().x + m_czx > 0)) return false; return true; case RayType::PMM: if ((m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.z < box.GetMin().z) || (m_jbyi * box.GetMax().x - box.GetMax().y + m_cxy > 0) || (m_ibyj * box.GetMin().y - box.GetMin().x + m_cyx < 0) || (m_jbyk * box.GetMin().z - box.GetMax().y + m_czy > 0) || (m_kbyj * box.GetMin().y - box.GetMax().z + m_cyz > 0) || (m_kbyi * box.GetMax().x - box.GetMax().z + m_cxz > 0) || (m_ibyk * box.GetMin().z - box.GetMin().x + m_czx < 0)) return false; return true; case RayType::PMP: if ((m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.z > box.GetMax().z) || (m_jbyi * box.GetMax().x - box.GetMax().y + m_cxy > 0) || (m_ibyj * box.GetMin().y - box.GetMin().x + m_cyx < 0) || (m_jbyk * box.GetMax().z - box.GetMax().y + m_czy > 0) || (m_kbyj * box.GetMin().y - box.GetMin().z + m_cyz < 0) || (m_kbyi * box.GetMax().x - box.GetMin().z + m_cxz < 0) || (m_ibyk * box.GetMax().z - box.GetMin().x + m_czx < 0)) return false; return true; case RayType::PPM: if ((m_origin.x > box.GetMax().x) || (m_origin.y > box.GetMax().y) || (m_origin.z < box.GetMin().z) || (m_jbyi * box.GetMax().x - box.GetMin().y + m_cxy < 0) || (m_ibyj * box.GetMax().y - box.GetMin().x + m_cyx < 0) || (m_jbyk * box.GetMin().z - box.GetMin().y + m_czy < 0) || (m_kbyj * box.GetMax().y - box.GetMax().z + m_cyz > 0) || (m_kbyi * box.GetMax().x - box.GetMax().z + m_cxz > 0) || (m_ibyk * box.GetMin().z - box.GetMin().x + m_czx < 0)) return false; return true; case RayType::PPP: if ((m_origin.x > box.GetMax().x) || (m_origin.y > box.GetMax().y) || (m_origin.z > box.GetMax().z) || (m_jbyi * box.GetMax().x - box.GetMin().y + m_cxy < 0) || (m_ibyj * box.GetMax().y - box.GetMin().x + m_cyx < 0) || (m_jbyk * box.GetMax().z - box.GetMin().y + m_czy < 0) || (m_kbyj * box.GetMax().y - box.GetMin().z + m_cyz < 0) || (m_kbyi * box.GetMax().x - box.GetMin().z + m_cxz < 0) || (m_ibyk * box.GetMax().z - box.GetMin().x + m_czx < 0)) return false; return true; case RayType::OMM: if ((m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.z < box.GetMin().z) || (m_jbyk * box.GetMin().z - box.GetMax().y + m_czy > 0) || (m_kbyj * box.GetMin().y - box.GetMax().z + m_cyz > 0)) return false; return true; case RayType::OMP: if ((m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.z > box.GetMax().z) || (m_jbyk * box.GetMax().z - box.GetMax().y + m_czy > 0) || (m_kbyj * box.GetMin().y - box.GetMin().z + m_cyz < 0)) return false; return true; case RayType::OPM: if ((m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y > box.GetMax().y) || (m_origin.z < box.GetMin().z) || (m_jbyk * box.GetMin().z - box.GetMin().y + m_czy < 0) || (m_kbyj * box.GetMax().y - box.GetMax().z + m_cyz > 0)) return false; return true; case RayType::OPP: if ((m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y > box.GetMax().y) || (m_origin.z > box.GetMax().z) || (m_jbyk * box.GetMax().z - box.GetMin().y + m_czy < 0) || (m_kbyj * box.GetMax().y - box.GetMin().z + m_cyz < 0)) return false; return true; case RayType::MOM: if ((m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y) || (m_origin.x < box.GetMin().x) || (m_origin.z < box.GetMin().z) || (m_kbyi * box.GetMin().x - box.GetMax().z + m_cxz > 0) || (m_ibyk * box.GetMin().z - box.GetMax().x + m_czx > 0)) return false; return true; case RayType::MOP: if ((m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y) || (m_origin.x < box.GetMin().x) || (m_origin.z > box.GetMax().z) || (m_kbyi * box.GetMin().x - box.GetMin().z + m_cxz < 0) || (m_ibyk * box.GetMax().z - box.GetMax().x + m_czx > 0)) return false; return true; case RayType::POM: if ((m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y) || (m_origin.x > box.GetMax().x) || (m_origin.z < box.GetMin().z) || (m_kbyi * box.GetMax().x - box.GetMax().z + m_cxz > 0) || (m_ibyk * box.GetMin().z - box.GetMin().x + m_czx < 0)) return false; return true; case RayType::POP: if ((m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y) || (m_origin.x > box.GetMax().x) || (m_origin.z > box.GetMax().z) || (m_kbyi * box.GetMax().x - box.GetMin().z + m_cxz < 0) || (m_ibyk * box.GetMax().z - box.GetMin().x + m_czx < 0)) return false; return true; case RayType::MMO: if ((m_origin.z < box.GetMin().z) || (m_origin.z > box.GetMax().z) || (m_origin.x < box.GetMin().x) || (m_origin.y < box.GetMin().y) || (m_jbyi * box.GetMin().x - box.GetMax().y + m_cxy > 0) || (m_ibyj * box.GetMin().y - box.GetMax().x + m_cyx > 0)) return false; return true; case RayType::MPO: if ((m_origin.z < box.GetMin().z) || (m_origin.z > box.GetMax().z) || (m_origin.x < box.GetMin().x) || (m_origin.y > box.GetMax().y) || (m_jbyi * box.GetMin().x - box.GetMin().y + m_cxy < 0) || (m_ibyj * box.GetMax().y - box.GetMax().x + m_cyx > 0)) return false; return true; case RayType::PMO: if ((m_origin.z < box.GetMin().z) || (m_origin.z > box.GetMax().z) || (m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_jbyi * box.GetMax().x - box.GetMax().y + m_cxy > 0) || (m_ibyj * box.GetMin().y - box.GetMin().x + m_cyx < 0)) return false; return true; case RayType::PPO: if ((m_origin.z < box.GetMin().z) || (m_origin.z > box.GetMax().z) || (m_origin.x > box.GetMax().x) || (m_origin.y > box.GetMax().y) || (m_jbyi * box.GetMax().x - box.GetMin().y + m_cxy < 0) || (m_ibyj * box.GetMax().y - box.GetMin().x + m_cyx < 0)) return false; return true; case RayType::MOO: if ((m_origin.x < box.GetMin().x) || (m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y) || (m_origin.z < box.GetMin().z) || (m_origin.z > box.GetMax().z)) return false; return true; case RayType::POO: if ((m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y) || (m_origin.z < box.GetMin().z) || (m_origin.z > box.GetMax().z)) return false; return true; case RayType::OMO: if ((m_origin.y < box.GetMin().y) || (m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.z < box.GetMin().z) || (m_origin.z > box.GetMax().z)) return false; if ((m_origin.y > box.GetMax().y) || (m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.z < box.GetMin().z) || (m_origin.z > box.GetMax().z)) return false; if ((m_origin.z < box.GetMin().z) || (m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y)) return false; if ((m_origin.z > box.GetMax().z) || (m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y)) return false; return true; case RayType::OPO: if ((m_origin.y > box.GetMax().y) || (m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.z < box.GetMin().z) || (m_origin.z > box.GetMax().z)) return false; if ((m_origin.z < box.GetMin().z) || (m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y)) return false; if ((m_origin.z > box.GetMax().z) || (m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y)) return false; return true; case RayType::OOM: if ((m_origin.z < box.GetMin().z) || (m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y)) return false; if ((m_origin.z > box.GetMax().z) || (m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y)) return false; return true; case RayType::OOP: if ((m_origin.z > box.GetMax().z) || (m_origin.x < box.GetMin().x) || (m_origin.x > box.GetMax().x) || (m_origin.y < box.GetMin().y) || (m_origin.y > box.GetMax().y)) return false; return true; } return false; }
void Object::SetHitbox(AABB hitbox) { this->hitbox.Set(hitbox.GetMin(), hitbox.GetMax()); }
static bool LoadBIN(const char* filename, SurfaceManager& test, const float* scale=null, bool mergeMeshes=false, udword tesselation=0, TesselationScheme ts = TESS_BUTTERFLY) { IceFile BinFile(filename); if(!BinFile.IsValid()) return false; const udword NbMeshes = BinFile.LoadDword(); printf("LoadBIN: loading %d meshes...\n", NbMeshes); AABB GlobalBounds; GlobalBounds.SetEmpty(); udword TotalNbTris = 0; udword TotalNbVerts = 0; if(!mergeMeshes) { for(udword i=0;i<NbMeshes;i++) { const udword Collidable = BinFile.LoadDword(); const udword Renderable = BinFile.LoadDword(); const udword NbVerts = BinFile.LoadDword(); const udword NbFaces = BinFile.LoadDword(); // TotalNbTris += NbFaces; // TotalNbVerts += NbVerts; IndexedSurface* IS = test.CreateManagedSurface(); bool Status = IS->Init(NbFaces, NbVerts); ASSERT(Status); Point* Verts = IS->GetVerts(); for(udword j=0;j<NbVerts;j++) { Verts[j].x = BinFile.LoadFloat(); Verts[j].y = BinFile.LoadFloat(); Verts[j].z = BinFile.LoadFloat(); if(scale) Verts[j] *= *scale; if(0) { Matrix3x3 RotX; RotX.RotX(HALFPI*0.5f); Verts[j] *= RotX; Verts[j] += Point(0.1f, -0.2f, 0.3f); } GlobalBounds.Extend(Verts[j]); } IndexedTriangle* F = IS->GetFaces(); for(udword j=0;j<NbFaces;j++) { F[j].mRef[0] = BinFile.LoadDword(); F[j].mRef[1] = BinFile.LoadDword(); F[j].mRef[2] = BinFile.LoadDword(); } /* if(tesselation) { for(udword j=0;j<tesselation;j++) { if(ts==TESS_BUTTERFLY) { ButterflyScheme BS; IS->Subdivide(BS); } else if(ts==TESS_POLYHEDRAL) { PolyhedralScheme PS; IS->Subdivide(PS); } } }*/ if(tesselation) Tesselate(IS, tesselation, ts); if(gUseMeshCleaner) { MeshCleaner Cleaner(IS->GetNbVerts(), IS->GetVerts(), IS->GetNbFaces(), IS->GetFaces()->mRef); IS->Init(Cleaner.mNbTris, Cleaner.mNbVerts, Cleaner.mVerts, (const IndexedTriangle*)Cleaner.mIndices); } TotalNbTris += IS->GetNbFaces(); TotalNbVerts += IS->GetNbVerts(); // SaveBIN("c:\\TessBunny.bin", *IS); } } else { IndexedSurface* IS = test.CreateManagedSurface(); for(udword i=0;i<NbMeshes;i++) { const udword Collidable = BinFile.LoadDword(); const udword Renderable = BinFile.LoadDword(); const udword NbVerts = BinFile.LoadDword(); const udword NbFaces = BinFile.LoadDword(); IndexedSurface LocalIS; bool Status = LocalIS.Init(NbFaces, NbVerts); ASSERT(Status); Point* Verts = LocalIS.GetVerts(); for(udword j=0;j<NbVerts;j++) { Verts[j].x = BinFile.LoadFloat(); Verts[j].y = BinFile.LoadFloat(); Verts[j].z = BinFile.LoadFloat(); if(scale) Verts[j] *= *scale; GlobalBounds.Extend(Verts[j]); } IndexedTriangle* F = LocalIS.GetFaces(); for(udword j=0;j<NbFaces;j++) { F[j].mRef[0] = BinFile.LoadDword(); F[j].mRef[1] = BinFile.LoadDword(); F[j].mRef[2] = BinFile.LoadDword(); } IS->Merge(&LocalIS); } /* if(tesselation) { for(udword j=0;j<tesselation;j++) { ButterflyScheme BS; IS->Subdivide(BS); } }*/ if(tesselation) Tesselate(IS, tesselation, ts); TotalNbTris = IS->GetNbFaces(); TotalNbVerts = IS->GetNbVerts(); } test.SetGlobalBounds(GlobalBounds); const udword GrandTotal = sizeof(Point)*TotalNbVerts + sizeof(IndexedTriangle)*TotalNbTris; printf("LoadBIN: loaded %d tris and %d verts, for a total of %d Kb.\n", TotalNbTris, TotalNbVerts, GrandTotal/1024); printf("LoadBIN: min bounds: %f | %f | %f\n", GlobalBounds.GetMin(0), GlobalBounds.GetMin(1), GlobalBounds.GetMin(2)); printf("LoadBIN: max bounds: %f | %f | %f\n", GlobalBounds.GetMax(0), GlobalBounds.GetMax(1), GlobalBounds.GetMax(2)); return true; }
_Use_decl_annotations_ uint32_t BIH::ProcessTriangles(CompileTriangle** triangles, uint32_t count, const AABB& bounds) { assert(*triangles != nullptr); assert(count > 0); if (count < 4) { // Make a leaf Node node; node.Axis = 3; node.StartTriangle = (uint32_t)_triangles.size(); CompileTriangle* t = *triangles; while (t != nullptr) { CompileTriangle* next = t->Next; _triangles.push_back(Triangle(t)); delete t; t = next; } *triangles = nullptr; node.NumTriangles = (uint32_t)_triangles.size() - node.StartTriangle; _nodes.push_back(node); return (uint32_t)_nodes.size() - 1; } else { // Split // Choose longest axis as initial candidate, but we may end up // trying all three if we can't find a good one uint8_t axis = 0; float xLength = bounds.GetMax().x - bounds.GetMin().x; float yLength = bounds.GetMax().y - bounds.GetMin().y; float zLength = bounds.GetMax().z - bounds.GetMin().z; if (yLength > xLength) { axis = 1; if (zLength > yLength) { axis = 2; } } else if (zLength > xLength) { axis = 2; } uint8_t startAxis = axis; CompileTriangle* t; uint32_t minCount; uint32_t maxCount; // Loop and test each axis. Doesn't actually modify lists yet, just counting for (;;) { t = *triangles; float totalAlongAxis = 0; while (t != nullptr) { totalAlongAxis += *(&t->Centroid.x + axis); t = t->Next; } float averageAlongAxis = totalAlongAxis / (float)count; t = *triangles; minCount = 0; maxCount = 0; while (t != nullptr) { float v = *(&t->Centroid.x + axis); if (v < averageAlongAxis) { ++minCount; } else { ++maxCount; } t = t->Next; } if (minCount == 0 || maxCount == 0) { // try the next axis if this one wasn't any good axis = (axis + 1) % 3; if (axis == startAxis) { // bail, no good break; } } else { // this axis is fine. break; } }; if (minCount == 0 || maxCount == 0) { // Failed to split, just make it a leaf Node node; node.Axis = 3; node.StartTriangle = (uint32_t)_triangles.size(); CompileTriangle* t = *triangles; while (t != nullptr) { CompileTriangle* next = t->Next; _triangles.push_back(Triangle(t)); delete t; t = next; } *triangles = nullptr; node.NumTriangles = (uint32_t)_triangles.size() - node.StartTriangle; _nodes.push_back(node); return (uint32_t)_nodes.size() - 1; } else { // Now we need to actually build lists CompileTriangle* minTriangles = nullptr; CompileTriangle* maxTriangles = nullptr; float min = FLT_MAX; float max = -FLT_MAX; minCount = 0; maxCount = 0; t = *triangles; float totalAlongAxis = 0; while (t != nullptr) { totalAlongAxis += *(&t->Centroid.x + axis); t = t->Next; } float averageAlongAxis = totalAlongAxis / (float)count; t = *triangles; while (t != nullptr) { CompileTriangle* next = t->Next; float v = *(&t->Centroid.x + axis); if (v < averageAlongAxis) { max = std::max(max, *(&t->Max.x + axis)); PUSH(&minTriangles, t); ++minCount; } else { min = std::min(min, *(&t->Min.x + axis)); PUSH(&maxTriangles, t); ++maxCount; } t = next; } Node node; node.Axis = axis; node.Max = max; node.Min = min; _nodes.push_back(node); uint32_t index = (uint32_t)_nodes.size() - 1; XMFLOAT3 boundsSide = bounds.GetMax(); *(&boundsSide.x + axis) = max; uint32_t minNode = ProcessTriangles(&minTriangles, minCount, AABB(bounds.GetMin(), boundsSide)); DBG_UNREFERENCED_LOCAL_VARIABLE(minNode); assert(minNode == index + 1); boundsSide = bounds.GetMin(); *(&boundsSide.x + axis) = min; _nodes[index].MaxNode = ProcessTriangles(&maxTriangles, maxCount, AABB(boundsSide, bounds.GetMax())); return index; } } }
// Move moving object away from stationary object so they do not intersect. // Relies on knowing the old (non-intersecting) position of the moving object. void PreventIntersection( GameObject* moving, const GameObject* stationary, const Vec3f& oldPos) { std::cout << "Moving apart: moving: " << Describe(moving) << " stnry: " << Describe(stationary) << ": "; /* Move back so not interpenetrating: Boxes now intersect in all axes. Moving away by penetration depth in any axis will resolve the intersection. To choose which axis, use the previous position. E.g. if previously not intersecting in X, move away in X axis. */ // Intersecton region AABB ir = stationary->GetAABB().Intersection(moving->GetAABB()); Vec3f goPos = moving->GetPos(); // Create AABB in old position AABB oldBox = moving->GetAABB(); Vec3f move = oldPos - goPos; oldBox.Translate(move); // Oldbox should not be intersecting in one or more axes const AABB& stationaryBox = stationary->GetAABB(); // Penetration depth in each axis Vec3f penDist( ir.GetMax(0) - ir.GetMin(0), ir.GetMax(1) - ir.GetMin(1), ir.GetMax(2) - ir.GetMin(2)); penDist *= 1.01f; // move away a bit more to be sure of clearing the collision std::cout << "Pen depths: " << Describe(penDist) << ": "; //Vec3f vel = moving->GetVel(); if (oldBox.GetMax(0) < stationaryBox.GetMin(0)) { // Old box to left of stationary object, so move away to the left // Assert(penDist.x > 0); goPos.x -= penDist.x; std::cout << "x -= " << penDist.x; } else if (oldBox.GetMin(0) > stationaryBox.GetMax(0)) { // Assert(penDist.x > 0); goPos.x += penDist.x; std::cout << "x += " << penDist.x; } if (oldBox.GetMax(1) < stationaryBox.GetMin(1)) { // Assert(penDist.y > 0); goPos.y -= penDist.y; std::cout << "y -= " << penDist.y; } else if (oldBox.GetMin(1) > stationaryBox.GetMax(1)) { // Assert(penDist.y > 0); goPos.y += penDist.y; std::cout << "y += " << penDist.y; } if (oldBox.GetMax(2) < stationaryBox.GetMin(2)) { // Assert(penDist.z > 0); goPos.z -= penDist.z; std::cout << "z -= " << penDist.z; } else if (oldBox.GetMin(2) > stationaryBox.GetMax(2)) { // Assert(penDist.z > 0); goPos.z += penDist.z; std::cout << "z += " << penDist.z; } std::cout << "\n"; moving->SetPos(goPos); // moving->RecalcAABB(); // Test that AABBs are no longer intersecting // if (stationary->GetAABB().Intersects(moving->GetAABB())) // { // AABB ir = stationary->GetAABB().Intersection(moving->GetAABB()); // } }