i have a function thats part of a class which detects a collision between 2 entities ( another class i made ). The weird part is, when i was testing to see how far the program was getting because it wasnt working i put
cout << "A" << endl;
to see if it was getting to that, and all of a sudden the function works correctly. Then i took away the
cout << "A" << endl;
And it didnt work correctly! ARGH! There is no way that a cout line could influence the return value of a function.
the function:
bool tri2d :: detectTriCollision ( point2d * p1, point2d * p2, tri2d * t )
{
int minX1, maxX1, minY1, maxY1;
int minX2, maxX2, minY2, maxY2;
bool doLinesIntersect;
// Get the bounding boxes
findMinMaxPts( minX1, maxX1, minY1, maxY1, p1 );
t->findMinMaxPts( minX2, maxX2, minY2, maxY2, p2 );
// Check the bounding boxes for intersection
// This makes sure if the triangles are far away,
// Dont even bother testing line segments
// Test if x intersection exists
if( ! ( ( minX1 >= minX2 && minX1 <= maxX2 ) || ( maxX1 >= minX2 && maxX1 <= maxX2 ) ) )
{
return false;
}
// Test if y intersection exists
if( ! ( ( minY1 >= minY2 && minY1 <= maxY2 ) || ( maxY1 >= minY2 && maxY1 <= maxY2 ) ) )
{
return false;
}
// This is the cout line that makes the difference
// WTH?!
cout << "A" << endl;
// Just test if the line segments intersect....
// Not important
// Test ab line against triangle t
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ b ].x, ( int ) p1[ b ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ b ].x, ( int ) p1[ b ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ b ].x, ( int ) p1[ b ].y,
( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
// Test ac line against triangle t
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
// Test bc line against triangle t
doLinesIntersect = linesIntersect( ( int ) p1[ b ].x, ( int ) p1[ b ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ b ].x, ( int ) p1[ b ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ b ].x, ( int ) p1[ b ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
return false;
}
and i have to entities in main(), me, and you like this:
entityType * me;
entityType * you;
// detectEntCollision calls detectTriCollision
// after it checks a few things...
if( me->detectEntCollision( you ) )
cout << "Yeah" << endl;
If this isnt enough information to find the problem, ill post more of the program.
Synopsis:
I add a cout line in detectTriCollision() it works properly, i take the cout line away, it stops working
Later
-Steve
cout << "A" << endl;
to see if it was getting to that, and all of a sudden the function works correctly. Then i took away the
cout << "A" << endl;
And it didnt work correctly! ARGH! There is no way that a cout line could influence the return value of a function.
the function:
bool tri2d :: detectTriCollision ( point2d * p1, point2d * p2, tri2d * t )
{
int minX1, maxX1, minY1, maxY1;
int minX2, maxX2, minY2, maxY2;
bool doLinesIntersect;
// Get the bounding boxes
findMinMaxPts( minX1, maxX1, minY1, maxY1, p1 );
t->findMinMaxPts( minX2, maxX2, minY2, maxY2, p2 );
// Check the bounding boxes for intersection
// This makes sure if the triangles are far away,
// Dont even bother testing line segments
// Test if x intersection exists
if( ! ( ( minX1 >= minX2 && minX1 <= maxX2 ) || ( maxX1 >= minX2 && maxX1 <= maxX2 ) ) )
{
return false;
}
// Test if y intersection exists
if( ! ( ( minY1 >= minY2 && minY1 <= maxY2 ) || ( maxY1 >= minY2 && maxY1 <= maxY2 ) ) )
{
return false;
}
// This is the cout line that makes the difference
// WTH?!
cout << "A" << endl;
// Just test if the line segments intersect....
// Not important
// Test ab line against triangle t
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ b ].x, ( int ) p1[ b ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ b ].x, ( int ) p1[ b ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ b ].x, ( int ) p1[ b ].y,
( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
// Test ac line against triangle t
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ a ].x, ( int ) p1[ a ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
// Test bc line against triangle t
doLinesIntersect = linesIntersect( ( int ) p1[ b ].x, ( int ) p1[ b ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ b ].x, ( int ) p1[ b ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->a ].x, ( int ) p2[ t->a ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
doLinesIntersect = linesIntersect( ( int ) p1[ b ].x, ( int ) p1[ b ].y, ( int ) p1[ c ].x, ( int ) p1[ c ].y,
( int ) p2[ t->b ].x, ( int ) p2[ t->b ].y, ( int ) p2[ t->c ].x, ( int ) p2[ t->c ].y );
if( doLinesIntersect )
return true;
return false;
}
and i have to entities in main(), me, and you like this:
entityType * me;
entityType * you;
// detectEntCollision calls detectTriCollision
// after it checks a few things...
if( me->detectEntCollision( you ) )
cout << "Yeah" << endl;
If this isnt enough information to find the problem, ill post more of the program.
Synopsis:
I add a cout line in detectTriCollision() it works properly, i take the cout line away, it stops working
Later
-Steve