Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reflect an angle across the Y axis

Tags:

math

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, along the X axis & across the Y 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 literally reverses the angle, making the cannons aim up when they are aiming down. So, I need to do something else, but it escapes my knowledge.

To put it in other words, I work in 2D, so I want an angle that is facing right to face left. My angles are defined:

  • 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. There's no Z axis present. Anyone would be so kind to help me with this?

like image 475
MissHalberd Avatar asked Jun 15 '10 10:06

MissHalberd


2 Answers

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?

like image 186
Troubadour Avatar answered Oct 14 '22 18:10

Troubadour


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.

like image 43
Lee Oades Avatar answered Oct 14 '22 20:10

Lee Oades