|
Post by Draxxarguest on May 25, 2005 0:44:25 GMT -5
On the old board I saved a copy of the following post (as I sort of like collecting formulas for some reason) and had some questions on it: --begin quote----: subject: "2D Angle Between 2 Objects" rad = Math.atan2(targety-originy, targetx-originx); deg = Math.round((rad*180/Math.PI)); rot = deg+90; Assuming your origin graphic is pointed up at 0 degrees. rot = _rotation, or the rotation (360) of your graphic, which will point it towards the target. originx/y and targetx/y is the x and y pixel coordinates of your origin and target objects respectively. ----end quote---- My question is how does the atan2 function work exactly? Does it sort out the "arctangent problem" mentioned here: hyperphysics.phy-astr.gsu.edu/hbase/ttrig.html#c3..in other words does it handle figuring out what quadrant the angle is in case arctangent returns a negative number?
|
|
|
Post by Draxxarguest on May 28, 2005 12:54:40 GMT -5
Basically my question was what would the formula be for regular tangent (Math.atn vs. Math.atn2).
I wrote up some code that seems to handle it, but it's much longer and a little buggy perhaps.
// Cartesian Coordinate System (Flash has an inverted y!) //.................-y.............. //..................|............... //............2....|...1.......... // x-_______|_______x+ //..................|.............. //............3....|...4th quadrant //..................|.............. //.................+y............
// translate to 0,0 // this is so you can find out what quadrant(1-4) the // originx/y ship is in originx = originx - targetx; originy = originy - targety; targetx = 0; targety = 0;
// handle div. by zero if((targetx - originx) ==0){ rad = Math.atan((targety-originy) / .0001); }else{ rad = Math.atan((targety-originy) / (targetx-originx)); } deg = rad * (180/Math.PI);
quadrant = 0; if (originx> 0 and originy < 0) { quadrant = 1; // angle is between 0 and 90 degrees deg = deg - 90; } if (originx < 0 and originy < 0) { quadrant = 2; // angle is between 90 and 180 degrees deg = (deg * (-1)) + 180; } if (originx < 0 and originy> 0) { quadrant = 3; // angle is between 180 and 270 degrees deg = deg + 90; } if (originx > 0 and originy > 0) { quadrant = 4; // angle is between 270 and 360 degrees (or 0 again) deg = deg + 270; } // handle x or y = 0 if (originx == 0 and originy > 0){ deg = 270; // angle is pointing straight down } if (originx == 0 and originy < 0){ deg = 90; // angle is pointing straight up } if (originx > 0 and originy == 0){ deg = 0; // angle is pointing to the right } if (originx < 0 and originy == 0){ deg = 180; // angle is pointing left }
rot = deg;
this._rotation = rot;
|
|
|
Post by Draxxarguest on May 28, 2005 13:17:49 GMT -5
Just figured out the bug I had in the code above... The line following "quadrant = 2" should say: deg = deg + 90; ...likewise the "quadrant = 4" line could be simplified to: deg = deg - 90; .... vs. deg = deg + 270... but it's the same result.
I know the original code is much simpler. I just like to figure out things the hard way I guess. Not all progamming languages have the atn2 function built in (especially old languages). So I like to back engineer what a function does sometimes, just for wider compatability to other languages.
|
|