Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot SA-MP Pawn Scripting [Pawn] Rounding float to make it divisible by specified value.

 
  • 0 Vote(s) - 0 Average
Pawn Rounding float to make it divisible by specified value.
Xray
Offline

Burgershot Member
Posts: 5
Threads: 1
Joined: Apr 2019
Reputation: 0
#1
2019-04-14, 09:12 PM
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?
Autorojo
Offline

Burgershot Member
Posts: 9
Threads: 0
Joined: Apr 2019
Reputation: 0
#2
2019-04-14, 10:23 PM
Have you tried floatround?

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

Burgershot Member
Posts: 5
Threads: 1
Joined: Apr 2019
Reputation: 0
#3
2019-04-14, 10:32 PM
(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.
blade
Offline

Burgershot Member
Posts: 8
Threads: 1
Joined: Apr 2019
Reputation: 2
#4
2019-04-14, 11:08 PM
Try this

stock GetGridValue(grid_size, Float:value)
{
new ivalue = floatround(value);
while (ivalue % grid_size != 0) (ivalue < 0) ? (++ivalue):(--ivalue);
return ivalue;
}
m e h
Xray
Offline

Burgershot Member
Posts: 5
Threads: 1
Joined: Apr 2019
Reputation: 0
#5
2019-04-15, 12:16 AM (This post was last modified: 2019-04-15, 12:18 AM by Xray.)
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?
denNorske
Offline

Duckyfox
Posts: 20
Threads: 4
Joined: Apr 2019
Reputation: 6
Location: Norway
#6
2019-04-15, 07:55 AM (This post was last modified: 2019-04-15, 07:56 AM by denNorske.)
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]
BigETI
Offline

open.mp Developer

Posts: 107
Threads: 5
Joined: Feb 2019
Reputation: 12
Location: Germany
#7
2019-04-15, 09:39 AM
grid position = X: floor(position x / grid size x); Y: floor(position y / grid size y)
[Image: github-samp-icon.png]
Xray
Offline

Burgershot Member
Posts: 5
Threads: 1
Joined: Apr 2019
Reputation: 0
#8
2019-04-15, 07:45 PM
(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!
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode