• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] Rounding float to make it divisible by specified value.
#1
Hi, I am developing a building system based on?a map grid. I?figured to use this grid (only xy axis based) I would only need to use a simple algorithm where I devide the current player position by the grid size.

Lets say I want to get the nearest grid in range of?a player, I need to round the player's position till it's devisible by the grid size to get the X Y position from that grid.



Could anyone explain me how to create a function that rounds?the player position to the nearest value where it is devisible by a specified value?
  Reply
#2
Have you tried floatround?



http://wiki.sa-mp.com/wiki/floatround
  Reply
#3
(2019-04-14, 10:23 PM)Autorojo Wrote: Have you tried floatround?



http://wiki.sa-mp.com/wiki/floatround



Thanks for your reply, although, that's not what I need.

I need a function which?rounds?a float to an amount till it's devisible by a specified value.
  Reply
#4
Try this



stock GetGridValue(grid_size, Float:value)

{

new ivalue = floatround(value);

while (ivalue % grid_size != 0) (ivalue < 0) ? (飼�):(--ivalue);

return ivalue;

}
m e h
  Reply
#5
Sorry, I'm trying to wrap my head around this whole grid thing, heck, I'm not even sure what I exactly 'need' to have this working.
I drew an image to show what I have in mind.
[Image: jEz0ppC.png]
With the player's position being:?-508.0857,-967.1429
I would need a function that rounds x to be divisible by 1.75 (Right?)
And another function that rounds y to be divisible by 1.606.
The outcome of the rounded x & y axis would be the center of the nearest grid, right?
  Reply
#6
I suggest you use modulo for this.

Modulo operator will return 0 when it matches a value that is dividable with 1.75 (or 1.606);

https://en.wikipedia.org/wiki/Modulo_operation

In pawn it looks like this: https://forum.sa-mp.com/showthread.php?t=485069

[Image: l3vgZTB.png]
  Reply
#7
grid position = X: floor(position x / grid size x); Y: floor(position y / grid size y)
[Image: github-samp-icon.png]
  Reply
#8
(2019-04-15, 09:39 AM)BigETI Wrote: grid position = X: floor(position x / grid size x); Y: floor(position y / grid size y)



Had to reread it a few times haha, but?this is indeed what I need. Thanks!
  Reply


Forum Jump: