i wrote some code which only works with objects with a heading of 0 degrees right now because its not finished but when i run it it never seems to catch the collision and break the loop. I need some help, thanks in advance.

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
double x;
double y;
int Heading;
double Distance;
    
double xDisplacement;
double yDisplacement;

double objectX;
double objectY;

bool CalculationDone = false;
bool LoopBreak = false;
    
cout << "Enter object's x co-ordinate: ";
cin >> x;
cout << "\n";
cout << "Enter object's y co-ordinate: ";
cin >> y;
cout << "\n";
cout << "Enter object's heading (degrees): ";
cin >> Heading;
cout << "\n";
cout << "Enter object's distance travelled: ";
cin >> Distance;
cout << "\n";
cout << "Enter 2nd Object's x co-ordinate: ";
cin >> objectX;
cout << "\n";
cout << "Enter 2nd Object's y co-ordinate: ";
cin >> objectY;

if (Heading == 0)
{
xDisplacement = 0;
yDisplacement = 0.01;
	
while (LoopBreak == false)
{
    if (y < Distance)
    {
    x += xDisplacement;
    y += yDisplacement;
    
    cout << "\n";
    cout << "(";
    cout << x;
    cout << ",";
    cout << " ";
    cout << y;
    cout << ")";
    cout << "\n";
    }
    
    if (x == objectX && y == objectY)
    {
    LoopBreak = true;
    cout << "Collision Detected at (";
    cout << x;
    cout << ",";
    cout << " ";
    cout << y;
    cout << ")";
    cout << "\n";
    }
    if (y >= Distance)
    {
    LoopBreak = true;
    }
}
}
system("PAUSE");
}

Recommended Answers

All 3 Replies

x += 0 ???
If second coord x = 2,
never touch x2..

that didnt help.

that didnt help.

His point is that x never changes.

xDisplacement = 0;
x += xDisplacement;

is the same as

x += 0;

which does nothing. Second problem. You are comparing two doubles using the == operator. Generally that is a bad idea. You should probably be calculating the distance between the two objects and checking whether they are within a certain tolerance. If so, it's a collision. A good tolerance would be the sum of the radii.

bool Collision (double x1, double y1, double x2, double y2, double rad1, double rad2)
{
     // calculate distance.
     if (distance  < (rad1 + rad2))
         return true;
     else
         return false;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.