the ball goes through everything

Here's the place to get help and discuss features. The focus is on the C++ version, but generic questions are welcome.
blendamend
Posts: 1
Joined: Mon May 22, 2017 11:36 am

the ball goes through everything

Postby blendamend » Mon May 22, 2017 11:52 am

hi guys!

I have some problems with my project, that's why I decided share it with you.
So, I have a ball, and this object is dynamic body. Also, I have some static boards, that I can move with my mouse and "really" static floor.
The trouble is that ball goes through every static bodies.
May be the reason of my problem is that I "redraw" static boards, when I change there positions with mouse. But I don't recreate and move a floor.

I hope, that you can help with my problems

blendamend.

p.s pieces of my code

Code: Select all

void load(int Lvl)
   {
      if (Lvl == 1 && Load)
      {
         x[0] = 550.f; x[1] = 50.f; x[2] = 180.f; x[3] = 560.f;
         y[0] = 275.f; y[1] = 550.f; y[2] = 550.f; y[3] = 200.f;
         for (int i = 0; i < 3; i++)
         {
            shape[i].SetAsBox((board[i].getSize().x/2)/30,(board[i].getSize().y/2)/30);
            boarddef[i].type = b2_staticBody;
            boarddef[i].position.Set(board[i].getPosition().x / 30.f, board[i].getPosition().y / 30.f);
            bodyBoard[i] = World.CreateBody(&boarddef[i]);
            bodyBoard[i]->CreateFixture(&shape[i],2);
            bodyBoard[i]->SetUserData("board");
            board[i].setPosition(x[i], y[i]);
            board[i].setOrigin(50, 15);
         }

         Ball.setPosition(x[3], y[3]);
         Load = false;
      }
   }



   void update()
   {
      window.clear();
      window.draw(BG);
      window.draw(Ball);
      if (START)window.draw(stop);
      if (!START) window.draw(start);
      for (int i = 1; i < 3; i++)
      {
         shape[i].SetAsBox((board[i].getSize().x / 2) / 30, (board[i].getSize().y / 2) / 30);
         boarddef[i].position.Set(board[i].getPosition().x / 30.f, board[i].getPosition().y / 30.f);
         bodyBoard[i] = World.CreateBody(&boarddef[i]);
         bodyBoard[i]->CreateFixture(&shape[i], 1);
         window.draw(board[i]);
      }
      window.draw(board[0]);
      window.draw(flooro);
   }


and piece of main file

Code: Select all

if (player.START)
         {      
            
            World.Step(1 / 60.f, 8, 3);
                  for (b2Body* it = World.GetBodyList(); it != 0; it = it->GetNext())
                  {
                     if (it->GetUserData() == "player")
                     {
                        b2Vec2 pos = it->GetPosition();
                        player.x[3] = pos.x*30.f;
                        player.y[3] = pos.y*30.f;
                        player.Ball.setPosition(player.x[3], player.y[3]);
                        window.draw(player.Ball);
                     }

                  }
               }

         player.update();

         window.display();

CrowbarBarian
Posts: 8
Joined: Sun Jun 25, 2017 9:05 pm

Re: the ball goes through everything

Postby CrowbarBarian » Mon Jul 03, 2017 8:21 pm

Try changing

Code: Select all

boarddef[i].type = b2_staticBody;
to

Code: Select all

boarddef[i].type = b2_kinematicBody;
and see if that works at all.


Return to “General Discussion”



Who is online

Users browsing this forum: No registered users and 2 guests