# Very very strange problem with trigonometry (SOLVED)

17 posts

 I don’t even know how to describe this problem other than my trigonometry is messed up and I dunno why. ```public function everyFrame() { calculateOffsets(); x += xOff; y -= yOff; } public function calculateOffsets() { xOff = Math.sin(rotation-90*(Math.PI/180))*mainClass.droneSpeed; yOff = Math.cos(rotation-90*(Math.PI/180))*mainClass.droneSpeed; }``` It’s a really simple and stock standard function, but it’s doing all kids of weird things. When the ship’s rotation is 0, it works fine. But if I make the rotation 1, it is rotated correctly, but moves in a direction that looks about -70 or so. On this demo you can see ships rotated at 0, 90, -90, and 180. Does anyone know what’s going on? -Yoshiyahu x = cos (adjacent over hypotenuse) y = sin (opposite over hypotenuse) Both should be x += xOff, y += yOff Order of operations entails (rotation-90) should be in parenthesis or it will always be 90*whatever ```xOff = Math.sin((rotation-90)*(Math.PI/180))*mainClass.droneSpeed; yOff = Math.cos((rotation-90)*(Math.PI/180))*mainClass.droneSpeed; ``` Rotation is in degrees, while you are stuffing it outside multiplication, causing sin() finction to interpret it in radians. 90 radians mod 2*PI is about 2.03 radians, and you are subtracting PI/2 out of it, making the result close to PI/6, something that’s seen in here. (My maths might be off a little, tho) Also, by all means avoid allocating new TextFields per frame, it causes the game to stutter every second here. It was the parenthesis. That’s it. I feel like an idiot. Originally posted by vesperbot:Also, by all means avoid allocating new TextFields per frame, it causes the game to stutter every second here. Yeah we just had a big discussion about that on another thread… :/ Wait how does this work when you have sin with x and cos with y. Is it because your `-90`? Originally posted by UnknownGuardian:Wait how does this work when you have sin with x and cos with y. Is it because your `-90`? No, that’s how I’ve always done it. It’s the same with my bullets. Originally posted by UnknownGuardian:Wait how does this work when you have sin with x and cos with y. Is it because your `-90`? Sure enough, didn’t you know, sin(x+PI/2) == cos(x), and cos(x+PI/2) == -1*sin(x) :D Yeh. Take out the `-90` and swap the x and y and do a `+=` for your yOffset and you should end up. Cause `cos` is just `sin` shifted by pi/4 (aka 90) EDIT Ban vesperbot for posting above me. I should edit my quote out of everyone’s post and then confusion will ensue! EDIT x2: List of excuse(s): Had my Calc 2 final yesterday. I’m done with math for 1 month. Originally posted by UnknownGuardian:Yeh. Take out the `-90` and swap the x and y and do a `+=` for your yOffset and you should end up. Cause `cos` is just `sin` shifted by pi/4 (aka 90) EDIT Ban vesperbot for posting above me. I should edit my quote out of everyone’s post and then confusion will ensue! EDIT x2: List of excuse(s): Had my Calc 2 final yesterday. I’m done with math for 1 month. I had no idea. I have always done it the other way. (Just for the record, I had to take away 90, not add it, so I ended up with (rotation-180) Dont forget that because flash’s coordinate system is upside down on the y axis (y increases as you move down) that you switch cos and sin vx = Math.cos(angleRads) * speed; vy = Math.sin(angleRads) * speed; You don’t switch sin and cos because the “coordinate system is upside-down.” sin has always been related to the y coordinate, and cos has always been related to x. Besides, rotation is inverted too (adding to rotation turns the object clockwise), so you actually don’t have to do anything at all to compensate. And it’s not “upside-down”. >_> Originally posted by player_03:You don’t switch sin and cos because the “coordinate system is upside-down.” sin has always been related to the y coordinate, and cos has always been related to x.Besides, rotation is inverted too (adding to rotation turns the object clockwise), so you actually don’t have to do anything at all to compensate. swapping sin/cos allows you to correct for the differences between typical math grids and flash’s grid without adding 90 degrees; also, 0 degrees is facing → Originally posted by Senekis93:And it’s not “upside-down”. >_> positive Y is down, not up: `(1, 1)` is one right, one down It’s not actually a difference between “typical math grids and flash’s grid”. It’s simply two conventions for angles: The trig convention, in which +x is to the right, +y is up, 0 is along +x and increasing angles (in radians) go in the trigonometric (counter-clockwise) direction. The compass convention in which north is up, 0 is north and angles (in degrees) increase in the clockwise direction. Half the problems stem from Flash using the compass convention for the rotation property and the trig convention for its Math library. The other half comes from direct referentials in math being the usual convention but the screen referential in computer science being indirect. I really hope we make contact with intelligent extraterrestrials within my lifetime. Watching scientists try to explain how we got to using the conventions we use will be hilarious (and don’t even get me started on our units of time.) Originally posted by Ace_Blue:It’s not actually a difference between “typical math grids and flash’s grid”. It’s simply two conventions for angles: The trig convention, in which +x is to the right, +y is up, 0 is along +x and increasing angles (in radians) go in the trigonometric (counter-clockwise) direction. The compass convention in which north is up, 0 is north and angles (in degrees) increase in the clockwise direction. Half the problems stem from Flash using the compass convention for the rotation property and the trig convention for its Math library. The other half comes from direct referentials in math being the usual convention but the screen referential in computer science being indirect. I really hope we make contact with intelligent extraterrestrials within my lifetime. Watching scientists try to explain how we got to using the conventions we use will be hilarious (and don’t even get me started on our units of time.)Metric or bust. Originally posted by Ace_Blue: I really hope we make contact with intelligent extraterrestrials within my lifetime. Watching scientists try to explain how we got to using the conventions we use will be hilarious (and don’t even get me started on our units of time.) Ah, but your observation raises an interesting question: Why seek intelligent life from other planets when we have so little evidence of it on ours? :P Originally posted by Ace_Blue:Half the problems stem from Flash using the compass convention for the rotation property and the trig convention for its Math library. 1) Draw a spaceship pointing to the right. 2) Add this code in the ship’s `ENTER_FRAME` handler: ```var r:Number = Math.atan2(parent.mouseY - y, parent.mouseX - x); rotation = r * 180 / Math.PI; x += Math.cos(r); y += Math.sin(r); ``` 3) Watch in awe as the ship both points towards the mouse and moves in the direction it’s pointing, all without swapping sin/cos, adding 90 degrees, or otherwise trying unnecessarily hard to compensate for perceived inconsistencies! (Besides radians/degrees, of course. That is a very real inconsistency that you do have to compensate for.)