Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 28 May 2016 06:01 PM |
So I've learned how to round to the nearest whole-stud with: CFrame.new(math.floor(mouse.Hit.x),math.floor(mouse.Hit.y+dragItem.Size.y/2),math.floor(mouse.Hit.z))
But now I'm looking to reduce it to the fifths grid.
How would this be done?
|
|
|
| Report Abuse |
|
|
UFAIL2
|
  |
| Joined: 14 Aug 2010 |
| Total Posts: 6905 |
|
|
| 28 May 2016 06:03 PM |
Is google a foreign tool for you? "lua round number" and the first result works. Learn how to search up crap. |
|
|
| Report Abuse |
|
|
|
| 28 May 2016 06:03 PM |
local function roundToGrid(num, grid) return math.floor(num/grid + 0.5) * grid end
And just call roundToGrid with the component and 0.2 (1/5) |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 28 May 2016 06:05 PM |
Thanks Flux, always helpful <3
|
|
|
| Report Abuse |
|
|
|
| 28 May 2016 06:08 PM |
| If you want the intuition on why/how this works I can explain it. But in short: think of the number in base-grid instead of base-10. |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 28 May 2016 06:09 PM |
Not 100% sure what base-grid or base-10 means
|
|
|
| Report Abuse |
|
|
|
| 28 May 2016 06:11 PM |
https://en.wikipedia.org/wiki/Positional_notation#Base_of_the_numeral_system
In short: base-x means you have 'x' characters as digits and all places are of powers of 'x'. base-10 is the system we're most familiar with (also known as decimal); all places are of powers of 10 (ones place (10^0), tens place, hundreds place, etc.) and we have 10 distinct digits to work with (0,1,2,3,4,5,6,7,8,9) |
|
|
| Report Abuse |
|
|
KapKing47
|
  |
| Joined: 09 Sep 2012 |
| Total Posts: 5522 |
|
|
| 28 May 2016 06:14 PM |
Use what Flux gave u, or even simpler.
local inc = 5 --Increment local number = math.random(100) print(number - (number % inc))
The way this works is, say u have 7, what my code does is find the remainder of "number / inc" ("number % inc") which in this case is 2, and takes it away from the number which gives us 5. Another example is, assuming we have 2, we find the remainder of "number / inc" and it is 2, we take away that from the number and we get 0. So as u can see, it snaps to each increment :) |
|
|
| Report Abuse |
|
|
|
| 28 May 2016 06:15 PM |
| That won't round it to the nearest grid, it'll round down to the nearest grid. |
|
|
| Report Abuse |
|
|
KapKing47
|
  |
| Joined: 09 Sep 2012 |
| Total Posts: 5522 |
|
|
| 28 May 2016 06:18 PM |
Oops, my bad
local inc = 5 --Increment local number = math.random(100) number = number * 2 print(number - (number % inc) - (number * .25))
This should work :) (And I broke my "Slightly simpler code" promise XD) |
|
|
| Report Abuse |
|
|
|
| 28 May 2016 06:19 PM |
| I mean mine is really easy to understand if you think in terms of a different base. |
|
|
| Report Abuse |
|
|
KapKing47
|
  |
| Joined: 09 Sep 2012 |
| Total Posts: 5522 |
|
| |
|