• 12
• 15
• 19
• 27
• 9

# Mathematical Conundrum!

This topic is 2756 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;}