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;
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top