• Create Account

Gilla

Member Since 29 Mar 2011
Offline Last Active Oct 01 2011 03:11 AM

In Topic: Tile based collision bug

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.

In Topic: Tile based collision bug

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

PARTNERS