1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Rotate object in space to direction [Simulation]

  1. Apr 7, 2014 #1
    1. The problem statement, all variables and given/known data


    I'm writing an AI simulation for am upcoming assessment in my computer programming course, and I'm having some trouble with the physics and calculations.

    The simulation is of a dogfight in space, in 3D. I am calculating each objects' position as a vector, by adding velocity to its position. The velocity is based off of its acceleration, which is based off of its force divided by mass, like so (everything but the mass is a 3D vector):


    When I want to move the object, I call an 'AddForce' function, which requires another 3D vector - if I want to move forward, I pass in a vector such as (0, 0, 50), which provides 50N along the z axis.

    This is the same as my rotation:


    Same deal, using an AddTorque function, where the amounts for each number of the vector represent the rotation around that vector, i.e. pitching forwards by 50 units of torque (don't know the term), might be (50, 0, 0), around the X axis.



    I'm trying to write the AI systems for flight, and need to calculate some information. For example, if I tell the AI to fly to position A, it needs to know when to turn around and counter-thrust (i.e. halfway).

    More importantly, though, I need to be able to rotate the craft to face a certain direction. I'm currently trying to break it down axis-by-axis, where if the craft's X axis of rotation is above the desired direction vector's X component, it adds a torque to try to correct it. But this is clumsy, and results in rubber-banding.

    How can I calculate the required torque to rotate the craft to face a direction, including the counter-thrusting to reach said rotation with a final angular velocity of 0. I.e. it can't over-shoot.

    I don't have the maths chops to solve the required equations simultaneously.

    2. Relevant equations

    Not sure.

    3. The attempt at a solution

    In C++:
    Code (Text):
    glm::vec3 angle = glm::eulerAngles(physics->GetRotation());
        glm::vec3 diff = angle - glm::normalize(a_targetPosition - position);

        if (diff.x > 0)
            physics->AddTorque(glm::vec3(-0.1, 0, 0));
        else if (diff.x < 0)
            physics->AddTorque(glm::vec3(0.1, 0, 0));

        if (diff.y > 0)
            physics->AddTorque(glm::vec3(0, -0.1, 0));
        else if (diff.y < 0)
            physics->AddTorque(glm::vec3(0, 0.1, 0));

        if (diff.z > 0)
            physics->AddTorque(glm::vec3(0, 0, -0.1));
        else if (diff.z < 0)
            physics->AddTorque(glm::vec3(0, 0, 0.1));
    Basically, calculating the direction between the craft's position, and the desired position, as a vector, and reacting to each component one by one. This is insane, though, and fails to reach the desired heading.

    Can anyone offer any advice? This is crucial to my assessment, but not an actual part of it, so it's not so bad to ask for assistance.

    - HateDread.
    Last edited: Apr 7, 2014
  2. jcsd
  3. Apr 7, 2014 #2


    User Avatar
    Science Advisor
    Homework Helper
    2017 Award

    Tough subject.

    You already use the term Euler angles, so I take it you studied Wiki and perhaps also this one and/or this one and have a way to get a rotation matrix.

    But I 'm not sure, because it doesn't look as if you realize that rotations don't commute: rotation 1 after rotation 2 is different from 2 after 1 when they have different axes.

    You want to dogfight in space, so perhaps you can reduce to 2D (no interest in roll) to start with. And in order to not disorient the prospective pilots, I would align the main thrust with his/her line of sight rather quickly while heading off somewhere.

    Conversely, when engaging, the crosshairs need to point at your targets, so the trajectory has to take second place: you want to approach from a certain direction, which requires a lot of trajectory planning (much more than just going from A to B).

    Not much help, I realize.

    But for physics: torque is force times distance; Newton times meters in SI units.
  4. Apr 7, 2014 #3
    Hi BvU,

    I do realise the nature of rotations. I guess I just hadn't expressed that point. And yes, I'm using matrices for my rotations (constructed out of euler angle vectors, just so I can work with them before converting them to the matrix).

    Yepp, that's the plan re. heading. For now, just dumb pointing right at the enemy, despite their velocities.

    The part I'm having trouble with is that of the initial post - figuring out how to rotate, even around one axis, with a final velocity of 0.

    Thanks for the ideas :)
  5. Apr 7, 2014 #4


    User Avatar
    Science Advisor
    Homework Helper
    2017 Award

    Equations are just like for spatial coordinates: if you apply a constant torque ##\tau = I\alpha##, you get a constant angular acceleration, e.g ##\vec\alpha = (0,0,\alpha_z)##, then with ##\vec\alpha = \dot{\vec\omega}\quad \Rightarrow \omega = \omega_0 + \alpha\,t \quad## (##\omega_0## and ##\omega## both in the z direction). Integrate once more: ##\phi = \phi_0 + \omega_0\, t + {1\over 2} \alpha_z\,t^2##.
  6. Apr 7, 2014 #5
    Thanks for the reply, BvU.

    I'm terribly sorry, but could you please walk me through these equations? I've forgotten my high-school physics, and even then we didn't use a few of these symbols. What's going on here?

  7. Apr 7, 2014 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You have unknowingly bitten off a bit more than you can chew, HateDread. Controlling the attitude of a spacecraft in 3D is typically the subject of an entire graduate level class in aerospace engineering and is the subject of entire books.

    A graduate level class: http://cuengineeringonline.colorado.edu/coursedb/view-course/141 [Broken]
    A book: http://books.google.com/books?hl=en&lr=&id=xDzEkslhpUwC&oi=fnd&pg=PP1#v=onepage&q&f=false.

    An overly simplified algorithm:
    1. Find the eigenrotation from orientation A to orientation B.
    2. Provide torque to rotate around this eigenaxis.
    3. A bang-bang approach is to
      • Provide a bit of torque at the start to achieve an angular velocity in the desired direction.
      • Coast (with minor corrections to keep the angular velocity constant) until it is time to undo this initial torque.
      • Provide a bit of torque at the end to zero out the angular velocity just when the vehicle reaches the desired orientation.
    4. A minimal time approach is to
      • Continuously provide maximal torque to accelerate rotationally along this axis until the halfway point.
      • Continuously provide maximal torque to decelerate rotationally along this axis until reaching the desired orientation.
      • Correct for deviations along the way. The Euler torque will cause deviations even if you have perfect effectors.
      • Good luck providing maximal torque and correcting at the same time. The two concepts don't mix well.
    Last edited by a moderator: May 6, 2017
  8. Apr 7, 2014 #7
    Hey DH,

    Yeah, I was aware of this when I actually sat down to think about it. Your post helps a lot though - I'm going for a very shoddy setup, and can take shortcuts with ease, as I control the environment (physics included). If worst comes to worst, I'll keep the forces for lateral movement, but remove all of the rotational physics.

    "1. Find the eigenrotation from orientation A to orientation B.
    2. Provide torque to rotate around this eigenaxis."

    With these points, what do you mean by eigen rotation? Just rotation using euler angles? If I have a vector representing my rotation, A, and a direction between my position and my desired position, B (normalize(desired position - current position)), would (B - A) represent this eigen rotation, from current heading -> required heading?

    Thanks a lot!
    Last edited by a moderator: May 6, 2017
  9. Apr 7, 2014 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    *Never* use Euler angles. Well, almost never. They're handy for human understanding. They are extremely unhandy for almost anything else. I suggest using orthogonal matrices or unit quaternions to represent orientation. There are other options, but don't use Euler angles.

    By eigenrotation I mean the rotation that takes you from orientation A to orientation B. That such a rotation exists is Euler's rotation theorem. Rotation in three dimensional space is a mathematical group, the group SO(3). This rotation from orientation A to orientation B in three dimensional space can be viewed as a rotation about a single axis. This single axis rotation is another consequence of Euler's rotation theorem.

    Aside: rotation in any N-dimensional space is a mathematical group, the group SO(N). Another aside: That the eigenrotation can be viewed as being around an axis is not true in general in higher dimensional spaces, but fortunately we live in a three dimensional world.
  10. Apr 7, 2014 #9
    I have heard of angle-axis stuff before, but is it not a rotation around each axis by a rotation amount, just like with Euler angles? I'm afraid I'm not understanding the difference. What form does this eigenrotation take?
  11. Apr 7, 2014 #10

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Suppose you want to rotate a reference frame about an axis ##\hat{\mathbf u}## by an angle ##\phi##. The transformation matrix that transforms a vector as represented in the original frame to its representation in the rotated frame is given by ##T_{ij} = cos\phi\,\delta_{ij} + (1-\cos\phi)\,\hat u_i \hat u_j + \epsilon_{ijk}\sin\phi\,\hat u_k## where ##\delta_{ij}## is the Kronecker delta, ##k## is the third axis (i.e., neither i nor j), and ##\epsilon_{ijk}## is the Levi-Civita symbol (see http://en.wikipedia.org/wiki/Levi-Civita_symbol#Three_dimensions).

    You want to invert this: Given a transformation matrix, what is the eigenaxis ##\hat{\mathbf u}## and eigenangle ##\phi## that generates this matrix?

    From the above, the trace of the matrix and the difference between pairs of off-diagonal elements are ##\mathrm{tr}(T) = 2\cos\phi + 1## and ##T_{ij}-T_{ji} = 2\epsilon_{ijk}\sin\phi\,\hat u_k##. This immediately gives the eigenangle (the amount by which you want to rotate) and the eigenaxis (the axis about which you want to rotate).

    Note: There are multiple alternate expressions for computing the eigenrotation from a matrix, all of which are equivalent in infinite precision arithmetic. They are not equivalent when using finite precision arithmetic. The above is but one way to compute the eigenaxis and eigenangle. The other approaches are more complicated.
    Last edited: Apr 7, 2014
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted