Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int iLeafIndex = -1;
- Contact contact; // Pass this to the CheckOverlap function and overwrite a_Contact when the collision time of contact is lower.
- contact.m_fCollisionTime = std::numeric_limits<float>::max();
- // Allocate memory..
- std::stack<Node*> queueNodes;
- // Traversing, start with the root node.
- Node* currentNode = *m_tree;
- // There is only one object.
- if (currentNode->m_Type == LEAF) {
- if (CheckOverlap(a_Ray, currentNode, iLeafIndex, contact)) {
- a_Contact = contact;
- a_Shape = currentNode->m_Shapes[iLeafIndex];
- }
- return;
- }
- do
- {
- // Get the node childrens.
- Node* childNodeL = (currentNode->m_NodeLeft != NULL) ? currentNode->m_NodeLeft : NULL;
- Node* childNodeR = (currentNode->m_NodeRight != NULL) ? currentNode->m_NodeRight : NULL;
- // Check for intersection with the left node or leaf.
- bool bOverlapNodeL = CheckOverlap(a_Ray, childNodeL, iLeafIndex, contact);
- // Overwrite the shape and contact data if the left node is a leaf and the collision time is closer by.
- if (bOverlapNodeL && childNodeL->m_Type == LEAF && a_Contact.m_fCollisionTime > contact.m_fCollisionTime) {
- a_Contact = contact;
- a_Shape = childNodeL->m_Shapes[iLeafIndex];
- }
- // Check for intersection with the right node or leaf.
- bool bOverlapNodeR = CheckOverlap(a_Ray, childNodeR, iLeafIndex, contact);
- // Overwrite the shape and contact data if the right node is a leaf and the collision time is closer by.
- if (bOverlapNodeR && childNodeR->m_Type == LEAF && a_Contact.m_fCollisionTime > contact.m_fCollisionTime) {
- a_Contact = contact;
- a_Shape = childNodeR->m_Shapes[iLeafIndex];
- }
- // Traverse further into the left or right node when there is an overlap and it is not a leaf.
- bool bTraverseL = (bOverlapNodeL && childNodeL->m_Type != LEAF);
- bool bTraverseR = (bOverlapNodeR && childNodeR->m_Type != LEAF);
- // We cannot traverse further into this node. Look in the stack if there is another option we haven't traversed into.
- if (!bTraverseL && !bTraverseR) {
- if (!queueNodes.empty()) {
- currentNode = queueNodes.top();
- queueNodes.pop();
- }
- else {
- currentNode = NULL;
- }
- }
- else
- {
- // First, go through the left node and when we cannot traverse there, look into the right node.
- currentNode = (bTraverseL) ? childNodeL : childNodeR;
- // Add the right child node to the stack, because there is an overlap with both the nodes and we are going through the left node first.
- if (bTraverseL && bTraverseR) {
- queueNodes.push(childNodeR);
- }
- }
- } while (currentNode != NULL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement