Examples of wall avoidance algorithm dealing with rectangles instead of lines?

dbomb101
  • Examples of wall avoidance algorithm dealing with rectangles instead of lines? dbomb101

    I have a tile based engine and in the middle of translating out the example wall avoidance alogrithm in the book AI programming by example. I was wondering if the method below can be modified so that it takes a rectangle instead of vector C and D and still produce the same output ?

    /-------------------- LineIntersection2D-------------------------
    //
    //  Given 2 lines in 2D space AB, CD this returns true if an 
    //  intersection occurs and sets dist to the distance the intersection
    //  occurs along AB. Also sets the 2d vector point to the point of
    //  intersection
    //----------------------------------------------------------------- 
    inline bool LineIntersection2D(Vector2D   A,
                                   Vector2D   B,
                                   Vector2D   C, 
                                   Vector2D   D,
                                   double&     dist,
                                   Vector2D&  point)
    {
    
      double rTop = (A.y-C.y)*(D.x-C.x)-(A.x-C.x)*(D.y-C.y);
        double rBot = (B.x-A.x)*(D.y-C.y)-(B.y-A.y)*(D.x-C.x);
    
        double sTop = (A.y-C.y)*(B.x-A.x)-(A.x-C.x)*(B.y-A.y);
        double sBot = (B.x-A.x)*(D.y-C.y)-(B.y-A.y)*(D.x-C.x);
    
        if ( (rBot == 0) || (sBot == 0))
        {
            //lines are parallel
            return false;
        }
    
        double r = rTop/rBot;
        double s = sTop/sBot;
    
        if( (r > 0) && (r < 1) && (s > 0) && (s < 1) )
      {
        dist = Vec2DDistance(A,B) * r;
    
        point = A + r * (B - A);
    
        return true;
      }
    
        else
      {
            dist = 0;
    
        return false;
      }
    } 
    

  • Sorry to state the obvious, but rectangles are essentially 4 lines.

    Why not just reuse the code and have something like:

    RectangleIntersectionWithLine( Vector2D a, Vector2D b, BoundingBox box )  
    {  
        LineIntersection2D( a, b, box.Min, Vector2D( box.Min.x, box.Max.y ) );  
        LineIntersection2D( a, b, box.Min, Vector2D( box.Max.x, box.Min.y ) );  
        LineIntersection2D( a, b, box.Max, Vector2D( box.Min.x, box.Max.y ) );  
        LineIntersection2D( a, b, box.Max, Vector2D( box.Max.x, box.Min.y ) );  
    }
    

    I leave the return values, etc up to you.

Tags
c++ ai
Related questions and answers
  • quit = false; quit != true &amp;&amp; dead== false;) { double newTime= myStopwatch.ElapsedTime()- refreshes; if(newTime &gt;= speed){ theScreen.Insert(L1, 0+newlines, nrows...--; cspace++;}} for(int s= 0; s &lt;sspace; s++) buff[buff.size()-1].push_back(' '); // cwall2 //if(cwall2!= 0){ // if(cwall2&gt;0... //////////// bool move(int X, int Y) { location[0] += X; location[1] += Y; return true;}; };// end of sprite

  • i think i just found the solution. 1) the problem is that backbuffer surface and source surface are of different formats - that is why exception was thrown. 2) the image path needed double slash "C...(D3D_SDK_VERSION))) return false; std::wstring wsPath = L"C:\wood.bmp"; // path to the image D3DXIMAGE_INFO Info; if (FAILED(D3DXGetImageInfoFromFile(wsPath.c_str(), &amp;Info..._VERTEXPROCESSING, &amp;d3dpp, &amp;pd3dDevice))) return false; return true; } void render(void) { // check to make sure you have a valid Direct3D device if (NULL == pd3dDevice) return

  • (); missile-&gt;Init(true, s-&gt;X, s-&gt;Y); bullets-&gt;push_back(missile); } } void CleanUp() { for(unsigned int index = 0; index &lt; bullets-&gt;size(); index++) { if (bullets-&gt;at(index)-&gt;Alive == false) { bullets-&gt;erase(bullets-&gt;begin() + index); } } } void UpdateBullets() { for(unsigned int index = 0; index &lt...!"); return 0; } while(!done) { sceCtrlReadBufferPositive(&amp;pad, 1); if (pad.Buttons &amp; PSP_CTRL_START) { done = true; } else

  • I been working in the animation of a 2D platformer game in C++/SDL/OpenGL, and my team and I reach the point where we need to establish that every animation of the player (Walking, Running, etc... ) { position.x += getSpeedX(); playerMoveInX = true; return; } } else if ( position.x + getSpeedX() + width &gt; 0 ) { position.x += getSpeedX(); playerMoveInX = true; return; } playerMoveInX = false; } } And for the frames I have a class Animation which handles the following information: Uint32 frameRate, oldTime; int frameDelay

  • I'm writing a simple class to draw all the debugging lines I have in my scene at once. The code in the draw loop is this so far: (If I put for example, 2 instead of temporary2DVerts.size() where I...(D3D10_PRIMITIVE_TOPOLOGY_LINELIST); UINT stride = sizeof(VERTEX); UINT offset = 0; device-&gt;IASetVertexBuffers(0, 1, &amp;pBuffer, &amp;stride, &amp;offset); screenPass-&gt;Apply(0...; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; device-&gt;CreateBuffer(&amp;bd, NULL, &amp;pBuffer); void* pVoid; pBuffer-&gt;Map(D3D10_MAP_WRITE_DISCARD, 0, &amp;pVoid); //HERE memcpy(pVoid

  • &lt; numTriangles; i++) { MD2Triangle* triangle = triangles + i; for(int j = 0; j &lt; 3; j++) { MD2Vertex* v1 = frame1-&gt;vertices + triangle-&gt;vertices[j... = triangles + i; for(int j = 0; j &lt; 3; j++) { MD2Vertex* v1 = frame1-&gt;vertices + triangle-&gt;vertices[j]; MD2Vertex* v2 = frame2-&gt;vertices + triangle-&gt;vertices[j]; Vec3f pos = v1-&gt;pos * (1 - frac) + v2-&gt;pos * frac; Vec3f normal = v1-&gt;normal * (1 - frac) + v2-&gt;normal * frac; if (normal[0] == 0 &amp;&amp; normal[1

  • ::Vector3(0,1,0); p.d = 0; Ogre::MeshManager::getSingleton().createPlane( "FloorPlane", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, p, 200000, 200000, 20, 20, true, 1, 9000,9000,Ogre...)Point[0], (float)Point[1], (float)Point[2])); // set rotation btVector3 EulerRotation; QuaternionToEuler(TObject-&gt;getOrientation(), EulerRotation); node-&gt;setOrientation(1,(Ogre...::DEFAULT_RESOURCE_GROUP_NAME, p, 200000, 200000, 20, 20, true, 1, 9000,9000,Ogre::Vector3::UNIT_Z); ent = m_pSceneMgr-&gt;createEntity("floor", "FloorPlane"); ent-&gt;setMaterialName("Test/Floor

  • ); UINT offset = 0; device-&gt;IASetVertexBuffers(0, 1, &amp;pBuffer, &amp;stride, &amp;offset); device-&gt;IASetIndexBuffer(iBuffer, DXGI_FORMAT_R32_UINT, 0); D3DXMATRIX matRotate, matView...; pTransform-&gt;SetMatrix(&amp;matFinal._11); pPass-&gt;Apply(0); device-&gt;DrawIndexed(36, 0, 0); // display the rendered frame swapchain-&gt;Present(0, 0); } // this function sets up... sorry if it's in the wrong place. SORRY IN ADVANCE FOR THE AMOUNT OF CODE! file that makes the calls: #include &lt;d3d10.h&gt; #include &lt;d3dx10.h&gt; #include "direct3D.h" #include "game.h

  • ;&amp; ballPrev.coords[2] &lt; block[i].coords[0] &amp;&amp; (((ball.coords[1] &lt; block[i].coords[1]) || (ball.coords[3] &lt; ball.coords[1])) || ((ball.coords[1] &lt; block[i].coords[3]) || ball.coords[3] &lt; block[i].coords[3]))) { left = 1; } if (ballPrev.coords[0] &gt; block[i].coords[2] &amp;&amp; ballPrev.coords[2] &gt; block[i].coords[2] &amp;&amp...; i++) { if (de2dCheckCollision(ball,block[i],0.0f,0.0f)==true) { de2dObj ballPrev; ballPrev.coords[0] = ball.coords[0]; ballPrev.coords[1] = ball.coords[1

Data information