tags:

views:

141

answers:

5

I am no mathematician, but I somehow got into game development as a hobby.

Having never studied anything beyond basic math, I have a lot of trouble figuring out how to reverse the angle of something, facing to the opposite direction, among the X axis. One image says more than 1000 words though (specially uneducated words): http://img156.imageshack.us/i/wihwin.png/

I basically want to reverse the direction of cannon objects adhered to a robot. When the robot changes from facing right to facing left, I do (180 - angle) as everyone suggested me, but it reverses the angle...literally, making the cannons aim up when they are aiming down. So, I need to do something else, but it escapes my knowledge.

Anyone would be so kind to help me with this? Oh, I use regular C by the way, in case there's something built-in specific to it.

To put it in other words, I work in 2D, so I want an angle that is facing right to face left. 0 being "totally to the right", 180 "left", 90 "up" and 270 "down". I want something that is aiming with an angle of 91 to turn into 89 when reversed, literally. There's no Z axis present.

EDIT: Thanks for the answers! Trying them out now. I'll post which one worked in a minute!

FINAL EDIT: -angle seem to work, seems I wronged somewhere else and got the right impression about 0 being "right". My apologies. Thanks everyone for your answers!

+1  A: 

It depends on how you are defining your angle. If you define it relative to the X axis then the angle is indeed just (180 - alpha).

Paul R
Yes, but unlike in the image I added, I get the red line into the lower quadrant instead...
MissHalberd
@MissHalberd: this is probably because you are not actually defining your angle relative to the X axis as shown in your diagram. I expect you may be defining it relative to the Y axis ?
Paul R
+1  A: 

Looking at your diagram, the angles you've marked are the same - you've simply changed the starting point for them. If you actually intended to measure the angle so that 0 deg is straight up, then it's 360 - x

Thus if you have aimed at 45 degrees, when you reverse it is 360-45 = 315 degrees

Paul Dixon
Oh my, I intended for the center to be common, I should have drawn arrows on them, but it'd look like "speed" instead...
MissHalberd
A: 

Where does your zero degree angle point, and where does 90 degrees point?

If zero is straight up then you could just do -1 * angle.

Blorgbeard
+1  A: 

In answer to your edit what you want then is

-( x - 90 ) + 90

i.e.

180 - x

Of course you will likely be working in radians and not degrees if you are using the standard C trigonometric functions so that would actually be

M_PI - x

Basically this breaks down into three steps

  1. ( x - 90 ) adjusts your angle so that the zero point is at 90 degrees.
  2. Negating this then flips the transformed angle.
  3. Add 90 back on to transform back to the original angle range.

Edit: Just noticed this is the same as @Paul R but you didn't seem to think that was correct?

Troubadour
+1  A: 

This is quite tricky to answer without knowing a bit more about how the cannons are defined in your game, but I'll try to give some pointers.

It sounds like your cannon is viewed from the side, and you are wanting it to turn around from right to left but keeping the cannon facing up. The calculation depends on which direction 0 is, and whether the angles run clockwise or anticlockwise.

If the angle of 0 has the cannon pointing straight up, then the angle is measured from straight up, clockwise. Therefore, the reverse angle will be -angle. If negative angles don't work then use (360-angle).

If the angle of 0 has the cannon pointing to the right and an angle of 45 points to the bottom right, then the upward facing cannon angles are from 180 to 360 with 270 being straight up. Therefore, to reverse an angle, you'd use (540-angle).

If the angle of 0 has the cannon pointing to the right but an angle of 45 points to the top right, then the cannon angles are from 0 to 180. To reverse the angle, use (180-angle).

I hope that helps! Lee.

Lee Oades
"-angle" worked! At least in the rendering aspect. I need to check if the engine considers the object as aiming in the right direction.It is odd though, I swear that thrusting an object with angle 0 makes it go right instead of up. I will experiment further, thank you!
MissHalberd