Flipping an angle horizontally

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-02 12:14:12

问题


I want to know a way to flip an angle in a horizontal axis, without having to do many operations. Say I have an angle of 0 ("pointing right" in my code's coordinate system), the flipped angle should be 180 (pointing left). If 90 (pointing up), flipped it should still be 90. 89 is 91, and so on. I can operate on the X/Y speeds implied by the angle but that would slow things down, and I feel it's not the proper way to go. I don't know much math so I might be calling things by the wrong name...Can anyone help?

EDIT: Sorry I took long, I had to be out of the computer for long, OK... http://img215.imageshack.us/img215/8095/screenshot031v.jpg

This screenshot might do.The above structure are two satellites and a beam linked to the white dot in the center. The two satellites should inherit the angle of the white dot (it's visible for debug purposes), so if it's aiming at an angle, they will follow. The satellite at the left is mirrored, so I calculated it with 180-angle as suggested, although it was my first try as well. As you can see it is not mirrored but flipped. And when the white dot rotates, it rotates backwards. The other does alright.

This is the angle recalculation for something linked to something else, pid would be the parent, and id the current. pin.ang is the angle offset copied when the object is linked to another, so it keeps position when rotated:

if(object[id].mirror)
    object[id].angle = 180 - (object[id].pin.ang + object[pid].angle);
else
    object[id].angle = object[id].pin.ang + object[pid].angle;

And this is the specific rotation part. OpenGL. the offx/y is for things rotated off-center, like the beam about to come out there, it renders everything else right.

glTranslatef(list[index[i]].x, list[index[i]].y, 0);
glRotatef(list[index[i]].angle, 0.0, 0.0, 1.0);
glTranslatef(list[index[i]].offx, -list[index[i]].offy, 0);

The rotation also seems to miss when the rotation speed (an integer added every redraw to the current angle, positive for rotating clockwise, like in this next one: http://img216.imageshack.us/img216/7/screenshot032ulr.jpg

So it's definitely not 180-angle, despite how obvious it'd be. The mirroring is done by just reversing the texture coordinates so it doesn't affect angle. I am afraid it might be a quirk on the GL rotation thing.


回答1:


The reflected amount (just looking at the maths) would be (180 - angle)

Angle | Reflection
------+-----------
    0 |        180
   90 |         90
   89 |         91
   91 |         89
  360 |       -180
  270 |        -90

Note the negatives if you fall below the "horizontal plane" - which you could leave as they are, or handle as a special case.




回答2:


Isn't it simply

result = 180-(your angle)




回答3:


As already explained, you find the opposite angle by subtracting your angle from 180 degrees. Eg:

180 - yourangle

Directly manipulating the X/Y speeds would not be very cumbersome. You simply reverse the direction of the X speed, by multiplying it by minus 1, example: speedx = (-1) * speedx. This would change the left-right direction, eg: something moving to the left would start moving to the right, and vice versa, and the vertical speed would be unaffected.

If you're using sine/cosine (sin/cos) to recalculate your X/Y speed components, then the *(-1) method would probably be more efficient. Ultimately it depends on the context of your program. If you're looking for a better solution, update your question with more details.




回答4:


Aah, seems the problem came from negative numbers after all, I ensured them being positive and now the rotation does fine, I don't even need to recalculate angle... Thanks to everyone, I ended up figuring out due to bits of every response.




回答5:


to flip counter clockwise to clockwise (270 on right -> 90 on right)

angle - 360

--

to flip vertical (180 on top -> 0/360 on top)

Math.Normalize(angle - 180)

--

both:

float flipped_vertical = angle - 360 float flipped_vertical_and_horizontal = Math.Normalize(flipped_vertical- 180)




回答6:


This solution is for -Y oriented angles (like a watch)! For +X orientation (like school math) you need to swap X and Y.

public static float FlipAngleX(float angle)
{
    angle = NormalizeAngle(angle);

    angle = TwoPi - angle;

    return angle;
}

public static float FlipAngleY(float angle)
{
    angle = NormalizeAngle(angle);

    if (angle < Pi)
    {
        angle = Pi - angle;
    }
    else
    {
        angle = TwoPi - angle + Pi;
    }
    return angle;
}

/// <summary>
/// Keeps angle between 0 - Two Pi
/// </summary>
public static float NormalizeAngle(float angle)
{
    if (angle < 0)
    {
        int backRevolutions = (int)(-angle / TwoPi);
        return angle + TwoPi * (backRevolutions + 1);
    }
    else
    {
        return angle % TwoPi;
    }
}



回答7:


just 360-angle will flip your angle horizontaly but not verticaly



来源:https://stackoverflow.com/questions/1371808/flipping-an-angle-horizontally

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!