#include
#include
using namespace std;
int main()
{
float shipx=0, shipy=0, shipr=1, shipi, shipj; //ship properties
float moonx, moony, moonr=10; //moon properties
float ppdi, ppdj, theta/*, lambda*/; //calculation variables - simultaneous equation setup
float dist, coll; //calculation variables - collision checking
int restart=1, vcheck=1;
while (restart == 1)
{
cout << "2D circular bounding box collision detection demo\n";
cout << "Ship starts at origin with radius 1\n";
while (vcheck == 1)
{
cout << "Enter ship velocity in i direction:\n";
cin >> shipi;
cout << "\nEnter ship velocity in j direction:\n";
cin >> shipj;
if (shipi == 0 && shipj == 0)
{
cout << "\nShip must have nonzero overall velocity\n";
vcheck = 1;
}
else
{vcheck = 0;}
}
vcheck = 1;
cout << "\nMoon has radius 10\nEnter moon x coord:\n";
cin >> moonx;
cout << "\nEnter moon y coord:\n";
cin >> moony;
cout << "\nSimulating...\n\n";
ppdi = shipj * (0-1);
ppdj = shipi; //finds vector perpendicular to ship's velocity
cout << "Ship position = " << shipx << "i + " << shipy << "j + lambda ( " << shipi << "i + " << shipj << "j )\n";
cout << "Perpendicular vector from moon = "<< moonx << "i + " << moony << "j + theta ( " << ppdi << "i + " << ppdj << "j )\n\n";
//find theta to intersection point of vectors from moon
theta = (((moonx - shipx) * shipj) - ((moony - shipy) * shipi)) / ((ppdj * shipi) - (ppdi * shipj));
cout << "theta = " << theta << "\n";
//find lambda (lambda equals time to closest point, incidentally - uncomment this section and the lambda variable to get time to closest point)
/*lambda = ( difx + ( ppdi * theta ) ) / shipi;
cout << "lambda = " << lambda << "\n";*/
//find distance from closest point to centre of moon
dist = sqrt( pow( ( theta * ppdi ) , 2 ) + pow( ( theta * ppdj ) , 2 ) );
cout << "Distance between centre points is " << dist << "\n";
//check for collision
coll = dist - moonr - shipr;
if (coll <= 0)
{cout << "Collision! Boundary distance is " << coll << "\n";}
else
{cout << "No collision! Boundary distance is " << coll << "\n";}
//reset or quit
cout << "Enter 1 to restart or 0 to quit\n";
cin >> restart;
cout << "\n\n";
}
return 0;
}