Skip to content

Commit 3f310f0

Browse files
committed
Added ESE_Math.CeilToNearestMultiple()
Rounds given float up to nearest multiple of other given float
1 parent c09bda3 commit 3f310f0

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

scripts/Game/ESE_Math.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@
88

99
class ESE_Math
1010
{
11+
// #ESE_ADD_DOCUMENTATION
12+
static float CeilToNearestMultiple(float number, float multiple)
13+
{
14+
if (multiple != 0 && number != 0)
15+
{
16+
float sign;
17+
if (number > 0) {sign = 1;}
18+
else {sign = -1;}
19+
number *= sign;
20+
number /= multiple;
21+
int fixedPoint = Math.Ceil(number);
22+
number = fixedPoint * multiple;
23+
number *= sign;
24+
}
25+
return number;
26+
}
27+
28+
1129
/**
1230
Fixes given value to modulo of 360 and then within -180 and 180, used mostly for rotation values
1331
@code
@@ -38,12 +56,41 @@ class ESE_Math
3856
// See FixInt180(), same but for vectors
3957
static vector FixVector180(vector vec)
4058
{
41-
for (int i = 0; i < vec.Length(); i++)
59+
for (int i = 0; i < vec.Length() - 1; i++)
4260
{
4361
vec[i] = FixFloat180(vec[i]);
4462
}
4563
return vec;
4664
}
65+
//-----------------------------------------------------------------------------------------------------------
66+
// Fast versions for when input is known to be between -360 & 360
67+
static float FastFixFloat180(float val)
68+
{
69+
if (val > 180) {val -= 360;}
70+
else if (val < -180) {val += 360;}
71+
return val;
72+
}
73+
74+
static int FastFixInt180(int val)
75+
{
76+
if (val > 180) {val -= 360;}
77+
else if (val < -180) {val += 360;}
78+
return val;
79+
}
80+
81+
static vector FastFixVector180(vector vec)
82+
{
83+
for (int a = 0; a < 3; a++)
84+
{
85+
float v = vec[a];
86+
while (v > 180)
87+
v -= 360;
88+
while (v < -180)
89+
v += 360;
90+
vec[a] = v;
91+
}
92+
return vec;
93+
}
4794
// -----------------------------------------------------------------------------------------------------------
4895

4996

0 commit comments

Comments
 (0)