c++ - Ball passing through paddle from certain angles -


i'm creating pong clone school c++ , sfml 2.1 , i'm having little problem when ball hits left paddle @ sharp angles (it goes through).

the right paddle works fine @ angles, , far can remember they're using same code.

this code i'm using collision:

    (auto& : collisionpaddles)     {         if (this->ballshape.getglobalbounds().intersects(it->getglobalpaddlebounds()))         {             float deltadistance = (this->y + this->radius) - (it->y + it->height / 2);             bool fromleft = true;              if ((ballangle < (3*mymath::my_pi/2) && ballangle > mymath::my_pi/2))             {                 fromleft = false;             }             else             {                 fromleft = true;             }              ballangle = static_cast<float>(deltadistance * (mymath::my_pi/180));              if (fromleft)             {                 ballangle = static_cast<float>(mymath::my_pi - ballangle);             }             moveball(2);         }     } 

that's not way pong should implemented. when model of physics uses deltas, lot of artefacts. 1 of prominent objects passing through each other. in such simple cases deltas should used animation only.

here's way solve it. when ball gets initial speed , vector of movement, calculate whole path until hits player's baseline.

  1. calculate initial ray current position of ball, directed same way moves.
  2. intersect ray segments consistute borders of field.
  3. calculate time needed reach intersection point.
  4. calculate new ray intersection point towards new movement vector.
  5. repeat steps 2-4 until hit player's base line. sum times.

now have relative time of hit baseline , place happen. on every frame should

  1. check if collision time has been between previous frame , current one. if was,
  2. calculate expected position of paddle @ moment.
  3. if segment of paddle contains intersection point, reflected. calculate new path described before.

this way you'll true game mechanics not limited of aspects of game development, i.e. fps, sudden lags etc.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -