Results 1 to 9 of 9

Thread: How to rotate a vector with quaternion?(graphics, openGL)

  1. #1
    Join Date
    Feb 2009
    Location
    Hungary (GMT + 1)
    Posts
    1,774
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default How to rotate a vector with quaternion?(graphics, openGL)

    So it's not really a math problem, well, kinda ...

    I have a little problem with my openGL homework.
    I don't want to go into details so I go straight to the point.
    We want to simulate the movement of a helicopter which only moves towards it's nose. (yep, fuck physics we got enough of that part in the homework anyway)
    So first of course the helicopter is going forward and it's movement vector is (1, 0, 0).
    Now we use quaternions to roll/pitch/yaw the helicopter so we got the helicopters quaternion. R for example rolls it with 20°.
    The problem is that the formula we use to rotate the movement vector with the quaternion is not working/buggy.

    But here it is anyway:
    c++ Code:
    Quaternion temp(0, heli);
      //heliQ.Normalize();  //not sure if this is needed
      Quaternion a = heliQ.Inverse();
      Quaternion b = heliQ%temp;
      temp = b%a;
      heli = temp.GetAxis();
      glutPostRedisplay();

    heliQ being the quaternion, heli being the movement vector. We were kinda trying things so maybe it's not pretty. % operator is quaternionXquaternion.
    We wrote the inverse part too, we are quiet sure it's okay, but I post it too just in case:
    c++ Code:
    float abs = sqrtf(s*s+d.x*d.x+d.y*d.y+d.z*d.z);
        float abs2 = abs*abs;
        Vector c = d*-1;
        Quaternion temp(s, c);
        return temp*(1/abs2);
    d is the vector.
    We were also debugging this so it's split apart too.

    Thanks!

    p.s.: How was the code highlight? I remember there was a way to for example c++ highlight, but I don't remember how.

    p.s.s: And if you are really curious the physics part of the homework is to follow the helicopter with a camera that is bound to the helicopter by a rubber rope while it's also on a ballon with a normal rope. Balloon is affected by gravity, lift force(whatever in english), and air resistance. Good luck us ...
    Last edited by Sabzi; 12-04-2012 at 03:09 PM.

  2. #2
    Join Date
    Mar 2007
    Posts
    5,125
    Mentioned
    275 Post(s)
    Quoted
    901 Post(s)

    Default

    *Waits for Brandons post*

    Forum account issues? Please send me a PM

  3. #3
    Join Date
    Feb 2009
    Location
    Hungary (GMT + 1)
    Posts
    1,774
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by [J]ustin View Post
    *Waits for Brandons post*
    Well yea, I am also waiting for him, but who knows .
    Last edited by Sabzi; 12-04-2012 at 11:53 AM.

  4. #4
    Join Date
    Mar 2012
    Location
    127.0.0.1
    Posts
    3,383
    Mentioned
    95 Post(s)
    Quoted
    717 Post(s)

    Default

    Came here, was ready to flip a vector, thought you just needed to reverse the elements in it.

    Read post

  5. #5
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    Brandon taught me quite a bit about vectors as such. if you wanna move a vector in 3d space you have to apply a matrix to it.

    http://songho.ca/opengl/index.html

    that site will help you out alot. look into it.

  6. #6
    Join Date
    Feb 2009
    Location
    Hungary (GMT + 1)
    Posts
    1,774
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    What we are doing(trying) in the code I posted is what you say, but a bit simpler.
    Because you can transform the quaternion into a matrix.
    I will look into that link.

  7. #7
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    multiply the vector with this.



    I can also explain a 3D vector?

    also [highlight=c++]c++ tag:
    c++ Code:
    .
    Working on: Tithe Farmer

  8. #8
    Join Date
    Feb 2009
    Location
    Hungary (GMT + 1)
    Posts
    1,774
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Well, I think our idea was good(maybe even the code too) and I start to think that I make something horribly wrong(because I don't understand openGL all that much) at another place.
    I suspect this to be wrong: (horribly)
    c++ Code:
    Vector axis = heliQ.GetAxis();  //this is in onDisplay btw
      glPushMatrix();
        glTranslatef(push.x, push.y, push.z);
        glPushMatrix();
          glRotatef(heliQ.GetRotationAngle(), axis.x, axis.y, axis.z);
          drawChopper();
        glPopMatrix();
      glPopMatrix();

    Push being the vector I want to move the helicopter with. Calculated like so: push = push + movement vector. In simulateWorld(called by onIdle) with the time difference and all, but it's not yet implemented.(easiest part)
    I do realize it would be easier to draw the helicopter to a given x, y, z coordinate, but the whole modelling would fell apart then :S.

  9. #9
    Join Date
    Feb 2009
    Location
    Hungary (GMT + 1)
    Posts
    1,774
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    If anyone is interested here is the final "product".
    It was made in a rush because of tight deadlines and other finals to worry about hence I would not call it well-made.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •