• Create Account

# Tile based collision bug

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Gilla  Members   -  Reputation: 100

Like
0Likes
Like

Posted 26 September 2011 - 10:42 PM

I'm making a 2D game with a tile engine. The issue seems to be my code is testing vertical and horizontal collision in a different order then I intended, causing the sprite get pushed off the screen when colliding. Or in some cases being locked in place due to faulty collision. My code is fairly simple so I can't see why it's failing. Here is the collision component, and I'll explain the code below.
public class CollisionComponent {

float x, y;
/*
* This here is a collision array
* I know, wtf right?
* He'eres the idea.
* I need to test for verticle and horizontal collision seperatly.
* If i don't then my game will ignore one for the other. This causes
* falls through floors and stuff. With collision being an array
* I can have one function return two values intead of two functions
* returning one value each.
*/

public void checkTouching(Entity entity, editor.MapComponent map ){

entity.collision[0] = 0;
entity.collision[1] = 0;

/*
* Entity is falling, check bottom side collision
*/
if (entity.verticleVelocity > 0.0f){
bottom (entity, map);
if (entity.collision[0] == 1){
entity.Y = ((map.rowIndex * map.tileHeight) - entity.Height) ;
entity.verticleVelocity = 0.0f;
}
}
/*
* Entity is rising, check top side for collision
*/
else if (entity.verticleVelocity <  0.0f){
top (entity, map);
if (entity.collision[0] == 2){
entity.Y = ((map.rowIndex + 1) * map.tileHeight);
entity.verticleVelocity = 0.0f;
}

}
/*
* Entity is moving right, check right for collision
*/
if (entity.Velocity > 0.0f){
right(entity, map);
if (entity.collision[1] == 3){
entity.X = ( ((map.columnIndex ) * map.tileWidth) - entity.Width) ;

}
}
/*
* Entity is moving left, check left for collision
*/
else if (entity.Velocity < 0.0f){
left(entity, map);
if (entity.collision[1] == 4){
entity.X = ((map.columnIndex + 1) * map.tileWidth);

}
}

}

public void bottom(Entity entity, editor.MapComponent map){

/*
* calculate which tile the bottom of entity is in
*/
map.rowIndex = (int) ((entity.Y + entity.Height ) / map.tileHeight);

for (float x = (entity.X); x <= ((entity.X) + entity.Width); x += map.tileWidth){

map.columnIndex = (int) (x / map.tileWidth); // the (int) casts the entity.x float into an int so I can do the calculation
map.selectedTile = map.map[map.rowIndex][map.columnIndex];
if( map.selectedTile == 1){
entity.collision[0] = 1;
}

}
/*
* this copy pasta code is horrible I know ..... same as above
* it's actually kind of meta now as I just copy pasta'd the
* copy pasta code for each side of the sprite....
*/
map.columnIndex = (int) (( (entity.X ) + entity.Width) / map.tileWidth);
map.selectedTile = map.map[map.rowIndex][map.columnIndex];

if( map.selectedTile == 1){
entity.collision[0] = 1;
}

}

public void top(Entity entity, editor.MapComponent map){

/*
* calculate which tile the top of entity is in
*/
map.rowIndex = (int) ((entity.Y ) / map.tileHeight);

for (float x = (entity.X); x <= ((entity.X ) + entity.Width); x += map.tileWidth){

map.columnIndex = (int) (x / map.tileWidth); // the (int) casts the entity.x float into an int so I can do the calculation
map.selectedTile = map.map[map.rowIndex][map.columnIndex];
if( map.selectedTile == 1){
entity.collision[0] = 2;
}

}
/*
* this copy pasta code is horrible I know ..... same as above
* it's actually kind of meta now as I just copy pasta'd the
* copy pasta code for each side of the sprite....
*/
map.columnIndex = (int) (( (entity.X) + entity.Width) / map.tileWidth);
map.selectedTile = map.map[map.rowIndex][map.columnIndex];

if( map.selectedTile == 1){
entity.collision[0] = 2;
}

}

public void right(Entity entity, editor.MapComponent map){

/*
* calculate which tile the right of entity is in
*/

map.columnIndex = (int) ((entity.X + entity.Width ) / map.tileWidth);

for (float y = (entity.Y) ; y <= ((entity.Y ) + entity.Height); y += map.tileHeight){
map.rowIndex = (int) ( (y) / map.tileHeight); // the (int) casts the entity.x float into an int so I can do the calculation
map.selectedTile = map.map[map.rowIndex][map.columnIndex];
if( map.selectedTile == 1){
entity.collision[1] = 3;
}

}
/*
* this copy pasta code is horrible I know ..... same as above
* it's actually kind of meta now as I just copy pasta'd the
* copy pasta code for each side of the sprite....
*/
map.rowIndex = (int) (((entity.Y)  + entity.Height) / map.tileHeight);
map.selectedTile = map.map[map.rowIndex][map.columnIndex];

if( map.selectedTile == 1){
entity.collision[1] = 3;
}

}

public void left(Entity entity, editor.MapComponent map){

/*
* calculate which tile the right of entity is in
*/

map.columnIndex = (int) ((entity.X) / map.tileWidth);

for (float y = (entity.Y) ; y <= ((entity.Y ) + entity.Height); y += map.tileHeight){
map.rowIndex = (int) ((y) / map.tileHeight); // the (int) casts the entity.x float into an int so I can do the calculation
map.selectedTile = map.map[map.rowIndex][map.columnIndex];
if( map.selectedTile == 1){
entity.collision[1] = 4;
}

}
/*
* this copy pasta code is horrible I know ..... same as above
* it's actually kind of meta now as I just copy pasta'd the
* copy pasta code for each side of the sprite....
*/
map.rowIndex = (int) (( (entity.Y) + entity.Height) / map.tileHeight);
map.selectedTile = map.map[map.rowIndex][map.columnIndex];

if( map.selectedTile == 1){
entity.collision[1] = 4;
}

}

}


It is rough I know. I hold the collision results in a 2d array. The first index hold vertical collision(0 for not, 1 for collision on bottom, 2 for collision on top), and I use the second index for the horizontal collision. I test verticle collision first and move the sprite if collision was true. I then test horizontal collision and so blah blah. The rest of my code basically works by polling input (which only changes velocity), applying physics (which applies the velocity to the sprite's position), then testing collision and finally updating the screen. I've included all my source if you want to look at it, but the collision code should be enough.

Any ideas where I'm going wrong here? I think this collision code should work.

### #2FlyingDutchman  Members   -  Reputation: 207

Like
0Likes
Like

Posted 27 September 2011 - 12:25 AM

I think I had the same problem. I fixed it by checking first IF the player hits the tile.. than from which side.. than i write all the hits in one char.. when i update my player i update his position, than read it out if have had hit and from where and update again.. It had also to do with the View Direction..

http://www.gamedev.n...sion-detection/

post #8 something contains my code...

The trick is to go through all the tiles in the environment of the player, so the grid around him, and check if there is AT LEAST one hit on one tile with the flag isWalkable no.. and when yes from which direction. So before i loop through so 9 tiles around the player, i set 4 bools for the direction to false. than i check, as soon as i have a hit, the bool goes true..
after the loop i write the 4 values in an char go back and test against the current viewDirection. Than i update ..

hope it helps!!

peace

I open sourced my C++/iOS OpenGL 2D RPG engine :-)

See my blog: (Tutorials and GameDev)

http://howtomakeitin....wordpress.com/

### #3Gilla  Members   -  Reputation: 100

Like
0Likes
Like

Posted 27 September 2011 - 11:45 AM

I think I had the same problem. I fixed it by checking first IF the player hits the tile.. than from which side.. than i write all the hits in one char.. when i update my player i update his position, than read it out if have had hit and from where and update again.. It had also to do with the View Direction..

http://www.gamedev.n...sion-detection/

post #8 something contains my code...

The trick is to go through all the tiles in the environment of the player, so the grid around him, and check if there is AT LEAST one hit on one tile with the flag isWalkable no.. and when yes from which direction. So before i loop through so 9 tiles around the player, i set 4 bools for the direction to false. than i check, as soon as i have a hit, the bool goes true..
after the loop i write the 4 values in an char go back and test against the current viewDirection. Than i update ..

As far as I can tell that's the way I'm doing it. The character can only collide with something in the direction he's moving, so I can use his velocity to determine that. No reason to check for collision on the top of him if he's falling. If he's falling I check for collision on his bottom side. Moving right, I check his right side, ect.

I also check for collision on the entire side of the direction he's moving. That is the for loops you see. If he were falling I'd check his bottom left corner. Then a tile width to the right. Continue until you reach his bottom right corner.

If I have found a collision I move the character to the out side of the tile he collided with. I do this once for his vertical collision, and then again with his horizontal.

My issue seems to be my collision component is picking up a collision on an axis that should already be solved and is stopping my character from moving. For instance. On the ground he can't move left or right but can jump. When jumping and touching a wall he can' fall or rise. I've poured over this code dozens of times and I can't see where my logical error is. I really need a fresh set of eyes on it.

Thanks

### #4Gilla  Members   -  Reputation: 100

Like
0Likes
Like

Posted 28 September 2011 - 02:45 PM

I fully understand not wanting to read through my code to find bugs. Is there some more information I can give that might help me get a better idea about what I'm missing. Most of my code there is just repeated for the 4 sides. Let me try and trim it down for readabilities sake.

public void bottom(Entity entity, editor.MapComponent map){

/*
* calculate which tile the bottom of entity is in
*/
map.rowIndex = (int) ((entity.Y + entity.Height ) / map.tileHeight); // find the row bottom of entity is in

for (float x = (entity.X); x <= ((entity.X) + entity.Width); x += map.tileWidth){ // check each column the bottom of the entity is in

map.columnIndex = (int) (x / map.tileWidth); // the (int) casts the entity.x float into an int so I can do the calculation
map.selectedTile = map.map[map.rowIndex][map.columnIndex];
if( map.selectedTile == 1){// tile is unwalkable collision is true
entity.collision[0] = 1;
}

}

//check the bottom right column entity is in
map.columnIndex = (int) (( (entity.X ) + entity.Width) / map.tileWidth);
map.selectedTile = map.map[map.rowIndex][map.columnIndex];

if( map.selectedTile == 1){
entity.collision[0] = 1;
}

}


Some where my code is picking up collision on both the horizontal and verticle movement, when it should only be one of the other if my code has moved him correctly.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS