| 01-15-2009, 08:15 PM | #1 |
I'm working on a little system but I need to get the phi.. the function that I've found contains a syntax error and I haven't found anything else then that. The function I've found: JASS:function AngleBetweenPointsPhi takes real x0, real y0, real z0, real x1, real y1, real z1 returns real local real x = x0 - x1 local real y = y0 - y1 local real z = z0 - z1 return Acos( z, SquareRoot ( ( x * x ) + ( y * y ) ) ) // The syntax error. (Gives it 2 argument while it only is able to use 1) endfunction Could someone please give me the "real" function / formula :) |
| 01-15-2009, 08:23 PM | #2 |
Acos takes only 1 argument but you're passing 2. About the other thing, give me some time to write a function. However if i see it correctly you want 2 angles. 1 for the the x/y plane and 1 for the z plane, right? |
| 01-15-2009, 08:24 PM | #3 |
Yes I do know that, but I do not know what I should put instead. Edit - Yep :) |
| 01-15-2009, 08:30 PM | #4 |
The X/Y Plane one provided by Blizzard uses Atan2(y_difference,x_difference), so you got the first wanted angle. The next one is propably the same with Atan2(z_difference,SquareRoot(x_difference*x_difference+y_difference*y_difference)) I guess that anwers your question, right? vJASS Code would look like this JASS:struct PlaneAngle real XYAngle = .0 real ZAngle = .0 static method create takes real x0, real y0, real z0, real x1, real y1, real z1 returns PlaneAngle local PlaneAngle d = PlaneAngle.allocate() local real xdif = x1-x0 local real ydif = y1-y0 local real zdif = z1-z0 set d.XYAngle = Atan2(ydif,xdif) set d.ZAngle = Atan2(zdif,SquareRoot(xdif*xdif+ydif*ydif)) return d endmethod endstruct ---EDIT--- Fixed |
| 01-15-2009, 08:48 PM | #5 |
I tried to use your function but the unit went straight up into the sky. Parts of my code: JASS:function AngleBetweenPointsPhi takes real x0, real y0, real z0, real x1, real y1, real z1 returns real local real xdif = x0-x1 local real ydif = y0-y1 local real zdif = z0-z1 return Atan2(zdif,SquareRoot(xdif*xdif+ydif*ydif)) endfunction ........... // IN the "init" function set .angle = Atan2((y - .y), (x - .x)) set .phi = AngleBetweenPointsPhi(.x, .y, .z, x, y, 0) .......... // The callback set p.x = p.x + p.speed * Sin(p.phi) * Cos(p.angle) set p.y = p.y + p.speed * Sin(p.phi) * Sin(p.angle) set p.z = p.z + p.speed * Cos(p.phi) When I use: JASS:set .phi = 90 * bj_DEGTORAD Then it works as it's supoosed to. |
| 01-15-2009, 09:10 PM | #6 |
Found the problem... it should be x1-x0, y1-y0 and z1-z0. Made my own test and it works proper then. |
| 01-15-2009, 09:49 PM | #7 |
or you could just do this: JASS:function AngleBetweenPointsPhi takes real x0, real y0, real z0, real x1, real y1, real z1 returns real local real x = x0 - x1 local real y = y0 - y1 local real z = z0 - z1 return Acos( z / SquareRoot ( ( x * x ) + ( y * y ) ) ) // The syntax error. (Gives it 2 argument while it only is able to use 1) endfunction |
| 01-15-2009, 10:11 PM | #8 |
No, Bobo, you'd still need to use Atan instead of Acos, also you'd have a potential threadcrashing division by zero in there. Atan2 is the right way to go. |
| 01-16-2009, 01:48 AM | #9 |
I beileive zdif^2 would be faster then taking sqrt(x^2+y^2), would it not? (Atan2(zdif^2, x^2 + y^2)) Square root is really slow so it's always good to avoid it where possible. |
| 01-16-2009, 11:09 AM | #10 |
The ratio between two numbers is not equal to the ratio between the squares of those two numbers so no, you can't do that. |
| 01-16-2009, 12:03 PM | #11 |
Now it works and the error was: JASS:// Not working function AngleBetweenPointsPhi takes real x0, real y0, real z0, real x1, real y1, real z1 returns real local real xdif = x0-x1 local real ydif = y0-y1 local real zdif = z0-z1 return Atan2(zdif,SquareRoot(xdif*xdif+ydif*ydif)) endfunction // Working function AngleBetweenPointsPhi takes real x0, real y0, real z0, real x1, real y1, real z1 returns real local real xdif = x1-x0 local real ydif = y1-y0 local real zdif = z1-z0 return Atan2(SquareRoot(xdif * xdif + ydif * ydif), zdif) // Switched the arguments endfunction |
| 01-16-2009, 12:09 PM | #12 |
They both work, it's just that the first one gives you the angle from the horizontal plane and the second one gives you the angle from a vertical line. |
