Colition Detector Malfunction

Started by
0 comments, last by Wyrframe 9 years ago

OK, so i have created a colition detector using bounding boxes. It gets the blocks below the player, one on the right, one on the left, or if the player is completely perfect on the block, the center one. It then creates bounding boxes, but it isnt working for some reason. IM only going to show some of it, but youll get the idea.

bool testForCollision(const boundingbox& a, const boundingbox& b) {
if (a.lry < b.uly) return false; // top of a is below bottom of b
if (a.uly > b.lry) return false; // bottom of a is above top of b
if (a.lrx < b.ulx) return false; // right of a is more right than left of b
if (a.ulx > b.lrx) return false; // left of a is more left than right of b
return true; // otherwise, they must be colliding somewhere
}

bool check_colition_down(int speed)
{
int tempx=mainplayer.x; int tempy=mainplayer.y+speed;
int tempblockx1=ceil(tempx/32); int tempblocky1=floor((tempy+mainplayer.height)/32);
int tempblockx2=ceil(tempx/32); int tempblocky2=ceil((tempy+mainplayer.height)/32);
boundingbox blockbelow; blockbelow.setposition(tempblockx1*32,tempblocky1*32,tempblockx1*32+32, tempblocky1*32+32);
boundingbox blockbelow2; blockbelow2.setposition(tempblockx2*32,tempblocky2*32,tempblockx2*32+32,tempblocky2*32+32);
if(testForCollision(blockbelow2,mainplayer.colition))
{
if(testForCollision(blockbelow,mainplayer.colition))
{
if(!blocklist[mainmap.getblock(tempblockx1,tempblocky1)].walkable)
{
if(!blocklist[mainmap.getblock(tempblockx2,tempblocky2)].walkable)
return false;
}
}
}
return true;
}

Advertisement

bool testForCollision(const boundingbox& a, const boundingbox& b) {
    if (a.lry < b.uly) return false; // top of a is below bottom of b
    if (a.uly > b.lry) return false; // bottom of a is above top of b
    if (a.lrx < b.ulx) return false; // right of a is more right than left of b
    if (a.ulx > b.lrx) return false; // left of a is more left than right of b
    return true; // otherwise, they must be colliding somewhere
}
 
bool check_collision_down(int speed)
{
	int tempx=mainplayer.x;
	int tempy=mainplayer.y+speed;
	
	int tempblockx1 = ceil(tempx/32);
	int tempblocky1 = floor((tempy+mainplayer.height)/32);
	
	int tempblockx2 = ceil(tempx/32);
	int tempblocky2 = ceil((tempy+mainplayer.height)/32);
	
	boundingbox blockbelow;
	blockbelow.setposition(tempblockx1*32, tempblocky1*32, tempblockx1*32+32, tempblocky1*32+32);
	
	boundingbox blockbelow2;
	blockbelow2.setposition(tempblockx2*32, tempblocky2*32, tempblockx2*32+32, tempblocky2*32+32);
	
	if(  testForCollision(blockbelow2, mainplayer.colition)
	  && testForCollision(blockbelow, mainplayer.colition)
	  && ! blocklist[mainmap.getblock(tempblockx1,tempblocky1)].walkable
	  && ! blocklist[mainmap.getblock(tempblockx2,tempblocky2)].walkable
	) {
		return false;
	}
	
	return true;
}

Rewritten for legibility (use the Code tags), and with a small edit for comprehensibility (the compound test in check_colition_down [sic]).

However, I'm finding this code pretty hard to follow. I don't know what you're trying to test.

Looking at the testForCollision() function, it appears that your coordinate system increases upwards and leftwards. This seems to conflict with the idea in the lines where you initialize tempblocky1,2, where you add the player's projected post-movement position to their height; unless mainplayer.height is negative, you seem to be testing for the presense of two tiles, one above the other, at the player's top, not the bottom.

mainplayer.colition [sic] appears to be a bounding rectangle for the player; but you are using the rectangle unmodified, you haven't positioned or adjusted it to account for the +speed movement you're applying in your search for tiles to test against. Why don't you just use the coordinates in mainplayer.colition, instead of calculating them from mainplayer.x,y,height ?

Last of all, this tests two tiles at the player's top Y position, and only in the column containing the player's X coordinate. Shouldn't you be testing two tiles at the player's head, one at the floor() of their X position and one at the ceil(), and even that assumes that the player is exactly one tile, 32 units here, in width? Again, why not iterate over the mainplayer.colition rectangle to test all tiles across their width?

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

This topic is closed to new replies.

Advertisement