0

i study OpenGL ES 2.0. But i think it's more C++ question rather then OpenGL. I'am stuck with rotation question. It is known, that rotation transformation can be applied using the following equations:

p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox
p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy

But it seems that when i perform this rotation operation several times the accuracy problem is occured. I guess, that the core of this problem is in uncertain results of cos function and floating point limitations. As a result i see that my rotating object is getting smaller and smaller and smaller. So:

1.) How do you think, does this issue really connected with floating point accuracy problem?

2.) If so, how can i handle this.

Suppose that float _points[] is array containing coordinates x1,y1,x2,y2...xn,yn. Then i recompute my coordinates after rotation in the following way:

       /* For x */
    float angle = .... ;
pair<float, float> orig_coordinates(0, 0);
        for (; coors_ctr < _n_points * 2; coors_ctr += 2)
            _points[coors_ctr] = cos(angle) * (_points[coors_ctr] - _orig_coordinates.first) -
                sin(angle) * (_points[coors_ctr + 1] - _orig_coordinates.second) + 
                _orig_coordinates.first;
        /* For y */
        coors_ctr = 1;
        for (; coors_ctr < _n_points * 2; coors_ctr += 2)
            _points[coors_ctr] = sin(angle) * (_points[coors_ctr - 1] - _orig_coordinates.first) +
            cos(angle) * (_points[coors_ctr] - _orig_coordinates.second) + _orig_coordinates.second;
Alex Hoppus
  • 3,821
  • 4
  • 28
  • 47
  • Why are you complicating things with orig_coordinates set to the origin? Remove them. Until you identify the problem. Also show your full code, if your going to access a variable or array then show the code that defines them, and show the code that initializes them. And why not do this in a single loop? I'm assuming _n_points is the number of points, not the size of the array which should be (_n_points * 2)? But the code is missing.. – Dan Apr 19 '14 at 17:52
  • And quit using underscores. These are typically reserved to indicate the nature of a variable. 2 underscores __var typically indicate that it is a compiler internal variable, and 1 underscore _var typically means it is an internal label of the crt or some other protected lib. Try to make your code neat and readable. It will take you much further towards understanding what is going on. – Dan Apr 19 '14 at 17:56
  • And as for precision. Yes. You will experience weird behaviors when you keep compounding error. You should store the original locations of the points, and create a new set of modified values each iteration by keeping track of the total theta that has built up over each iteration. Cos(theta) will always be 0..1..0..-1..0, Sin(theta) 1..0..-1..0..1, so the error here always results in a valid rotation. Where errors in your position will result in errors in your position that will eventually no longer be valid. – Dan Apr 19 '14 at 18:00

1 Answers1

4

I think the problem is that you're writing the rotated result back to the input array.

p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox
p'y = sin(theta) * (p'x-ox) + cos(theta) * (py-oy) + oy

Try doing the rotation out of place, or use temporary variables and do one point (x,y) at a time.

Gautam Raj
  • 199
  • 1
  • 4