need angle calculation help

January 5th, 2009
  • object 1 is at x1,y1,z1 and object 2 is at x2,y2,z2.I need to find what angles object 1 is to object 2.I making simple free program I making.I need it to help me with the program for stuff like what angle would object 1 need to go toward object 2


  • Hi there, There are a several ways to answer this question, so I will give you one of the simplest. I actually had to research problems like this (in 2D though) for the programming contests at http://www.topcoder.com not long ago. We are given two points P1 and P2 in Euclidean three-dimensional space at coordinates (x1,y1,z1) and (x2,y2,z2) and we want to find the angle between the rays O-P1 and O-P2 where O is the origin. Let theta be this angle. Then it is a theorem that the inner product of P1 and P2, see http://mathworld.wolfram.com/DotProduct.html, that P1 dot P2 has value P1 P2 cos theta The inner product of P1 and P2 is: x1 x2 + y1 y2 + z1 z2 And the norm of a vector is the square root of the sum of the squares of its coordinates (this works also in any number of dimensions) Thus, here is how one might solve this in pseudocode double angle (double P1, double P2){ //compute angle between P1 and P2 double P1norm=0; // P1 double P2norm=0; // P2 for (int i=0;i<3;i++)P1norm+=P1[i]*P1[i]; P1norm=sqrt(P1norm); // Get P1norm for (int i=0;i<3;i++) P2norm+=P2[i]*P2[i]; P2norm=sqrt(P2norm); //Get P2norm double epsilon=.000000000000001 //some very small positive value, depending on machine precision and application details if (P1norm

  • I will try to clarify what I am looking for better.I make will startrek like. object 1 is enterprise d.it is located at x1, y1, z1. object 2 is Romulan warbird that just decloaked inside federation space. it is located at x2,y2,z2 The enterprise d wants to move toward the romulan warbird.The helmsman needs to know the following angles: 1 the angle that how left or right


  • I will try to clear up what I am looking for.I will startrek like. object 1 is enterprise d. it located at x1, y1, z2 object 2 is romulan warbird in federation space.it is located at x2,y2,z3 of course the enterprise d want to go to the romulan warbird.The helmsman needs the following info. 1 the angle saying how left or right enterprise d needs to be go to strait toward the romulan warbird. 2 the angle saying how up or down the enterprise d needs to go strait toward the romulan warbird. Ps the weapons officer may need the info also. Since I am darkbasic for the programming language it does not appear to have inverse sin or inverse tan.They have the following math commands. SQRT() will return the square root of an expression ABS() will return the positive equivalent of an expression INT() will return the largest integer before the decimal point RND() will return a random number within a given range EXP() will return a number, raised to the power of a value COS() will return the cosine of a value SIN() will return the sine of a value TAN() will return the tangent of the a value ACOS() will return the arccosine of a value ASIN() will return the arcsine of a value ATAN() will return the tangent of a value ATANFULL() will return the angle between points HCOS() will return the hyperbolic cosine of a value HSIN() will return the hyperbolic sine of a value HTAN() will return the hyperbolic tangent of a value


  • First, the arc cosine, ACOS, is the same function as "inverse cosine". These are just different terms for the same thing. Second, in all these computations I am going to assume that the objects reside in Euclidean 3-dimensional space. In fact the topology of the universe may be different from Euclidean three-space (it might be a Riemannian manifold) in which case the computations become more complex because the meaning of "straight line" changes (by analogy, think of a ship on the surface of the Earth trying to go from point A to point B: the ship cannot simply "point towards" A and B, since it must travel along geodesics, great circles.) Third, I am going to assume that both objects are at rest relative to one another. If Object 2 is moving at some rate then the Enterprise may wish to aim at a location such that the Enterprise arrives to where the object is at the time the Enterprise arrives; but I will neglect this. Similarly, if the Enterprise is moving at some rate then I will neglect the time it takes the Enterprise to change its orientation and momentum vector. Fourth, there is not quite enough information given to solve the problem. Specifically, you will need the orientation of the Enterprise. Let me explain. Suppose the Enterprise is at (0,0,0) and the other ship is at (1,0,0). Then if the Enterprise is already pointing in the direction of (1,0,0) then clearly the Enterprise does not need to turn at all. On the other hand, if the Enterprise is facing opposite to (1,0,0) then the Enterprise must perform a 180-degree rotation about, for example, the z-axis. Now suppose we idealize the Enterprise is a small directed line segment from endpoint P1_a to P1_b representing the stern and the bow of the Enterprise. One might think that this would be sufficient to solve the problem, since one could determine how much to rotate the line segment about lines parallel to the coordinate axes and throught the center of mass of the Enterprise. However, remember that (presumably) the coordinate system used by the helmsman of the Enterprise will be the natural coordinate system given by the Enterprise itself; that is, the helmsman can rotate about axes perpindicular to the line segment through Captain Picard, if Picard were standing straight up and facing forward. There are several ways one can use to denote the orientation of the Enterprise. If you like, you can tell me how you plan to represent this orientation, or I can choose some one and then give you the answer from that representation. Which would you prefer?


  • Well think I got it right.Well I plugged some numbers in it looked about what I think it should be.For example x1 ,y1, z1 where all 1 and x2,y2,z2 where all 2 and both angles showed up as 45 which I believe is correct.Below is source code I made for it.Does it look right to you? xdif = x2 -x1 ydif = y2 - y1 zdif = z2 - z1 if ydif > 0 then xyangle = atan(xdif/ydif) if ydif < 0 then xyangle = atan(xdif/ydif) + 180 h = sqrt( (xdif)^2 + (ydif)^2 ) if h > 0 then zangle = atan(zdif/h) if h < 0 then zangle = atan(zdif/h) + 180


  • We are actually discussing three separate issues here: 1. How does one usefully mathematically model the angular navigation commands of the Enterprise? 2. Given a mathematical definition of the problem in 1, how does one solve for the angles required? 3. Given a mathematical solution in 2, how do we actually code it? Regarding 1, one resource I used is: Star Trek: The Next Generation Technical Manual by Rick Sternbach and Michael Okuda (Pocket Books, 1991). Section 3.4 of that book discusses flight control. Pages 36-37 discuss navigation methods specifically. Six input modes for Conn are given: 1. Destination planet or star system. 2. Destination sector. 3. Spacecraft intercept (only when the target spacecraft has a tactical sensor lock). 4. Relative bearing. The flight vector is specified as azimuth/elevation relative to the current orientation of the Enterprise. 5. Absolute heading. Flight vector is specified as azimuth/elevation relative to the center of the galaxy 6. Galactic coordinates (XYZ). Presumably you are most interested in input modes 4 and 5 for the purposes of this question. As I pointed out in my previous answer clarification, however, in order to implement mode 4 you will need to keep track of the spatial orientation of the Enterprise: which way it is pointed and how much it is twisted about its axis. 6. This implementation would require a lot of additional information and would require some linear algebra to do, so you may not want to implement input mode 4. Input mode 5 does suggest that you can use absolute angles and so you do not need to keep track of the orientation of the Enterprise. My recommendation if you choose to use input mode 5 would be to convert both the Enterprise P1 and the target P2 into spherical coordinates. (see http://mathworld.wolfram.com/SphericalCoordinates.html ) . The spherical coordinates of a point [x,y,z], assuming the sphere has origin at the galactic center, is given by: r = sqrt(x^2 + y^2 + z^2) theta = ATAN(y/x) phi = ACOS (z/r) Here, theta is the "azimuthal angle", the angle in the xy-plane from the x axis, and phi is the "polar angle", the angle from the z-axis, formed by the ray O-[x,y,z]. The picture at the above URL is clearer than I can draw here as well. Anyway, get the azimuth and polar angles for the Enterprise at P1, say phi_1 and theta_1 Next, get the azimuth and polar angles for the target, say the Romulan warbird, at phi_2 and theta_2 using the above formulas. Now, the angles you need to rotate the enterprise are given by the XY rotation, the azimuthal rotation, of: theta_2 - theta_1 = A and the elevation angle, given by: phi_2 - phi_1 = E Thus, the instruction would be A mark E, where A and E are defined above. Finally, I will answer your specific request for clarification: You write: "For example x1 ,y1, z1 where all 1 and x2,y2,z2 where all 2 and both angles showed up as 45" I'm afraid I do not understand why in this case, in which P1, P2, and the origin are all collinear, that "both angles should show up as 45 degrees". Either you are using relative bearing or absolute heading (input modes 4 and 5 above). If you are using relative bearing, then the amount to rotate the Enterprise depends not only on x1,x2,x3 but also on the orientation of the Enterprise. Since you do not specify the orientation of the Enterprise, there is no way to know what its orientation is, hence, there is no way to know how much to rotate it. Your answer would be correct, however, if you are using relative bearing AND if the natural coordinate system defined by the Enterprise was parallel to the galactic coordinate system, but this is not necessarily the case (and doesn't make sense, since the Enterprise is going to turn around as soon as it executes the mark order anyway; that is to say, the Enterprise is going to be turning all the time). On the other hand, if you are using absolute bearing, then the Enterpise need not change its mark at all: it does not need to perform any azimuthal or elevation rotation, since its polar and azimuthal angles are identical to that of the target. Thus, since it remains uspecified what it is that your code intends to compute so I cannot comment on it (well, of course, h is never negative and you do not need to check for that case)


  • I just wanted to add a couple of other comments. You can find additional information on Star Trek cartography at http://www.stdimension.de/int/Cartography/IntroTools.htm . This pretty much agrees with my suggestion to use spherical coordinates with the origin at the galaxy center, which was taken from the Star Trek Next Generation Technical manual (op cit.) . This site has some additional detail on Star Trek cartography, including coordinates for numerous stars. For implementation, you might consider first writing a routine to convert galactic coordinates to spherical coordinates. For example, the point (1,1,1) should have spherical coordinates of r=sqrt(3), and azimuthal and polar angles each equal to 45 degrees, using the formulae in my previous clarification. Finally, one suggestion that you might consider, if you have not already done so, is first to build your system working for simplified 2-D Star Trek, and only then to add in the third dimension. I personally find it useful when developing programs to get some simple prototypes up first before tackling the full problem; I don't really have a reference for you on that, maybe http:///www.extremeprogramming.org .







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about need angle calculation help , Please add it free.