Mathematical Conundrum!

Started by
6 comments, last by slynk 13 years, 7 months ago
Hey all,

I am trying to code collision with a tile map.

The problem I have is that the tile map is by array location, but player movement is by pixel.

I need a way to make this calculation to determine whether a tile is passable (array location value 0).

bool tilePassable(){  if(map[guyPosX / 32][guyPosY / 32] == 0)   {     return true;   }  else   {     return false;   }}


The problem I have is that naturally the precise pixel the character is sitting on will not necessarily be a multiple of 32, so I will end up with a decimal resule when I divide its location by 32.

Is there a way to make this calculation to the nearest whole number, ie ignoring any remainder, so that, for example 100 / 32 = 3 (ignoring the remaining 4).

Is there a way to make this calculation in code?

Thanks

[Edited by - Zahlman on August 31, 2010 4:08:40 PM]
Advertisement
If guyPosX and guyPosY are integers, chances are what you have will work fine.
Yes, they are integers.

Wont I get an error saying I'm converting from an integer to a double if I do it though?
No, what you are doing is called integer division. Your operands guyPosX and 32 are both integers.

Read this: http://cnx.org/content/m18717/latest/
Ok thanks!

Tried it and it works ok :)

Quote:Original post by kutuup
Is there a way to make this calculation to the nearest whole number, ie ignoring any remainder, so that, for example 100 / 32 = 3 (ignoring the remaining 4).


Though in this case it isn't needed since you're using integers, you could use the floor function.
While we're at it, let's simplify.

"If it is raining outside, I will take my umbrella with me; otherwise, I will not take my umbrella with me."

Ugh. Wordy, and not actually making things clearer.

"I will take my umbrella with me if (and only if) it is raining".

Much better.

Thus:

bool tilePassable(){  return map[guyPosX / 32][guyPosY / 32] == 0;}
The problem with this approach, as I learned a few days ago coding my tile engine, is that if the player's sprite isn't the same dimensions as the tiles, then you'll collide with a lot of "air". If the pos of your sprite is based on the top left pixel, then checking collision on the top and left will be perfect but on the right and bottom will not.

I'm sure there are a million ways around this, however I just include 3 small arrays (mine are vectors but you could use what ever you want). One for the xpos for collidable tiles, one for the ypos of collidable tiles, and one for the type of collidable tile (==1 for collidable on all sides, ==2 if it damages you, etc etc.) Then you can make a more precise collision check based off of x/y coord instead of tile spots. This also stops you from cycling through a large map and only cycle through known collidable spots. The array is filled when the level is made by deciding what tiles are collidable, multiplying their index by the tile size, and adding that number to the array.

This topic is closed to new replies.

Advertisement