int RecF::Collide(RecF Rec, RecF block) { // RecF is the moving RecF // block is the static RecF RecF broadphaseRecF = GetSweptBroadphaseRec(Rec); if (AABBCheck(broadphaseRecF, block)) { float normalx, normaly; float collisiontime = SweptAABB(Rec, block, normalx, normaly); float remainingtime = 1.0f - collisiontime; if (collisiontime < 1.0f) { if (Rec.collisionTime > collisiontime) Rec.collisionTime = collisiontime; if (normalx == -1) return 2; if (normalx == 1) return 1; if (normaly == -1) return 4; if (normaly == 1) return 3; } else if (AABBCheck(Rec, block)) { return 5; } } return 0; }
void Node::InsertObject(std::map<int, Node*> mapQuadTree, GameObject* object, Box objectBox, int &returnNodeId) { //if object doesn't belong to node, then return if (!AABBCheck(_BoundaryBox, objectBox)) return; //if node covers all screenhight and screenwidth, then divide it into 4 subnodes if (_BoundaryBox.fWidth > SCREEN_WIDTH || _BoundaryBox.fHeight > SCREEN_HEIGHT) { if (_Tl == NULL) { _Tl = new Node( _NodeId * 10 + eNodePosition::eTopLeft, _X - _Width / 4, _Y + _Height / 4, _Width / 2, _Height / 2 ); mapQuadTree[_Tl->GetNodeId()] = _Tl; } if (_Tr == NULL) { _Tr = new Node( _NodeId * 10 + eNodePosition::eTopRight, _X + _Width / 4, _Y + _Height / 4, _Width / 2, _Height / 2 ); mapQuadTree[_Tr->GetNodeId()] = _Tr; } if (_Bl == NULL) { _Bl = new Node( _NodeId * 10 + eNodePosition::eBotLeft, _X - _Width / 4, _Y - _Height / 4, _Width / 2, _Height / 2 ); mapQuadTree[_Bl->GetNodeId()] = _Bl; } if (_Br == NULL) { _Br = new Node( _NodeId * 10 + eNodePosition::eBotRight, _X + _Width / 4, _Y - _Height / 4, _Width / 2, _Height / 2 ); mapQuadTree[_Br->GetNodeId()] = _Br; } float left, right, top, bot; left = objectBox.fX; right = objectBox.fX + objectBox.fWidth; top = objectBox.fY; bot = objectBox.fY - objectBox.fHeight; //if object places in two diagonals, then add it to node if ((left < _X && right > _X) || (bot < _Y && top > _Y)) { _ListObjects.push_back(object); returnNodeId = this->_NodeId; } else { //insert each subnode to node _Tl->InsertObject(mapQuadTree, object, objectBox, returnNodeId); _Tr->InsertObject(mapQuadTree, object, objectBox, returnNodeId); _Bl->InsertObject(mapQuadTree, object, objectBox, returnNodeId); _Br->InsertObject(mapQuadTree, object, objectBox, returnNodeId); } } //else if node just covers enough screenwidth or screenheight, then add obj to node else { _ListObjects.push_back(object); returnNodeId = this->_NodeId; } }