The function, written inward C++ for Direct3D 9, determines if a unmarried betoken volition cross a triangular bird (parameters vP1-vP3 specified inward clockwise order). Parameter prevPos is the initial seat your character/camera/object moves from together with curPos is the seat to which it volition survive moved. For example, if y'all desire to notice if an FPS actor volition collide amongst a wall, y'all would telephone band this role amongst the camera's initial seat equally prevPos together with its predicted adjacent seat [prevPos + (speed * direction)] equally curPos. Submit a comment if y'all convey whatever questions, together with perish on inward heed I volition speak over the code inward exceptional inward a futurity post.
Btw, I'm certain at that spot are engines that grip all this materials for you, but since I haven't gotten into those nonetheless it was a squeamish exercise for me. And I produce apologize for the ugly formatting. I'll piece of job on making code selections expect ameliorate inward futurity posts!
enum PlanePosition { PlaneFront, PlaneBack, OnPlane }; bool IntersectsTriangle(D3DXVECTOR3 prevPos, D3DXVECTOR3 curPos, D3DXVECTOR3 vP1, D3DXVECTOR3 vP2, D3DXVECTOR3 vP3) { float p, d, t, thetaSum; PlanePosition prevLoc, curLoc; D3DXVECTOR3 ray, v1, v2, v3, vNormal, intersect; // Compute normal vector for the bird v1 = (vP2 - vP1); v2 = (vP3 - vP2); D3DXVec3Cross(&vNormal, &v1, &v2); D3DXVec3Normalize(&vNormal, &vNormal); // Compute bird distance d = - D3DXVec3Dot(&vP1, &vNormal); // Classify positions using planar equation p = (D3DXVec3Dot(&vNormal, &prevPos) + d); if ( p > 0.0f ) prevLoc = PlaneFront; else if ( p < 0.0f ) prevLoc = PlaneBack; else prevLoc = OnPlane; p = (D3DXVec3Dot(&vNormal, &curPos) + d); if( p > 0.0f ) curLoc = PlaneFront; else if (p < 0.0f ) curLoc = PlaneBack; else curLoc = OnPlane; if (prevLoc == curLoc) furnish false; // Crossed the plane, did intersect travel on within triangle? // Get normalized ray ray = curPos - prevPos; D3DXVec3Normalize(&ray, &ray); // t = betoken along ray where intersection occurs t = - (d + D3DXVec3Dot(&vNormal, &prevPos)) / D3DXVec3Dot(&vNormal, &ray); // Get intersection betoken on the bird intersect = prevPos + (ray * t); // Determine if intersection is within triangle bird // Angles or together with thus intersection should full 360 degrees (2 PI) v1 = intersect - vP1; v2 = intersect - vP2; v3 = intersect - vP3; D3DXVec3Normalize(&v1, &v1); D3DXVec3Normalize(&v2, &v2); D3DXVec3Normalize(&v3, &v3); thetaSum = acos(D3DXVec3Dot(&v1, &v2)) + acos(D3DXVec3Dot(&v2, &v3)) + acos(D3DXVec3Dot(&v3, &v1)); // If centre == 2PI nosotros convey an intersection! if ((thetaSum >= ((2 * D3DX_PI) - 0.1)) && (thetaSum <= ((2 * D3DX_PI) + 0.1))) { furnish true; } furnish false; }
0 Response to "Quick 'N Muddied Point-Plane Collision Detection"
Post a Comment