Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

this is making NO sense. 1

Status
Not open for further replies.

xS73v3x

Programmer
Jan 31, 2003
11
US
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 << &quot;A&quot; << endl;
to see if it was getting to that, and all of a sudden the function works correctly. Then i took away the
cout << &quot;A&quot; << 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 << &quot;A&quot; << 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 << &quot;Yeah&quot; << 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
 
When you say it stops working, what do you mean?

eg, program aborts
memory error
invalid results
 
well it compiles and all, but the value the function returns is different.

when it works, the value returned should be true for the values i set on the entity.

when it doesnt work, the value returned is false.

So basically the cout statement makes it return a different value :\
 
Have you stepped through the code and watched the variables that affect the output?
 
I feel that ur problem is u haven't initialized the variable properly.
so it is causing u this problem.

initialise the variable and try !
It will work properly

this could be a case where cout makes a difference.
 
Uninitialized variables and pointers that point to nowhere are often influenced by adding/taking away a random line of code somewhere else, often a line that hasn't even been executed yet.
The reason is that the extra line is extra padding in the code: it takes up memory and moves everything along a bit. That means that the uninitialized variable is somewhere else, often having a value left over from whatever your computer was last doing, or the pointer points at something different...
Programs with problems like this tend to work fine in one environment and crash horribly in another. And I've made plenty like that. Sad.
These are the most horrid errors to search for, but once found are nice and easy to correct, and it's very satisfying.


 
i guess you mean minX, maxX...ect. is undefined...

but heres the function

void tri2d :: findMinMaxPts ( int & minX, int & maxX, int & minY, int & maxY, point2d * pts )
{
minX = ( int ) pts[ a ].x;
maxX = ( int ) pts[ a ].x;

if( pts[ b ].x < minX )
minX = ( int ) pts[ b ].x;
if( pts[ b ].x > maxX )
maxX = ( int ) pts[ b ].x;

if( pts[ c ].x < minX )
minX = ( int ) pts[ c ].x;
if( pts[ c ].x > maxX )
maxX = ( int ) pts[ c ].x;

minY = ( int ) pts[ a ].y;
maxY = ( int ) pts[ a ].y;

if( pts[ b ].y < minY )
minY = ( int ) pts[ b ].y;
if( pts[ b ].y > maxY )
maxY = ( int ) pts[ b ].y;

if( pts[ c ].y < minY )
minY = ( int ) pts[ c ].y;
if( pts[ c ].y > maxY )
maxY = ( int ) pts[ c ].y;
}


as you can see, it doesnt use any of the variables immediatly, it assigns a value to them first...

is that it? or do you mean another uninitialized variable? im gona try to use gdb to see if i can find where the problem is...
 
oh and i printed out the values of minX1, maxX1..ect, after the function findMinMaxPts has been called and they are like they should be...
 
um and another thing that may be problematic, in gdb i printed out the first doLinesIntersect after the function linesIntersect was called and it was 232....how is that possible if doLinesIntersect is bool?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top