Advertisement
Guest User

collision struggles

a guest
Jan 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. Ball::Ball(const Vec2 & pos_in, const Vec2 & vel_in)
  2. {
  3.     pos = pos_in;
  4.     vel = vel_in;
  5. }
  6.  
  7. void Ball::Draw(Graphics& gfx) const
  8. {
  9.     SpriteCodex::DrawBall(pos,gfx);
  10. }
  11.  
  12. void Ball::Update(float dt)
  13. {
  14.     pos += vel * dt;
  15. }
  16.  
  17. bool Ball::Colliding(const RectF & walls)
  18. {
  19.     bool collided = false;
  20.     const RectF rect = GetRect();
  21.     if (rect.left  < walls.left)
  22.     {
  23.         pos.x += walls.left - rect.left;
  24.         ReboundX();
  25.         collided = true;
  26.     }
  27.  
  28.     else if (rect.right > walls.right)
  29.     {
  30.         pos.x -= rect.right - walls.right;
  31.         ReboundX();
  32.         collided = true;
  33.     }
  34.    
  35.     if (rect.top < walls.top)
  36.     {
  37.         pos.y += walls.top - rect.top;
  38.         ReboundY();
  39.         collided = true;
  40.     }
  41.  
  42.     else if (rect.bottom > walls.bottom)
  43.     {
  44.         pos.y -= rect.bottom - walls.bottom;
  45.         ReboundY();
  46.         collided = true;
  47.     }
  48.     return collided;
  49. }
  50.  
  51. void Ball::ReboundX()
  52. {
  53.     vel.x = -vel.x;
  54. }
  55.  
  56. void Ball::ReboundY()
  57. {
  58.     vel.y = -vel.y;
  59. }
  60.  
  61. RectF Ball::GetRect() const
  62. {
  63.     return RectF::FromCenter(pos, radius, radius);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement