Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: vector.h
- Copyright: none
- Author: Dev
- Date: 19-05-10 19:23
- Description:
- */
- const float dTime = 0.15;
- struct vec {
- float x;
- float y;
- float z;
- float len(){return sqrt(pow(x,2) + pow(y,2));};
- vec normalize(){
- float length = len();
- return vec(x / length, y / length);
- };
- vec perp(){return vec(y, -x);}
- vec(){x = 0.0f; y = 0.0f; z = 0.0f;}
- vec(float x, float y) : x(x), y(y){}
- vec(float x, float y,float z) : x(x), y(y), z(z){}
- vec operator- ()const{return vec(-x, -y); }
- vec& operator+= (const vec& v){x += v.x; y += v.y; return *this; }
- vec& operator-= (const vec& v){x -= v.x; y -= v.y; return *this; }
- vec& operator*= (const vec& v){x *= v.x; y *= v.y; return *this; }
- vec& operator*= (float a){x *= a; y *= a; return *this; }
- };
- inline vec operator+ (const vec& v, const vec& n) {return vec(v.x + n.x, v.y + n.y);};
- inline vec operator- (const vec& v, const vec& n) {return vec(v.x - n.x, v.y - n.y);};
- inline vec operator* (const vec& v, const vec& n) {return vec(v.x * n.x, v.y * n.y);};
- inline vec operator* (float v, const vec& n) {return vec(v * n.x, v * n.y);};
- inline vec operator* (const vec& v, float n) {return vec(v.x * n, v.y * n);};
- inline vec operator/ (const vec& v, float n) {return vec(v.x / n, v.y / n);};
- float dot(vec v, vec n){return ((v.x * n.x) + (v.y * n.y));}
- vec abs(vec v){return vec( fabs(v.x) , fabs(v.y) ); };
- void normalize(vec& v){v = v.normalize();}
- void perp(vec& v){v = v.perp();}
- template <typename T>
- T rand(T high,T low){
- return (rand() / ((T)(RAND_MAX) + 1.0))* (high - low) + low;
- }
- struct ball {
- int r;
- int color;
- int id;
- float coefRest;
- float mass;
- vec pos;
- vec v;
- ball(){ coefRest = 1.0f;}
- void draw();
- void update();
- };
- struct segment {
- vec a;
- vec b;
- vec *ptra;
- vec *ptrb;
- vec n;
- float coefRest;
- segment(){ptra = NULL; ptrb = NULL;coefRest = 0.98f;}
- segment(vec a, vec b) : a(a), b(b){ptra = NULL; ptrb = NULL;update();coefRest = 0.98f;}
- segment(vec a, vec b, float coefRest) : a(a), b(b), coefRest(coefRest){ptra = NULL; ptrb = NULL;update();}
- segment(vec* a, vec* b) : ptra(a), ptrb(b){update();coefRest = 0.98f;}
- void update();
- };
- void segment::update(){
- n = (a - b).perp().normalize();
- if(ptra != NULL) a = *ptra;
- if(ptrb != NULL) b = *ptrb;
- }
- vec closestPointToSegment(ball ball, segment segment){
- vec l = segment.b - segment.a;
- vec w = ball.pos - segment.a;
- float t = dot(w,l) / dot(l,l);
- if(t > 1) t = 1;
- if(t < 0) t = 0;
- return segment.a + l * t;
- }
- bool ballSegmentCollision(ball &ball, segment segment){
- vec cPoint = closestPointToSegment(ball, segment);
- vec dist = cPoint - ball.pos;
- if( dot(dist,dist) <= ball.r * ball.r){
- vec V = ball.v;
- vec n = dist.normalize();
- vec N = dot(-V,n) * n;
- vec L = V + N;
- ball.v = (-V + 2 * L) * segment.coefRest;
- ball.pos = cPoint - ball.r * n;
- return true;
- }
- return false;
- }
- bool intersectRayBall(ball &a, ball &b){
- float t;
- vec cDiff = b.pos - a.pos;
- float r = a.r + b.r;
- float c = dot(cDiff,cDiff) - r * r;
- if( c < 0 ) return true;
- vec vDiff = b.v - a.v;
- float x = dot( vDiff, vDiff );
- if( x < 0.1f ) return false;
- float v = dot(vDiff, cDiff );
- if( v >= 0 ) return false;
- float z = v * v - x * c;
- if( z < 0 ) return false;
- t = ( -v - sqrt( z ) ) / x;
- if(t > dTime) return false;
- a.pos += a.v * t;
- b.pos += b.v * t;
- return true;
- }
- bool ballBallCollision(ball &a ,ball &b){
- intersectRayBall(a, b); //yeah do sth with it
- vec s = a.pos - b.pos;
- if(s.len() <= a.r + b.r ){
- float coefRest = a.coefRest * b.coefRest;
- vec n = s.normalize();
- vec x1 = dot(a.v,n)*n;
- vec x2 = dot(b.v,n)*n;
- vec p1 = a.v - x1;
- vec p2 = b.v - x2;
- a.v = (x2 + p1) * coefRest;
- b.v = (x1 + p2) * coefRest;
- float penteration = (a.r + b.r - s.len()) / 2;
- a.pos += s.normalize() * penteration;
- b.pos -= s.normalize() * penteration;
- return true;
- }
- return false;
- }
Add Comment
Please, Sign In to add comment