4

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    It sounds to me as if you want to 'invert about a VERTICAL axis' – pavium Sep 03 '09 at 07:05
  • Do you want to treat pointing down as 270 or -90? Or is that never going to happen? – pavium Sep 03 '09 at 07:15
  • Well, I am not too sure of what I am doing, I am sorry for the bad explanation. My objective is having cannons on a character, and those cannons following the state of the character so they aim at the opposite direction based on their angle. Basically swapping right and left so the cannons can follow the mirror flag. If I do 180-angle when rotating, they will be flipped, so a cannon pointing down-right will aim up-left. I try to have it point down-left. –  Sep 03 '09 at 07:28
  • 1
    If you do 180+angle, it will give you the exact opposite direction (eg: up-left will become down-right). If you do 180-angle, it will flip it left-to-right (or right-to-left), without affecting the up/down (eg: up-left will become up-right). – Samuel Jaeschke Sep 03 '09 at 07:58
  • So what is it exactly that you want, do you want to reflect it across the the Y-axis? Because subtracting the angle from 180 works perfectly, could you be more clear on the angles in your example where it doesn't work. For everything you said in the first post, subtracting from 180 works. What you could do though just operating on the x/y coordinates is multiply the y coordinate by -1. This will always reflect your coordinate across the y axis. – JSchlather Sep 03 '09 at 17:11
  • I want it for both satellites to be facing each other as they rotate, and not rotate backwards. There is no more code related to angles except for angle+=rotatespeed. –  Sep 03 '09 at 17:58
  • I think [this](https://math.stackexchange.com/questions/1319615/how-to-calculate-opposite-direction-angle) would help for your case. – juniortan Mar 18 '18 at 14:30

7 Answers7

13

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.

butterchicken
  • 13,583
  • 2
  • 33
  • 43
  • 180-angle will also flip it vertically, like a 9 and a 6. I should have noted I am using this for graphics. The graphic will be flipped when a flag is set, and I want it to have the opposite angle so it keeps the same construction if a base is rotated as well. But 180 - angle results in the object turning upside down instead. –  Sep 03 '09 at 07:19
  • 1
    You can normalize angles to fall into for example the [0;360) interval by adding or subtracting a multiple of 360 degrees. – starblue Sep 03 '09 at 07:30
  • 1
    @DalGr: In that case there is no way to flip the object by changing just the angle. You need to mirror it, i.e. flip each point separately in the ojbect. – Guffa Sep 03 '09 at 07:30
  • @DalGr: I think you are confusing something. How does the model look that you want to flip? How should it look after the flip? – Svante Sep 03 '09 at 08:05
  • Are you able to post a screenshot? It depends on how your image is constructed. But yes, as @Guffa said, you probably need to flip the image. – Samuel Jaeschke Sep 03 '09 at 08:06
  • @DalGr - You should add that to the question. @Guffa is right in this case. – butterchicken Sep 03 '09 at 08:11
3

Isn't it simply

result = 180-(your angle)

Martin
  • 2,442
  • 14
  • 15
2

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.

Samuel Jaeschke
  • 1,126
  • 11
  • 23
2

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;
    }
}
vikingfabian
  • 451
  • 5
  • 5
0

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.

0

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)

Wikkle_A
  • 145
  • 2
  • 12
-1

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

user5450074
  • 97
  • 1
  • 8