Archived

This topic is now archived and is closed to further replies.

problem with recursive function

This topic is 5309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I can't figure out how to get it to return the position of the object that's being hit (not the object that's doing the hitting).
int Collisions::boundingBoxCollision(const Object *object, const int &objectPos, int pos2){
if(objectPos < numObjects-(pos2-objectPos)){
if( object[objectPos].vector.pos.y <= object[pos2].vector.pos.y+object[pos2].dimensions.height &&
object[objectPos].vector.pos.y + object[objectPos].dimensions.height >=object[pos2].vector.pos.y )
if( object[objectPos].vector.pos.x <= object[pos2].vector.pos.x+object[pos2].dimensions.width &&
object[objectPos].vector.pos.x + object[objectPos].dimensions.width >= object[pos2].vector.pos.x)

return pos2;
else
return boundingBoxCollision(object, objectPos, pos2+1);

}
return 0;
}

It checks to see if the Y boundaries intersect. If they do, it checks the X boundaries. If those intersect too, it should return 'pos2', the index of the element in the array. If the Y collision is true but the X isn't, it moves ahead by one element the Object array (which is sorted by Y position). As far as I can tell, every time it reports a collision, it returns 1 (even that may be garbage). I can't see what's wrong with the code though. Any help would be very much appreciated. [edited by - Odoacer on January 3, 2004 11:58:54 PM]

Share on other sites
Your question is a little confusing - I don''t understand what else this function could return when it "reports a collision" but pos2, since the only two return statements in the whole function return pos2 and zero. If a value of 1 is being returned, then your test must be accepting the Object in object[1].

Your collision detection logic is a little weird - that may be the source of your problem. I think it will only return true (pos2) when object[objectPos] is entirely within object[pos2] - overlapping cases will not be detected.

Again, though, I don''t understand exactly what you''re having trouble with. Can you clarify your question please?

HTH...
Riley

PS:
if (objectPos < numObjects - (pos2 - objectPos))

simplifies to

if (pos2 < numObjects)

Share on other sites
I''m not really sure what''s wrong either (all I know is that it''s not really working), so I''ll try to explain as best as I can what''s going on.

As for the collision-detection logic, I''m pretty sure it works OK. I designed it first in Macromedia Flash and it worked fine then. From my stripped-down tests in C++ (using DirectDraw), it seems to be fine. If you have a better method, I''d be happy to hear it.

I''m beginning to think that maybe the collision-detection isn''t the source of the problem I''m having. What''s happening is, if this returns pos2, I have a function that''s supposed to apply a few changes (mostly vector swaps and so forth) onto the objects. But instead of just the two objects (objectPos and pos2) being affected, it screws up positions in the whole array. I thought I had it tracked down to it returning the wrong position in the array, but maybe not.
I''m calling a Sort function but that''s after I cycle through every element in my Object list and apply any relevant changes, so I don''t see how that could be affecting anything.

It''s probably my pointers acting funny. Here''s my collision-detection loop, if it helps:
int collisionPos = 0;	for(int i = 0; i < numObjects; i++){		collisionPos = collision.detectObjectCollision(object, i, i+1);			if(0 == collisionPos)			//no collision, apply things like gravity		else			objectCollisionDetected(object[i], object[collisionPos]); //its after I call this that things get messed up	}	collision.Sort(object, 0);

The objectCollisionDetected parameters are passed as reference, and the ''object'' in Sort is passed as a pointer to the whole array. (''object'' is already a pointer)

Share on other sites
Ah, I''m afraid I must confess to extreme stupidity.

I know why my function was returning 1 - I forgot that the ''boundingBoxCollision'' being called within a general ''detectObjectCollision'' function, and guess which was bool. But all my code is fine otherwise, thanks for trying to help anyway rileyriley. This one had me stumped.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 28
• 40
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631737
• Total Posts
3001945
×