1
\$\begingroup\$

I wrote a for loop to check every instance of the class Wall, only the the last instance in the vector properly works

hybrid.h

 void reactTo(Player* other)
 {
     if (react)
     {
         if ((other->back<front && other->front > front) && (other->y > foot && other->y < head))
         {
             other->goAhead[0] = false;
         }
         else
         {
             other->goAhead[0] = true;

             if ((other->front >= back) && other->back < back && other->y > foot && other->y < head)
             {
                 other->goAhead[1] = false;
             }
             else
             {
                 other->goAhead[1] = true;
             }
         }
     }
     if (other->foot <= head && other->head >= head && other->x > back && other->x < front)
     {
         other->goAhead[2] = false;
     }
     else
     {
         other->goAhead[2] = true;
         if ((other->head >= foot) && other->foot < foot && foot && other->x > back && other->x < front)
         {
             other->goAhead[3] = false;
         }
         else
         {
             other->goAhead[3] = true;
         }
     }
     if (other->front > back && other->back <= front && other->head < head && other->foot > foot)
     {
         R = 0;
     }
     else
     {
         R = 1;
     }
 }

source.cpp

#include"hybrid/hybrid.h"

int main()
{
   setWindowSize(1500, 600);

   makeSequenceWall();//push back 5 Wall constructor

   Player p(300, 300,30,30,0.5,0,1,0);
   //------------
   slWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "HYBRID", false);
   while (!slShouldClose())
   {
               //----------
       p.move();
       p.render();//render the player

       
       for (int i = 0; i != 5; i++)//check the collision
       {
           wallSeq.at(i).reactTo(&p);
       }

       
       for (itr = wallSeq.begin(); itr != wallSeq.end();itr++)//render the wall
       {
           itr->render();
       }
       
       slRender();
   
   }
   slClose();
   return 0;
}

```
\$\endgroup\$
0
1
\$\begingroup\$

only the the last instance in the vector properly works

Yes, it looks like void reactTo(Player* other) only really takes into consideration stuff that happens with "this wall": it will overwrite in the player whatever happened with the "previous wall" that was checked.

You probably need to "reset" the data in your player at the beginning of each frame, then in your wall-check code, check also what happened with the previous wall checks (e.g. check if other->goAhead[X] has already been set to false, and if it has, don't set it to true). Or maybe set everything to true (goAhead[X] = true;) at the beginning of each frame, and only toggle the values to false if they must (i.e. don't set them to true in the collision-checking function).

\$\endgroup\$
2
  • \$\begingroup\$ I followed what you said, and so I created a array called listToGetBlocked, if it is true will make goAhead[X] true only if currentCollided is equal to current wall's Id, that was the solution :D \$\endgroup\$ Apr 19 at 2:04
  • \$\begingroup\$ I meant "make goAhead[X] false' \$\endgroup\$ Apr 19 at 2:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.