Quick 'N Muddied Point-Plane Collision Detection

A span weeks agone I was inward a frenzy to implement collision detection inward my SkyCop demo. Sure, I planned to larn dorsum together with report it inward to a greater extent than exceptional at some point, but for the minute I only wanted to larn something done together with thus I could avoid flight through enemy ships together with outset blowing them up. After a few days of searching, I apace realized that collision detection is no lilliputian task; it's a dependent plain all on its own! I started amongst this flipcode article that served equally a practiced introduction but left me a chip confused. After to a greater extent than enquiry together with trial-and-error I had a working instance role that I would similar to percentage amongst anyone needing to feed that same fundamental urge to larn something working quickly.

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; } 

Subscribe to receive free email updates:

0 Response to "Quick 'N Muddied Point-Plane Collision Detection"

Post a Comment