Guest User

Untitled

a guest
May 27th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. /*
  2. Name: vector.h
  3. Copyright: none
  4. Author: Dev
  5. Date: 19-05-10 19:23
  6. Description:
  7. */
  8.  
  9. const float dTime = 0.15;
  10.  
  11. struct vec {
  12. float x;
  13. float y;
  14. float z;
  15.  
  16. float len(){return sqrt(pow(x,2) + pow(y,2));};
  17.  
  18. vec normalize(){
  19. float length = len();
  20. return vec(x / length, y / length);
  21. };
  22.  
  23. vec perp(){return vec(y, -x);}
  24.  
  25. vec(){x = 0.0f; y = 0.0f; z = 0.0f;}
  26. vec(float x, float y) : x(x), y(y){}
  27. vec(float x, float y,float z) : x(x), y(y), z(z){}
  28.  
  29. vec operator- ()const{return vec(-x, -y); }
  30. vec& operator+= (const vec& v){x += v.x; y += v.y; return *this; }
  31. vec& operator-= (const vec& v){x -= v.x; y -= v.y; return *this; }
  32. vec& operator*= (const vec& v){x *= v.x; y *= v.y; return *this; }
  33. vec& operator*= (float a){x *= a; y *= a; return *this; }
  34. };
  35.  
  36. inline vec operator+ (const vec& v, const vec& n) {return vec(v.x + n.x, v.y + n.y);};
  37. inline vec operator- (const vec& v, const vec& n) {return vec(v.x - n.x, v.y - n.y);};
  38. inline vec operator* (const vec& v, const vec& n) {return vec(v.x * n.x, v.y * n.y);};
  39. inline vec operator* (float v, const vec& n) {return vec(v * n.x, v * n.y);};
  40. inline vec operator* (const vec& v, float n) {return vec(v.x * n, v.y * n);};
  41. inline vec operator/ (const vec& v, float n) {return vec(v.x / n, v.y / n);};
  42.  
  43. float dot(vec v, vec n){return ((v.x * n.x) + (v.y * n.y));}
  44. vec abs(vec v){return vec( fabs(v.x) , fabs(v.y) ); };
  45.  
  46. void normalize(vec& v){v = v.normalize();}
  47. void perp(vec& v){v = v.perp();}
  48.  
  49. template <typename T>
  50. T rand(T high,T low){
  51. return (rand() / ((T)(RAND_MAX) + 1.0))* (high - low) + low;
  52. }
  53.  
  54. struct ball {
  55. int r;
  56. int color;
  57. int id;
  58. float coefRest;
  59. float mass;
  60. vec pos;
  61. vec v;
  62.  
  63. ball(){ coefRest = 1.0f;}
  64. void draw();
  65. void update();
  66. };
  67.  
  68. struct segment {
  69. vec a;
  70. vec b;
  71. vec *ptra;
  72. vec *ptrb;
  73. vec n;
  74. float coefRest;
  75.  
  76. segment(){ptra = NULL; ptrb = NULL;coefRest = 0.98f;}
  77. segment(vec a, vec b) : a(a), b(b){ptra = NULL; ptrb = NULL;update();coefRest = 0.98f;}
  78. segment(vec a, vec b, float coefRest) : a(a), b(b), coefRest(coefRest){ptra = NULL; ptrb = NULL;update();}
  79. segment(vec* a, vec* b) : ptra(a), ptrb(b){update();coefRest = 0.98f;}
  80.  
  81. void update();
  82. };
  83.  
  84. void segment::update(){
  85. n = (a - b).perp().normalize();
  86. if(ptra != NULL) a = *ptra;
  87. if(ptrb != NULL) b = *ptrb;
  88. }
  89.  
  90. vec closestPointToSegment(ball ball, segment segment){
  91. vec l = segment.b - segment.a;
  92. vec w = ball.pos - segment.a;
  93. float t = dot(w,l) / dot(l,l);
  94. if(t > 1) t = 1;
  95. if(t < 0) t = 0;
  96. return segment.a + l * t;
  97. }
  98.  
  99. bool ballSegmentCollision(ball &ball, segment segment){
  100. vec cPoint = closestPointToSegment(ball, segment);
  101. vec dist = cPoint - ball.pos;
  102. if( dot(dist,dist) <= ball.r * ball.r){
  103. vec V = ball.v;
  104. vec n = dist.normalize();
  105. vec N = dot(-V,n) * n;
  106. vec L = V + N;
  107. ball.v = (-V + 2 * L) * segment.coefRest;
  108. ball.pos = cPoint - ball.r * n;
  109. return true;
  110. }
  111. return false;
  112. }
  113.  
  114. bool intersectRayBall(ball &a, ball &b){
  115. float t;
  116. vec cDiff = b.pos - a.pos;
  117. float r = a.r + b.r;
  118. float c = dot(cDiff,cDiff) - r * r;
  119. if( c < 0 ) return true;
  120. vec vDiff = b.v - a.v;
  121.  
  122. float x = dot( vDiff, vDiff );
  123. if( x < 0.1f ) return false;
  124.  
  125. float v = dot(vDiff, cDiff );
  126. if( v >= 0 ) return false;
  127.  
  128. float z = v * v - x * c;
  129. if( z < 0 ) return false;
  130.  
  131. t = ( -v - sqrt( z ) ) / x;
  132. if(t > dTime) return false;
  133. a.pos += a.v * t;
  134. b.pos += b.v * t;
  135. return true;
  136. }
  137.  
  138. bool ballBallCollision(ball &a ,ball &b){
  139. intersectRayBall(a, b); //yeah do sth with it
  140. vec s = a.pos - b.pos;
  141.  
  142. if(s.len() <= a.r + b.r ){
  143.  
  144. float coefRest = a.coefRest * b.coefRest;
  145. vec n = s.normalize();
  146. vec x1 = dot(a.v,n)*n;
  147. vec x2 = dot(b.v,n)*n;
  148. vec p1 = a.v - x1;
  149. vec p2 = b.v - x2;
  150.  
  151. a.v = (x2 + p1) * coefRest;
  152. b.v = (x1 + p2) * coefRest;
  153.  
  154. float penteration = (a.r + b.r - s.len()) / 2;
  155. a.pos += s.normalize() * penteration;
  156. b.pos -= s.normalize() * penteration;
  157. return true;
  158. }
  159. return false;
  160. }
Add Comment
Please, Sign In to add comment