Advertisement
much101141

collider

Apr 26th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #ifndef COLLIDER_H
  2. #define COLLIDER_H
  3. #include <SFML/Graphics.hpp>
  4. #include "Player.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. class collider
  9. {
  10. public:
  11. void Move(float dx,float dy){player.move(dx,dy);}
  12. collider(sf::RectangleShape &player);
  13. bool checkcollider(collider player,float push);
  14. bool checkcolliderforplant(collider player);
  15. sf::Vector2f Getposition(){return player.getPosition();}
  16. sf::Vector2f Gethalfsize(){return player.getSize()/2.0f;}
  17. virtual ~collider();
  18.  
  19.  
  20. protected:
  21.  
  22. private:sf::RectangleShape &player;
  23. int full;
  24. int type;
  25. };
  26. bool collider::checkcollider(collider player,float push)
  27. {
  28. sf::Vector2f otherposition=player.Getposition();
  29. sf::Vector2f otherhalfsize=player.Gethalfsize();
  30. sf::Vector2f currentposition=Getposition();
  31. sf::Vector2f currenthalfsize=Gethalfsize();
  32. float checkx=otherposition.x-currentposition.x;
  33. float checky=otherposition.y-currentposition.y;
  34. float intersecx,intersecy;
  35. if(checkx<0)
  36. {
  37. intersecx= -(checkx)-(otherhalfsize.x+currenthalfsize.x);
  38. }
  39. if(checkx>=0) intersecx= (checkx)-(otherhalfsize.x+currenthalfsize.x);
  40. if(checky<0)
  41. {
  42. intersecy= -(checky)-(otherhalfsize.y+currenthalfsize.y);
  43. }
  44. if(checky>=0) intersecy= (checky)-(otherhalfsize.y+currenthalfsize.y);
  45.  
  46. if(intersecx<0.0f&&intersecy<0.0f)
  47. {
  48.  
  49. if(intersecx>intersecy)
  50. {
  51. if(checkx>0.0f)
  52. {
  53. Move(intersecx*(1.0f-push),0.0f);
  54. player.Move(-(intersecx*push),0.0f);
  55. }
  56. else
  57. {
  58. Move(-intersecx*(1.0f-push),0.0f);
  59. player.Move(intersecx*push,0.0f);
  60. }
  61. }
  62. else
  63. {
  64. if(checky>0.0f)
  65. {
  66. Move(0.0f,intersecy*(1.0f-push));
  67. player.Move(0.0f,-(intersecy*push));
  68. }
  69. else
  70. {
  71. Move(0.0f,-intersecy*(1.0f-push));
  72. player.Move(0.0f,intersecy*push);
  73. }
  74. }
  75. return true;
  76.  
  77.  
  78.  
  79. }
  80. return false;
  81. }
  82. collider::collider(sf::RectangleShape &player):
  83. player(player)
  84. {
  85.  
  86. }
  87. bool collider::checkcolliderforplant(collider player)
  88. {
  89. sf::Vector2f otherposition=player.Getposition();
  90. sf::Vector2f otherhalfsize=player.Gethalfsize();
  91. sf::Vector2f currentposition=Getposition();
  92. sf::Vector2f currenthalfsize=Gethalfsize();
  93. float checkx=otherposition.x-currentposition.x;
  94. float checky=otherposition.y-currentposition.y;
  95. float intersecx,intersecy;
  96. if(checkx<0)
  97. {
  98. intersecx= -(checkx)-(otherhalfsize.x+currenthalfsize.x);
  99. }
  100. if(checkx>=0) intersecx= (checkx)-(otherhalfsize.x+currenthalfsize.x);
  101. if(checky<0)
  102. {
  103. intersecy= -(checky)-(otherhalfsize.y+currenthalfsize.y);
  104. }
  105. if(checky>=0) intersecy= (checky)-(otherhalfsize.y+currenthalfsize.y);
  106. if(intersecx<0.0f&&intersecy<0.0f)
  107. {
  108. return true;
  109.  
  110. //cout<<"eiei"<<endl;
  111. }
  112. else return false;
  113.  
  114. }
  115. collider::~collider()
  116. {
  117. //dtor
  118. }
  119. #endif // COLLIDER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement