(int) Math.floor(x / TILESIZE) or just (int) (x / TILESIZE)
Posted
by
Aidan Mueller
on Game Development
See other posts from Game Development
or by Aidan Mueller
Published on 2012-10-06T16:06:08Z
Indexed on
2012/10/06
21:56 UTC
Read the original article
Hit count: 310
I have a Array that stores my map data and my Tiles are 64X64. Sometimes I need to convert from pixels to units of tiles. So I was doing:
int x
int y
public void myFunction()
{
getTile((int) Math.floor(x / 64), (int) Math.floor(y / 64)).doOperation();
}
But I discovered by using (I'm using java BTW) System.out.println((int) (1 / 1.5))
that converting to an int automatically rounds down. This means that I can replace the (int) Math.floor with just x / 64.
But if I run this on a different OS do you think it might give a different result? I'm just afraid there might be some case where this would round up and not down. Should I keep doing it the way I was and maybe make a function like convert(int i) to make it easier? Or is it OK to just do x / 64?
© Game Development or respective owner