# Help with collision!

• November 3rd, 2012, 01:00 PM
Dr.Code
Help with collision!
Hi, I'm making a simple game, but I have problems with my collision detection! My world is based on tiles with currently four different types: FLOOR, WALL, VOID and PIT.
You shouldn't be able to walk over WALL or VOID and you should only be able to get over PIT if you're jumping! Here's my problem:

It miscalculates wich type of tile I am actually standing on! For example:

If I'm standing on a FLOOR Tile it may say that I'm standing on an VOID tile 3 tiles away, but it isn't that it calculates each void tile 1 tile away from the actual tile, It doesn't have that kind of pattern!

This is my code that checks if a player can be on a specified tile or not:

Code java:

```public boolean canBeAt(Entity par0Entity, int par1, int par2) {   int var0 = (int) Math.floor((par1 / 32)); int var1 = (int) Math.floor((par2 / 32));   //System.out.println(var0 + " " + var1);   Tile var2 = world[var0][var1]; TileType var3 = var2.getType();   System.out.println(var3);   if (var3 == TileType.VOID || var3 == TileType.WALL) return false;   if (var3 == TileType.PIT && par0Entity.isInContactWithGround()) return false;   return true; }   public BufferedImage getTexture(int par0) {   return tileTextures[par0]; }```

Since my tiles are 32*32 and since my tiles are stored in a Tile[][] were the first bracket is the y axis and the second is the x axis I thought i would do like this:

Divide the players x axis(par1) with 32 then floor the value into an integer and use this as the x axis in the Tile array, then do the same thing with the player y axis(par2), but for some reason I cant explain
it doesn't work!

• November 3rd, 2012, 10:19 PM
Programming_Hobbyist
Re: Help with collision!
You haven't provided enough information for someone to extrapolate the answer to your problem. You didn't even state what type of error you're getting. Is the comparison not working? For instance you're on a regular tile for the ground and it says you're in a pit? Well the problem may lay in the look-up tile array you have.
• November 4th, 2012, 01:38 AM
Dr.Code
Re: Help with collision!
Quote:

Originally Posted by Programming_Hobbyist
You haven't provided enough information for someone to extrapolate the answer to your problem. You didn't even state what type of error you're getting. Is the comparison not working? For instance you're on a regular tile for the ground and it says you're in a pit? Well the problem may lay in the look-up tile array you have.

Im sorry if my message was unclear! I will edit it now!
• November 4th, 2012, 02:06 AM
jps
Re: Help with collision!
Quote:

int var0 = (int) Math.floor((par1 / 32));
(par1 / 32) does not return a double because both operands are integers. So math.floor is not required. Nor is the cast to int at that point.
So:
int var0 = par1/32;
is the same thing as above.

Does this give you the values you thought it would? What does that commented sysout show?
• November 4th, 2012, 02:12 AM
Dr.Code
Re: Help with collision!
Quote:

Originally Posted by jps
Does this give you the values you thought it would? What does that commented sysout show?

I just realized it do not! The x axis is moved forward 3 stepes(where i should get the value 3 i get 0) and the y axis is moved back 2 stepes (where I should get 0 i get 2) I will look further in the problem!
• November 4th, 2012, 02:22 AM
Dr.Code
Re: Help with collision!
SOMEBODY PLEASE HIT ME IN THE HEAD! I have been wrong all along! the look-up Tile array is [y-axis][x-axis] not [x-axis][y-axis]! OMG! But that is easily fixed with a little tweaking on the rendering... But it still doesn't work with the tile collision!
• November 4th, 2012, 04:58 AM
jps
Re: Help with collision!
Quote:

But that is easily fixed with a little tweaking on the rendering... But it still doesn't work with the tile collision!
That does not tell us much and you should post the code with your question.
• November 4th, 2012, 01:51 PM
Dr.Code
Re: Help with collision!
Quote:

Originally Posted by jps
That does not tell us much and you should post the code with your question.

I am still pretty sure that the error is in the canBeAt() method in the first post!
• November 4th, 2012, 03:09 PM
jps
Re: Help with collision!
Quote:

Originally Posted by Dr.Code
I am still pretty sure that the error is in the canBeAt() method in the first post!

Well you are only talking about 6 lines of code. Brute Force!!!
sysout every variable in the method between every line of code in the method.

There is no way we can do anything for you without enough code to compile and run that shows the problem.