Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

best way to round rotation(in radians)?

Asked by 5 years ago
Edited 5 years ago

i am messing with a retro - like concept, where i have everything's position rounded to the nearest whole(via CFrame) and have their orientation is rounded to nearest 45 degrees, or 2pi / 4 in radians(also via CFrame). I am encountering problems with the latter as i am yet to find a good way to round it to the nearest 2pi / 8(since it is in radians, the math.floor thing is practically rendered useless). Currently i am alternating between two methods, which are:

1: converting an object's rotation(in radians) into degrees before rounding it to the nearest 45 degrees and converting that back into radians

and

2: referring to a table of angular revolution (in radians(e.g, (2pi / 8 * 1), (2pi / 8 * 2), (2pi / 8 * 3), etc)), finding the one closest in proximity to the object's actual rotation(in radians) and changing the object's rotation to that.

I feel these methods are pretty trashy, and wanna find a more clean - cut one(like, just directly rounding the radians to the nearest 2pi / 8 without, say, any conversion or referral of a table). So pls help

thx a lot

1 answer

Log in to vote
1
Answered by
sleazel 1287 Moderation Voter
5 years ago
Edited 5 years ago

Actually math.floor is your answer, you just need to use it correctly:

local rotation = --insert actual part rotation here
local rotationStep = (2*math.pi)/8 --BTW you can just use math.pi/4
local newRotation = math.floor((rotation/rotationStep)+0.5)*rotationStep

Edit: added 0.5, to round to the nearest angle

0
works great thx TheluaBanana 946 — 5y
0
guess i really didnt think this through TheluaBanana 946 — 5y
0
You're welcome :) I wouldn't know that, if I haven't had a similar problem in the past. sleazel 1287 — 5y
Ad

Answer this question