/* find if any of the current balls intersect with each other in the * current timestep. returns the index of the 2 intersecting balls, * the point and time of intersection */ int find_collision(TVector& point, double& timePoint, double time2, int& ball1, int& ball2) { TVector relativeV; TRay rays; double Mytime = 0.0, Add = time2/150.0, timedummy = 10000, timedummy2 = -1; TVector posi; /* Test all balls against each other in 150 small steps */ for (int i = 0; i < ball_count - 1; i++){ for (int j = i+1; j < ball_count; j++){ // Find Distance relativeV = ball_vel[i]-ball_vel[j]; rays = TRay(old_pos[i],TVector::unit(relativeV)); Mytime = 0.0; // If distance detween centers greater than 2*radius an // intersection occurred loop to find the exact intersection point if ( (rays.dist(old_pos[j])) > ball_r * 2 ) continue; while (Mytime < time2) { Mytime += Add; posi = old_pos[i] + relativeV*Mytime; if (posi.dist(old_pos[j]) <= ball_r * 2 ) { point = posi; if (timedummy > (Mytime - Add)) timedummy = Mytime-Add; ball1 = i; ball2 = j; break; } } } } if (timedummy!=10000) { timePoint=timedummy; return 1; } return 0; }
int FindBallCol(TVector& point, double& TimePoint, double Time2, int& BallNr1, int& BallNr2) { TVector RelativeV; TRay rays; double MyTime=0.0, Add=Time2/150.0, Timedummy=10000, Timedummy2=-1; TVector posi; //Test all balls against eachother in 150 small steps for (int i=0;i<NrOfBalls-1;i++) { for (int j=i+1;j<NrOfBalls;j++) { RelativeV=ArrayVel[i]-ArrayVel[j]; rays=TRay(OldPos[i],TVector::unit(RelativeV)); MyTime=0.0; if ( (rays.dist(OldPos[j])) > 40) continue; while (MyTime<Time2) { MyTime+=Add; posi=OldPos[i]+RelativeV*MyTime; if (posi.dist(OldPos[j])<=40) { point=posi; if (Timedummy>(MyTime-Add)) Timedummy=MyTime-Add; BallNr1=i; BallNr2=j; break; } } } } if (Timedummy!=10000) { TimePoint=Timedummy; return 1; } return 0; }
bool MyGameState::FindBallCol(TVector& point, double& TimePoint, double Time2, int& BallNr1, int& BallNr2){ TVector RelativeV; TRay rays; double MyTime=0.0, Add=Time2/150.0, Timedummy=10000, Timedummy2=-1; TVector posi; //Test all balls against eachother in 150 small steps for (int i = 0; i <= 15-1; i++){ for (int j = i + 1; j<= 15; j++){ RelativeV = movement[i] - movement[j]; rays = TRay(oldPos[i],TVector::unit(RelativeV)); MyTime=0.0; if ( rays.dist(oldPos[j]) > 10){ continue; } while (MyTime < Time2){ MyTime += Add; posi = oldPos[i] + RelativeV * MyTime; if (posi.dist(oldPos[j]) <= 3) { point = posi; if (Timedummy > (MyTime-Add)) { Timedummy = MyTime - Add; } BallNr1=i; BallNr2=j; break; } } } } if (Timedummy!=10000) { TimePoint=Timedummy; return true; } return false; }