## Re: [Algorithms] Rounding Problem

 Re: [Algorithms] Rounding Problem From: Jon Watte - 2008-05-20 18:56:37 ```Paul at Home wrote: > God this is driving me nuts. It should be so simple! > > I have a world system whereby a grid of 31x31 in grid units is mapped > over a range of real values ranging from -15.5 to +15.5 > > For collision detection reasons, I need a function that will basically > do a mod such that when applied to my real X or Z coordinate, it tells > me (as a range 0-1) how far into a grid cell I am. I would expect the > routine to return 0.1 if I'm a tenth into a particular grid square. > So: gridCellSize = (gridRightEdge-gridLeftEdge)/gridCellCount; floatGridCoordinate = (floatWorldCoordinate-gridLeftEdge)/gridCellSize; intGridCoordinate = (int)floor(floatGridCoordinate); floatPartialGridCoordinate = floatGridCoordinate - intGridCoordinate; ```

 [Algorithms] Rounding Problem From: Paul at Home - 2008-05-20 17:01:08 Attachments: Message as HTML ```God this is driving me nuts. It should be so simple! I have a world system whereby a grid of 31x31 in grid units is mapped over a range of real values ranging from -15.5 to +15.5 For collision detection reasons, I need a function that will basically do a mod such that when applied to my real X or Z coordinate, it tells me (as a range 0-1) how far into a grid cell I am. I would expect the routine to return 0.1 if I'm a tenth into a particular grid square. But, whatever combinations of mod,floor,ceil,etc I've tried, when the real coord is negative, I get a 0.9 instead of a 0.1 I'm kinda looking for a float version of "&" iyswim. Any ideas what I'm doing wrong ? ========================================== RoundDown=crt floorf btw tINT GetGridXFromWorld (tF32 WorldX) { return tINT(RoundDown(WorldX+15.5F)); } tINT GetGridYFromWorld (tF32 WorldZ) { return tINT(RoundDown(15.5f-WorldZ)); } I would expect this function to return how far into the current square I am as a normalised "percentage". It's changed a lot but this is definitely wrong. I just can't seem to fix it! tF32 Frac=Abs((Zc+0.5F)-RoundDown(Zc+0.5F)); Regards, Paul Johnson. http://www.rubicondev.com```
 Re: [Algorithms] Rounding Problem From: Megan Fox - 2008-05-20 17:16:44 ```float val = (coord + 15.5) / (15.5 * 2.0) * 31.0; float gridAmt = val - (int)val; ... which in this case simplifies out to just coord + 15.5, but you get the idea. Or at least I think that works out right? On Tue, May 20, 2008 at 11:01 AM, Paul at Home wrote: > God this is driving me nuts. It should be so simple! > > I have a world system whereby a grid of 31x31 in grid units is mapped over a > range of real values ranging from -15.5 to +15.5 > > For collision detection reasons, I need a function that will basically do a > mod such that when applied to my real X or Z coordinate, it tells me (as a > range 0-1) how far into a grid cell I am. I would expect the routine to > return 0.1 if I'm a tenth into a particular grid square. > > But, whatever combinations of mod,floor,ceil,etc I've tried, when the real > coord is negative, I get a 0.9 instead of a 0.1 > > I'm kinda looking for a float version of "&" iyswim. > > Any ideas what I'm doing wrong ? > ========================================== > RoundDown=crt floorf btw > > tINT GetGridXFromWorld (tF32 WorldX) > { > return tINT(RoundDown(WorldX+15.5F)); > } > tINT GetGridYFromWorld (tF32 WorldZ) > { > return tINT(RoundDown(15.5f-WorldZ)); > } > > I would expect this function to return how far into the current square I am > as a normalised "percentage". It's changed a lot but this is definitely > wrong. I just can't seem to fix it! > tF32 Frac=Abs((Zc+0.5F)-RoundDown(Zc+0.5F)); > > > Regards, > Paul Johnson. > http://www.rubicondev.com > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > GDAlgorithms-list mailing list > GDAlgorithms-list@... > https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list > -- Megan Fox Idyllon, LLC http://www.shalinor.com/ http://www.idyllon.com/ ```
 Re: [Algorithms] Rounding Problem From: Paul at Home - 2008-05-20 17:43:15 ```Apparently not, that's where I came in. I'm actually starting to think there could be a bug elsewhere but this scenario is a nightmare for trying to actually trace into stuff and spot when the problem happens. It might be that my grid coordinates are not overlapping perfectly and I'm reading the wrong bit of map ? Because of the negative, they were out somewhat before I added the floors there. Regards, Paul Johnson. http://www.rubicondev.com ----- Original Message ----- From: "Megan Fox" To: "Game Development Algorithms" Sent: Tuesday, May 20, 2008 6:16 PM Subject: Re: [Algorithms] Rounding Problem > float val = (coord + 15.5) / (15.5 * 2.0) * 31.0; > float gridAmt = val - (int)val; > > ... which in this case simplifies out to just coord + 15.5, but you > get the idea. Or at least I think that works out right? > > On Tue, May 20, 2008 at 11:01 AM, Paul at Home > wrote: >> God this is driving me nuts. It should be so simple! >> >> I have a world system whereby a grid of 31x31 in grid units is mapped >> over a >> range of real values ranging from -15.5 to +15.5 >> >> For collision detection reasons, I need a function that will basically do >> a >> mod such that when applied to my real X or Z coordinate, it tells me (as >> a >> range 0-1) how far into a grid cell I am. I would expect the routine to >> return 0.1 if I'm a tenth into a particular grid square. >> >> But, whatever combinations of mod,floor,ceil,etc I've tried, when the >> real >> coord is negative, I get a 0.9 instead of a 0.1 >> >> I'm kinda looking for a float version of "&" iyswim. >> >> Any ideas what I'm doing wrong ? >> ========================================== >> RoundDown=crt floorf btw >> >> tINT GetGridXFromWorld (tF32 WorldX) >> { >> return tINT(RoundDown(WorldX+15.5F)); >> } >> tINT GetGridYFromWorld (tF32 WorldZ) >> { >> return tINT(RoundDown(15.5f-WorldZ)); >> } >> >> I would expect this function to return how far into the current square I >> am >> as a normalised "percentage". It's changed a lot but this is definitely >> wrong. I just can't seem to fix it! >> tF32 Frac=Abs((Zc+0.5F)-RoundDown(Zc+0.5F)); >> >> >> Regards, >> Paul Johnson. >> http://www.rubicondev.com >> ------------------------------------------------------------------------- >> This SF.net email is sponsored by: Microsoft >> Defy all challenges. Microsoft(R) Visual Studio 2008. >> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >> _______________________________________________ >> GDAlgorithms-list mailing list >> GDAlgorithms-list@... >> https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list >> Archives: >> http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list >> > > > > -- > Megan Fox > Idyllon, LLC > http://www.shalinor.com/ > http://www.idyllon.com/ > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > GDAlgorithms-list mailing list > GDAlgorithms-list@... > https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list > > ```
 Re: [Algorithms] Rounding Problem From: Tom Plunket - 2008-05-20 18:35:26 ```> It might be that my grid coordinates are not overlapping perfectly > and I'm reading the wrong bit of map ? Because of the negative, > they were out somewhat before I added the floors there. Do your unit tests all pass? This seems like a pretty obvious and even simple example of where unit tests will show you the true path. First pass in (0,0) and make sure you get what you expect (and my question is, what do you expect?). Then pass in the four extents, then spot check a few other places on the grid... -tom! ```
 Re: [Algorithms] Rounding Problem From: Jon Watte - 2008-05-20 18:59:56 ```Tom Plunket wrote: > unit tests will show you the true path. First pass in (0,0) and > make sure you get what you expect (and my question is, what do you > The other question is where the center of the samples in the grid lie. If you expect 0,0 to be the CENTER of the sample in the grid (not the corner of the sample of the grid), then you're in for more bending, as the "true" extent of the 31x31 grid is then 30x30. ```
 Re: [Algorithms] Rounding Problem From: Paul at Home - 2008-05-20 19:46:30 ```Yep, give that man a cookie. I was getting all wound up with negatives, but it was the classic pixel centres problem all along. Thanks again people..... Regards, Paul Johnson. http://www.rubicondev.com ----- Original Message ----- From: "Jon Watte" To: "Game Development Algorithms" Sent: Tuesday, May 20, 2008 8:00 PM Subject: Re: [Algorithms] Rounding Problem > Tom Plunket wrote: >> unit tests will show you the true path. First pass in (0,0) and >> make sure you get what you expect (and my question is, what do you >> > > The other question is where the center of the samples in the grid lie. > If you expect 0,0 to be the CENTER of the sample in the grid (not the > corner of the sample of the grid), then you're in for more bending, as > the "true" extent of the 31x31 grid is then 30x30. > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > GDAlgorithms-list mailing list > GDAlgorithms-list@... > https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list > > ```
 Re: [Algorithms] Rounding Problem From: Jon Watte - 2008-05-20 18:56:37 ```Paul at Home wrote: > God this is driving me nuts. It should be so simple! > > I have a world system whereby a grid of 31x31 in grid units is mapped > over a range of real values ranging from -15.5 to +15.5 > > For collision detection reasons, I need a function that will basically > do a mod such that when applied to my real X or Z coordinate, it tells > me (as a range 0-1) how far into a grid cell I am. I would expect the > routine to return 0.1 if I'm a tenth into a particular grid square. > So: gridCellSize = (gridRightEdge-gridLeftEdge)/gridCellCount; floatGridCoordinate = (floatWorldCoordinate-gridLeftEdge)/gridCellSize; intGridCoordinate = (int)floor(floatGridCoordinate); floatPartialGridCoordinate = floatGridCoordinate - intGridCoordinate; ```