Page 4 of 6 FirstFirst ... 23456 LastLast
Results 31 to 40 of 56

Thread: OpenGL 3.x Rotation Problem

  1. #31

    Re: OpenGL 3.x Rotation Problem

    I have been trying to include the direction vector in my model matrix, but no success. What can be wrong with this code?
    [code=delphi]
    function TBrainObject.GetMatrix: TBrainMatrix;
    var
    M: TBrainMatrix;
    V: TBrainVector;
    begin
    if UpdateNeeded then
    begin
    M := Mat4CreateRotationEuler(FRotation);
    V := Mat4ApplyToVector(M, Vec3(0.0, 0.0, -1.0));
    V := Vec3Normalize(V);

    CachedMatrix := Mat4Scale(Mat4Translate(M,
    Vec3Negate(Vec3Add(FPosition, V))), FScale);

    UpdateNeeded := False;
    end;
    Result := CachedMatrix;
    end;
    [/code]

    What happens is that the camera doesn't include the new direction after rotation and instead moves on the same axes. Here's a video for better explanation: http://www.youtube.com/watch?v=lOL8sqR5fQ0

    Do you happen to know what is wrong?

    EDIT
    Hm, I also thought of using a LookAt function. What do you think?
    @User137: Can you post the code of the CreateMatrix function here?

  2. #32

    Re: OpenGL 3.x Rotation Problem

    Can you elaborate? What does the Direction vector have to do "exactly"?

    I think a lookAt function is a must have. Almost everyone uses one.
    There are three things you need to know about your camera:
    - Position
    - LookAt
    - UpVector (Denotes the "up" direction)
    Simply pass these three to a lookAt function and you should be all good.

    http://pyopengl.sourceforge.net/docu...LookAt.3G.html
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #33

    Re: OpenGL 3.x Rotation Problem

    The vector specifies what direction the object should move at. By default, the vector is (0,0,-1), but after rotation it should change along with the rotation angles.

  4. #34

    Re: OpenGL 3.x Rotation Problem

    You can see the whole 3D unit here, didn't paste the 2D math but you can figure if needed functions out i suppose, or ask:
    http://pastebin.com/gtLhUvj5

    [pascal]// Create a rotation matrix
    function CreateMatrix(const x, y, z: TVector): TMatrix;
    begin
    result[0,0]:=x.x; result[0,1]:=x.y; result[0,2]:=x.z; result[0,3]:=0;
    result[1,0]:=y.x; result[1,1]:=y.y; result[1,2]:=y.z; result[1,3]:=0;
    result[2,0]:=z.x; result[2,1]:=z.y; result[2,2]:=z.z; result[2,3]:=0;
    result[3,0]:=0; result[3,1]:=0; result[3,2]:=0; result[3,3]:=1;
    end;

    // Rotation matrix written with rows and columns switched
    function CreateMatrix2(const x, y, z: TVector): TMatrix;
    begin
    result[0,0]:=x.x; result[1,0]:=x.y; result[2,0]:=x.z; result[3,0]:=0;
    result[0,1]:=y.x; result[1,1]:=y.y; result[2,1]:=y.z; result[3,1]:=0;
    result[0,2]:=z.x; result[1,2]:=z.y; result[2,2]:=z.z; result[3,2]:=0;
    result[0,3]:=0; result[1,3]:=0; result[2,3]:=0; result[3,3]:=1;
    end;

    function CreateTranslateMatrix(const p: TVector): TMatrix;
    begin
    result:=NewMatrix;
    result[3,0]:=p.x; result[3,1]:=p.y; result[3,2]:=p.z;
    end;[/pascal]

    Edit: I changed the code a bit to make everything click. LookAt function has to use CreateMatrix2 that uses opposite row direction while usual operations such as MatrixOnPlane() use CreateMatrix.

  5. #35

    Re: OpenGL 3.x Rotation Problem

    I played with your LookAt yesterday and it seems to work, but there's still a little bug I'm going to track down. Something is calculated wrong, because the object slightly rotates with the camera and when the camera angle is high enough, the triangle disappears.

    Clearly, it's a problem with matrices. I suppose the function which returns the direction vector from a matrix is faulty. Here is the code:
    [code=delphi]
    { .: Mat4ApplyToVector :. }
    function Mat4ApplyToVector(const M: TBrainMatrix; const V: TBrainVector): TBrainVector; inline;
    begin
    Result.X := V.X * M.M[0, 0] + V.Y * M.M[1, 0] + V.Z * M.M[2, 0] + M.M[3, 0];
    Result.Y := V.X * M.M[0, 1] + V.Y * M.M[1, 1] + V.Z * M.M[2, 1] + M.M[3, 1];
    Result.Z := V.X * M.M[0, 2] + V.Y * M.M[1, 2] + V.Z * M.M[2, 2] + M.M[3, 2];
    end;

    { .: Mat4GetDirectionVector :. }
    function Mat4GetDirectionVector(const M: TBrainMatrix): TBrainVector; inline;
    var
    M1: TBrainMatrix;
    E: TBrainEuler;
    begin
    Result := Vec3(0.0, 0.0, -1.0);

    E := Mat4GetRotationEuler(M);
    M1 := Mat4CreateRotationEuler(E);
    Result := Mat4ApplyToVector(M1, Result);
    Result := Vec3Normalize(Result);
    end;
    [/code]

    Or maybe the problem is in the way I use them? I'm not sure. Anyway, if someone can tell me whether these two functions are correct, I'll be glad.

    I think I'm very close to solving the problem. Of course once I'm done with that, I'll post a complete source code here.

  6. #36

    Re: OpenGL 3.x Rotation Problem

    I just doublechecked the camera by looking both directions along all X,Y,Z-axis and their mid-angles and all worked fine. First with up-vector(0,1,0) and then vector(-0.2,0.93,0.2) (normalized).

  7. #37

    Re: OpenGL 3.x Rotation Problem

    Which vector did you normalize?

  8. #38

    Re: OpenGL 3.x Rotation Problem

    Everything should be normalized unless you absolutely know that it already is. Scaling matrix is exception to that.

    I mean, i sometimes test with even vectors like norm(vector(30,50,-70)). Just to vaguely point them somewhere.

  9. #39

    Re: OpenGL 3.x Rotation Problem

    Here's a demo of the framework: http://pateman.net76.net/BrainEngineDemo.rar
    Can you tell me whether the camera works correctly in your opinion?
    Edit
    W,S,A,D - move around
    Left key, right key - rotate the camera

  10. #40

    Re: OpenGL 3.x Rotation Problem

    Looks perfectly normal to me, but oh the colors hurt my eyes

Page 4 of 6 FirstFirst ... 23456 LastLast

Bookmarks

Posting Permissions

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