Thread: Unable to Roll along axis on my camera class

  1. #1
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,037

    Exclamation Unable to Roll along axis on my camera class

    Hey guys, this is my first post asking for help on this forum for about 7-8 years. I'm using DirectX(I know should go under Windows or Game programming,but nobody browses those and this is a C++ question in general anyway.

    I have my camera class, from Camera.cpp and camera.h.
    They all have three functions that work perfectly(actually two of them work)

    when I go
    Code:
    m_camera.Yaw(DeltaY);
    m_camera.Pitch(DeltaX);
    She works. Beautifully, I might add. However...
    Code:
    m_camera.Roll(DeltaZ);
    Does nothing. Even if I just put
    Code:
    m_camera.Roll(10.00f); //or any over float value
    It simply does not roll.

    Here is Camera.cpp:
    Code:
    #include <d3d9.h>					// directx header files
    #include <d3dx9.h>
    #include "camera.h"
    #include "common.h"
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Default constructor
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    CCamera::CCamera()
    {
    	m_maxPitch = D3DXToRadian(89.0f);
    	m_maxVelocity = 1.0f;
    	m_invertY = FALSE;
    	m_enableYMovement = TRUE;
    	m_position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
    	m_velocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
    	m_look = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
    	CreateProjectionMatrix(D3DX_PI / 3.0f, 1.3f, 0.1f, 1000.0f);
    	Update();
    }
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Creates the projection matrix.
    Parameters:
    [in] fov - Field of view
    [in] aspect - Aspect ratio
    [in] near - Near plane
    [in] far - Far plane
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    void CCamera::CreateProjectionMatrix(float fov, float aspect, float nearPlane, float farPlane)
    {
    	m_fov = fov;
    	m_aspect = aspect;
    	m_nearPlane = nearPlane;
    	m_farPlane = farPlane;
    	D3DXMatrixPerspectiveFovLH(&m_projection, m_fov, m_aspect, m_nearPlane, m_farPlane);
    }
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Moves the camera forward and backward
    Parameters:
    [in] units - Amount to move
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    void CCamera::MoveForward(float units)
    {
    	if (m_enableYMovement)
    	{
    		m_velocity += m_look * units;
    	}
    	else
    	{
    		D3DXVECTOR3 moveVector(m_look.x, 0.0f, m_look.z);
    		D3DXVec3Normalize(&moveVector, &moveVector);
    		moveVector *= units;
    		m_velocity += moveVector;
    	}
    }
    
    void CCamera::MoveBackward(float units)
    {
    	if (m_enableYMovement)
    	{
    		m_velocity -= m_look * units;
    	}
    	else
    	{
    		D3DXVECTOR3 moveVector(m_look.x, 0.0f, m_look.z);
    		D3DXVec3Normalize(&moveVector, &moveVector);
    		moveVector *= units;
    		m_velocity += moveVector;
    	}
    }
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Moves the camera left and right
    Parameters:
    [in] units - Amount to move
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    void CCamera::Strafe(float units)
    {
    	m_velocity += m_right * units;
    }
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Moves the camera up and down
    Parameters:
    [in] units - Amount to move
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    void CCamera::MoveUp(float units)
    {
    	if (m_enableYMovement)
    	{
    		m_velocity.y += units;
    	}
    }
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Yaw the camera around its Y-axis.
    Parameters:
    [in] radians - Radians to yaw.
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    void CCamera::Yaw(float radians)
    {
    	if (radians == 0.0f)
    	{
    		return;
    	}
    	D3DXMATRIX rotation;
    	D3DXMatrixRotationAxis(&rotation, &m_up, radians);
    	D3DXVec3TransformNormal(&m_right, &m_right, &rotation);
    	D3DXVec3TransformNormal(&m_look, &m_look, &rotation);
    }
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Pitch the camera around its X-axis.
    Parameters:
    [in] radians - Radians to pitch.
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    void CCamera::Pitch(float radians)
    {
    	if (radians == 0.0f)
    	{
    		return;
    	}
    
    	radians = (m_invertY) ? -radians : radians;
    	m_pitch -= radians;
    	if (m_pitch > m_maxPitch)
    	{
    		radians += m_pitch - m_maxPitch;
    	}
    	else if (m_pitch < -m_maxPitch)
    	{
    		radians += m_pitch + m_maxPitch;
    	}
    
    	D3DXMATRIX rotation;
    	D3DXMatrixRotationAxis(&rotation, &m_right, radians);
    	D3DXVec3TransformNormal(&m_up, &m_up, &rotation);
    	D3DXVec3TransformNormal(&m_look, &m_look, &rotation);
    }
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Roll the camera around its Z-axis.
    Parameters:
    [in] radians - Radians to roll.
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    void CCamera::Roll(float radians)
    {
    	if (radians == 0.0f)
    	{
    		return;
    	}
    
    	D3DXMATRIX rotation;
    	D3DXMatrixRotationAxis(&rotation, &m_look, radians);
    	D3DXVec3TransformNormal(&m_right, &m_right, &rotation);
    	D3DXVec3TransformNormal(&m_up, &m_up, &rotation);
    }
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Updates the camera and creates a new view matrix.
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    void CCamera::Update()
    {
    	// Cap velocity to max velocity
    	if (D3DXVec3Length(&m_velocity) > m_maxVelocity)
    	{
    		m_velocity = *(D3DXVec3Normalize(&m_velocity, &m_velocity)) * m_maxVelocity;
    	}
    
    	// Move the camera
    	m_position += m_velocity;
    	// Could decelerate here. I'll just stop completely.
    	m_velocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
    	m_lookAt = m_position + m_look;
    
    	// Calculate the new view matrix
    	D3DXVECTOR3 up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
    	D3DXMatrixLookAtLH(&m_view, &m_position, &m_lookAt, &up);
    
    	// Set the camera axes from the view matrix
    	m_right.x = m_view._11;
    	m_right.y = m_view._21;
    	m_right.z = m_view._31;
    	m_up.x = m_view._12;
    	m_up.y = m_view._22;
    	m_up.z = m_view._32;
    	m_look.x = m_view._13;
    	m_look.y = m_view._23;
    	m_look.z = m_view._33;
    	
    	// Calculate yaw and pitch
    	float lookLengthOnXZ = sqrtf(m_look.z * m_look.z + m_look.x * m_look.x);
    	m_pitch = atan2f(m_look.y, lookLengthOnXZ);
    	m_yaw = atan2f(m_look.x, m_look.z);
    }
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Updates the camera and creates a new view matrix.
    Parameters:
    [in] pPosition - New position
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    void CCamera::SetPosition(D3DXVECTOR3* pPosition)
    {
    	m_position.x = pPosition->x;
    	m_position.y = pPosition->y;
    	m_position.z = pPosition->z;
    }
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Summary: Updates the camera and creates a new view matrix.
    Parameters:
    [in] pPosition - New target
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    void CCamera::SetLookAt(D3DXVECTOR3* pLookAt)
    {
    	m_lookAt.x = pLookAt->x;
    	m_lookAt.y = pLookAt->y;
    	m_lookAt.z = pLookAt->z;
    	D3DXVec3Normalize(&m_look, &(m_lookAt - m_position));
    }
    and Here is camera.h:
    Code:
    #ifndef CCAMERA_H
    #define CCAMERA_H
    
    
    
    class CCamera
    {
    public:
    	CCamera();
    	void CreateProjectionMatrix(float fov, float aspect, float nearPlane, float farPlane);
    	void MoveForward(float units);
    	void MoveBackward(float units);
    	void Strafe(float units);
    	void MoveUp(float units);
    
    	void Yaw(float radians);
    	void Pitch(float radians);
    	void Roll(float radians);
    	void Update();
    
    	void SetPosition(D3DXVECTOR3* pPosition);
    	void SetLookAt(D3DXVECTOR3* pLookAt);
    	void SetFOV(float fov)            { CreateProjectionMatrix(fov, m_aspect, m_nearPlane, m_farPlane); }
    	void SetAspectRatio(float aspect) { CreateProjectionMatrix(m_fov, aspect, m_nearPlane, m_farPlane); }
    	void SetNearPlane(float plane)    { CreateProjectionMatrix(m_fov, m_aspect, plane, m_farPlane); }
    	void SetFarPlane(float plane)     { CreateProjectionMatrix(m_fov, m_aspect, m_nearPlane, plane); }
    	void SetMaxVelocity(float maxVelocity) { m_maxVelocity = maxVelocity; }
    	void SetInvertY(BOOL invert)           { m_invertY = invert; }
    	void SetMaxPitch(float maxPitch)       { m_maxPitch = maxPitch; }
    
    	D3DXMATRIX* GetViewMatrix()        { return &m_view; }
    	D3DXMATRIX* GetProjectionMatrix()  { return &m_projection; }
    	D3DXVECTOR3* GetPosition()         { return &m_position; }
    	D3DXVECTOR3 GetPos()               { return m_position; }
    	D3DXVECTOR3* GetLookAt()           { return &m_lookAt; }
    	float GetFOV()                     { return m_fov; }
    	float GetAspectRatio()             { return m_aspect; }
    	float GetNearPlane()               { return m_nearPlane; }
    	float GetFarPlane()                { return m_farPlane; }
    	float GetMaxVelocity()             { return m_maxVelocity; }
    	BOOL  GetInvertY()                 { return m_invertY; }
    	float GetPitch()                   { return m_pitch; }
    	float GetYaw()                     { return m_yaw; }
    	float GetMaxPitch()                { return m_maxPitch; }
    
    private:
    	D3DXMATRIX  m_view;
    	D3DXMATRIX  m_projection;
    	D3DXVECTOR3 m_right;
    	D3DXVECTOR3 m_up;
    	D3DXVECTOR3 m_look;
    	D3DXVECTOR3 m_position;
    	D3DXVECTOR3 m_lookAt;
    	D3DXVECTOR3 m_velocity;
    	float       m_yaw;
    	float       m_pitch;
    	float		m_roll;
    	float       m_maxPitch;
    	float       m_maxVelocity;
    	float       m_fov;
    	float       m_aspect;
    	float       m_nearPlane;
    	float       m_farPlane;
    	BOOL        m_invertY;
    	BOOL        m_enableYMovement;
    };
    
    #endif
    What gives? Yaw and Pitch work PERFECT, but not Roll(). Why? What am I missing? I know some you experts out there can help me find the answer.

    Thank you so much for your time and consideration. And mods, please don't move this another subforum that nobody reads. This is mostly a C++ issue, after all, not a graphics API issue.

    Thank you!

    ~Xterria

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,483
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,037
    Quote Originally Posted by Salem View Post
    Hey Salem! It's been years since I've seen you around. Sorry for the cross-post, I'm simply attempting to get to the bottom of the issue as fast as possible -- so anyone here who can help with this issue the help would be greatly appreciated. On Cplusplus.com they actually believed that by affixing an 'f' next to a numeral into a parameter that is a float might've been the issue. I know the people on this forum are a lot more experienced, so I posted here first and would still appreciate the help.

    It's good to see you again, Salem.
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unable to instantiate derived class!!!
    By V8cTor in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2015, 09:52 AM
  2. Unable to see enclosed private class?
    By std10093 in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2013, 07:02 AM
  3. Roll on 18 wheels roll on
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 05-13-2009, 09:12 PM
  4. How can I make the base class unable to instantiate?
    By dxfoo in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 09:57 AM
  5. Digital Camera -> Slo-Mo Camera??
    By Masa in forum Tech Board
    Replies: 6
    Last Post: 12-24-2003, 11:11 AM

Tags for this Thread