开发者

Relative camera rotations?

I currently have this update code set up to rotate and move my camera.

float move = 0.5f;
        float look = 0.01f;


        //Rotation
        if (Keyboard[Key.Left]) {

            modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationY (-look));
        }
        if (Keyboard[Key.Right]) {
            modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationY (look));

        }
        if (Keyboard[Key.Up])
        {
            modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationX (-look));
        }
        if (Keyboard[Key.Down])
        {
            modelview = Matrix4.Mult (modelview, Matrix4.CreateRotationX (look));
        }

        //Movement
        if (Keyboard[Key.W])
        {
            modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (0f, 0f, move));
        }
        if (Keyboard[Key.S]) {
            modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (0f, 0f, -move));
        }
        if (Keyboard[Key.A])
        {
            modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (move, 0f, 0));
        }
        if (Keyboard[Key.D]) {
            modelview = Matrix4.Mult (modelview, Matrix4.CreateTranslation (-move, 0f, 0));
        }

When I move my camera simulates the effect of rotating around the world origin(0,0,0) rather than its current position.

My model view matrix is loaded 开发者_开发技巧like so:

            GL.MatrixMode (MatrixMode.Modelview);
        GL.LoadMatrix (ref modelview);


There's a lot to be done with this code. Long story short:

I believe that your code is a part of a rendering loop?

You'd need to:

  • replace move with 3 variables, for movement in x, y, z directions,
  • replace look with 2 variables, yaw for left-right look and pitch for up-down look, google for "Euler angles" for more theory on this;
  • move all those outside the loop - they need to persist between frames.

After that, in each frame, you're supposed to:

  • update those variables (increase or decrease by a constant value multiplied by delta time between 2 frames) according to the input,
  • replace the model-view matrix with identity matrix (LoadIdentity()),
  • multiply the model-view matrix by translation by (x,y,z),
  • multiply by rotation in X-plane by yaw,
  • multiply by rotation by Y-plane by pitch.

To sum up, you need to separate the input handling from matrix handling, and then each frame set the correct model-view matrix (as a set of transformations) basing on current position and direction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜