tags:

views:

228

answers:

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. Again, I am sorry for the bad explanation, despite having an engine working OK (except for annoyances like this one), I am not much of a coder, just a designer...

+6  A: 

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
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.
DalGr
You can normalize angles to fall into for example the [0;360) interval by adding or subtracting a multiple of 360 degrees.
starblue
@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
@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
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.
@DalGr - You should add that to the question. @Guffa is right in this case.
butterchicken
I updated the explanation, I am sorry for the delay.
DalGr
+1  A: 

Isn't it simply

result = 180-(your angle)

Martin
A: 

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.

A: 

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.

DalGr