MCPcopy Create free account
hub / github.com/DanielChappuis/reactphysics3d / raycast

Method raycast

src/collision/broadphase/DynamicAABBTree.cpp:684–747  ·  view source on GitHub ↗

Ray casting method

Source from the content-addressed store, hash-verified

682
683// Ray casting method
684void DynamicAABBTree::raycast(const Ray& ray, DynamicAABBTreeRaycastCallback& callback) const {
685
686 RP3D_PROFILE("DynamicAABBTree::raycast()", mProfiler);
687
688 decimal maxFraction = ray.maxFraction;
689
690 // Compute the inverse ray direction
691 const Vector3 rayDirection = ray.point2 - ray.point1;
692 const Vector3 rayDirectionInverse(decimal(1.0) / rayDirection.x, decimal(1.0) / rayDirection.y, decimal(1.0) / rayDirection.z);
693
694 Stack<int32> stack(mAllocator, 128);
695 stack.push(mRootNodeID);
696
697 // Walk through the tree from the root looking for colliders
698 // that overlap with the ray AABB
699 while (stack.size() > 0) {
700
701 // Get the next node in the stack
702 int32 nodeID = stack.pop();
703
704 // If it is a null node, skip it
705 if (nodeID == TreeNode::NULL_TREE_NODE) continue;
706
707 // Get the corresponding node
708 const TreeNode* node = mNodes + nodeID;
709
710 // Test if the ray intersects with the current node AABB
711 if (!node->aabb.testRayIntersect(ray.point1, rayDirectionInverse, maxFraction)) continue;
712
713 // If the node is a leaf of the tree
714 if (node->isLeaf()) {
715
716 Ray rayTemp(ray.point1, ray.point2, maxFraction);
717
718 // Call the callback that will raycast again the broad-phase shape
719 decimal hitFraction = callback.raycastBroadPhaseShape(nodeID, rayTemp);
720
721 // If the user returned a hitFraction of zero, it means that
722 // the raycasting should stop here
723 if (hitFraction == decimal(0.0)) {
724 return;
725 }
726
727 // If the user returned a positive fraction
728 if (hitFraction > decimal(0.0)) {
729
730 // We update the maxFraction value and the ray
731 // AABB using the new maximum fraction
732 if (hitFraction < maxFraction) {
733 maxFraction = hitFraction;
734 }
735 }
736
737 // If the user returned a negative fraction, we continue
738 // the raycasting as if the collider did not exist
739 }
740 else { // If the node has children
741

Callers

nothing calls this directly

Calls 6

pushMethod · 0.80
popMethod · 0.80
testRayIntersectMethod · 0.80
isLeafMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected