Getting at the remainder
BlitzPlus Forums/BlitzPlus Programming/Getting at the remainder
| ||
Some help would be very welcome guys ! I've written this tile engine thingy, that extracts the tile at the screen coords by dividing the x and y coords by the tile size. Seems to work ok, but I'd like to extract the remainder from it.. (to use as as offset) for example.. my routine brings back X = 3.567 Yes, I use the 3 to find the map tile, but I'd also like to use 5 to tell me how many pixels it is into the tile. (So I can check for collisions within the tile) Problem is, the tiles are 64 x 64 - so I cant just strip out to 1 decimal place. :( Any Advice please !. |
| ||
Something like this?TileWidth% = 64 TileHeight% = 64 x# = 3.567 OffsetX% = Tilewidth * (X Mod 1) |
| ||
Thanks GfK, I think thats it.. But my code is so screwed it doesnt look like its passing the correct position to the sum anyway. My routine has become so bloated with stuff I'm not sure about, I cant work out whats going on !!!. If I ever figure it out, I'm sure that formula is what I need. CHEERS !. |
| ||
HELP !! This makes no sense what-so-ever !!!! Having stripped out all the maths doda parts to make it as simple as possible, its still not working. Imagine the screen 800x600, each tile is 64x64, drawn from 0,0. (2d top-left) I want to report what tile is at the current mouse coords, so I've been dividing xmouse/64.0 But for some reason, its out of sync !. Seems to only work at +32 - the middle of each tile. (try it !) I can add a 32 pixel offset: x2#=(xmouse+32)/64.0 Thats fine, gives back the correct tile number.. but now I want to return the remainder. Using GfK's formula above.. its still doing that weird thing where it's off by 32 pixels. (wheather you add the offset of 32 pixels to it or not !) - which I have no idea why I should have to anyway !?. OffsetX% = 64 * ((x2#+32) Mod 1) Just doesnt give me the correct numbers back !. Why is it doing this ?. |
| ||
How did floating point numbers get into this? Pixels and mouse coordinates are all integers. Let's say you start with x = mousex(), which happens to be 170. tilex = x/64 tilex is now 2 because any remainder is discarded. There are thus 2 whole tiles to left. How far are you into the current tile? offsetx = x - tilex*64 This subtracts off the two tiles to the left, leaving 170-128 = 42. |
| ||
If you know the integral part of the float, then you can get the remainder. F# = your float. FI# = Floor(F#) FR# = F# - Floor(F#) |
| ||
Thanks for all the help guys ,really appreicated.. but still stuck ! :( What happening is I've got this scrolly routine that uses offsets. Since a tile is 64 pixels, if can go off the screen to the coords -63. (before the map is updated by one tile and the offset put back to '0') What was x = 0.. before scrolling can now be x = -5 (or whatever) I can counter for the offset quite easy to get at the current tile under the mouse: Function return_tile(xtemp,ytemp,ly) x=layer(ly)\x_offset# tilex = (xtemp-x)/64 tilex=(tilex+layer(ly)\x_map)+1 Text 100,340,"Tile X:"+tilex,True,True ; WORKS ! :) offsetx=xtemp - tilex *64 Text 200,340,"Offset X:"+offsetx,True,True ; Nope.. offset still screwed :( End Function But as you can see, having that offset doesnt give back a proper remainder. layer(ly)\x_map contains the map position layer(ly)\x_offset# contains the offset of the whole map It's easier to see whats going on when you see the program running - I'd be more than happy to email the whole source to someone if they could take the time to take a look. |
| ||
Solved it myself with this:Function return_tile(xtemp,ytemp,ly) x=layer(ly)\x_offset# tilex = (xtemp-x)/64 tilex=(tilex+layer(ly)\x_map)+1 Text 100,340,"Tile X:"+tilex,True,True tilex2=xtemp/64 offsetx=xtemp - tilex2 * 64 offsetx=offsetx+(64-x) If offsetx>64 offsetx=offsetx-64 Text 200,340,"Offset X:"+offsetx,True,True End Function But its not pretty !, must be an better way of doing it than what I'm doing :/ |