# Mathematical Conundrum!

This topic is 2975 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
If guyPosX and guyPosY are integers, chances are what you have will work fine.

##### Share on other sites
Yes, they are integers.

Wont I get an error saying I'm converting from an integer to a double if I do it though?

##### Share on other sites
No, what you are doing is called integer division. Your operands guyPosX and 32 are both integers.

##### Share on other sites
Ok thanks!

Tried it and it works ok :)

##### Share on other sites
Quote:
 Original post by kutuupIs 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.

##### Share on other sites
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;}

##### Share on other sites
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.

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 15
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633004
• Total Posts
3009840
• ### Who's Online (See full list)

There are no registered users currently online

×