Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Quaternion-based Camera problem

Started by bladerunner627 Nov 6, 2007 at 1:24 AM 0 replies 2.8k views
Original Post
bladerunner627
bladerunner627
I'm not sure if this is in the right forum or not, but it's the closest one I could find. I'm using MDX(yea, so sue me). I'm trying to make a Quaternion-based camera based off of this tutorial: http://www.gamedev.net/reference/programming/features/quatcam/ Anyways, I've got a good grasp on how Quaternions work but I still can't seem to get my Camera class to work properly. None of the rotations effect the camera AT ALL, it just keeps looking in the same direction. I don't think this is a programming issue, but more math related. Here's the Camera class:
using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace MDXTemplate
{
    public class Camera
    {
        private Vector3 Position;
        private Vector3 LookAt;
        private Vector3 UpDir;

        public Camera( )
        {
            Position = Vector3.Empty;
            LookAt = Vector3.Empty;
            UpDir = new Vector3( 0, 1, 0 );
        }

        public Camera( Vector3 position, Vector3 lookAt, Vector3 upDir )
        {
            Position = position;
            LookAt = lookAt;
            UpDir = upDir;
        }

        public void RotateCamera( float angle, Vector3 axis )
        {
            Quaternion rotation, lookAt, result;

            rotation = new Quaternion( );

            rotation.X = axis.X * (float) Math.Sin( angle / 2 );
            rotation.Y = axis.Y * (float) Math.Sin( angle / 2 );
            rotation.Z = axis.Z * (float) Math.Sin( angle / 2 );
            rotation.W = (float) Math.Cos( angle / 2 );

            lookAt = new Quaternion( LookAt.X, LookAt.Y, LookAt.Z, 0 );

            result = ( rotation * lookAt ) * Quaternion.Conjugate( rotation );

            LookAt = new Vector3( result.X, result.Y, result.Z );
        }

        //  Aspect Ratio is Window width/height.
        public void UpdateCamera( Device device, float aspectRatio )
        {
            device.Transform.Projection = Matrix.PerspectiveFovLH( (float) Math.PI / 4, aspectRatio, 100.0f, -10000.0f );
            device.Transform.View = Matrix.LookAtLH( Position, LookAt, UpDir );
        }
    }
}
Any help would be greatly appreciated.
Blade -
legalize
legalize
What you posted looks fine based on a casual skimming of your code, but that's only the definition of your camera class and doesn't show how you're trying to use it.

In other words, if you never ask this camera to update its internal state, then it won't ever change.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.