if (x < -PI)
x += TWOPI;
else
if (x > PI)
x -= TWOPI;
The problem: x could be 400.0 or more, under those circumstances this wrapping method fails.
I need to keep x in the radian range, always. Any ideas?
Thanks
keep value within the range of radians
My current code:
Really? that's just pseudo code in fact. I'm coding in C++. I just drew a blank and I can't think of any good way of doing this...
Right now I'd have to apply the aforementioned statement on each update instead of every now and then (when I actually do require X to be in that range). It's a small piece of code and it won't kill any performance at all I was just wondering though if there could be a better way of doing it.
Right now I'd have to apply the aforementioned statement on each update instead of every now and then (when I actually do require X to be in that range). It's a small piece of code and it won't kill any performance at all I was just wondering though if there could be a better way of doing it.
How would you go about using modulo (fmod as suggested) when the variable starts on -PI and ends as PI ? (full range) -- take a look at my pseudo code to see what I mean.
Quote:Original post by gussoAbsolutely, imagine if you'd picked a language that has a wrapping function already [smile]
Really?
Quote:I'm coding in C++. I just drew a blank and I can't think of any good way of doing this...This is a common and fairly unsophisticated solution:
template <typename T>T wrap(T value, T lower, T upper){ T r = upper - lower; while (upper < value) value -= r; while (value < lower) value += r; return value;}// ...x = wrap(x, -PI, PI);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement