Archived

This topic is now archived and is closed to further replies.

dashworth

Direct3D Rotation Question

Recommended Posts

Ok, I''m putting in the time, reading the SDK, checking out drunken Hyena and checking out Nexe. I''m currently working on rotation and using matrices. My problem is, I''d like to be able to rotate my view around the y axis and place objects in front of, behind, and beside my view. I''m using Direct3DX and it should probably be easy, but there''s something I''m not getting. Everything I''m reading about talks more about rotating objects on the screen than the view itself. Can anyone point me in the right direction? I''m not asking for a handout or a quick fix, just a direction so I can add more countless hours of study to my current list of study hours. I''ve gotten to the point where I''m about to finish all the material i have on Matrices, rotation, etc, and I still dont feel like I have a total grasp on the topic obviously, especially when rotating my views. I feel pretty good about matrices and what they are for, but this rotating a camera thing is killin me, and I''ve yet to cover Quaternions and scaling. I can rotate squares, triangles, etc all day, but I''m interested in rotating views (cameras) around an axis, or rotate around the point the camera is pointing to. (i.e. a camera rotating around a model that represents the player). I know I''ll be rereading what I have, but all I have now is the SDK and Beginning Direct 3D, and it hasn''t been much of a help unfortunately. It''s introduced me to alot as far as understanding concepts, but that''s about it. Thanks for any help. Dave _________________ There are three kinds of people in this world: Those who can count, and those who cant.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
<table border=1 cellpadding=0 cellspacing=0 bgcolor=#FFFFFF width=100%>
<tr>
<td align=left>

// /////////////////////////////////////////
//Camera.h
// /////////////////////////////////////////

class Camera
{

protected:

D3DXVECTOR3 m_vEyePt;
D3DXVECTOR3 m_vRotation;

FLOAT m_Sensitivity;
FLOAT m_Velocity;

public:

Camera();

VOID SetSensitivity(FLOAT Value = 0.0025f) { m_Sensitivity = Value; }
VOID SetVelocity(FLOAT Value = 0.4f) { m_Velocity = Value; }

HRESULT Move(BOOL* bKeys);
HRESULT MouseLook(HWND hWnd, IDirect3DDevice8* pd3dDevice, FLOAT EyeLevel);

};


// /////////////////////////////////////////
// Camera.cpp
// /////////////////////////////////////////

#include
#include "Camera.h"

Camera::Camera()
{
m_vEyePt.x = 0.0f;
m_vEyePt.y = 1.0f;
m_vEyePt.z = -5.0f;

m_vRotation.x = 0.0f;
m_vRotation.y = 0.0f;

m_Sensitivity = 0.0025f;
m_Velocity = 0.4f;
}

HRESULT Camera::Move(BOOL* bKeys)
{

// Forward
if( bKeys[''W''] )
{
m_vEyePt.x = m_vEyePt.x + (float)cos(m_vRotation.x) * (float)sin(m_vRotation.y) * m_Velocity;
m_vEyePt.z = m_vEyePt.z + (float)cos(m_vRotation.x) * (float)cos(m_vRotation.y) * m_Velocity;
}

// Back
if( bKeys[''S''] )
{
m_vEyePt.x = m_vEyePt.x - (float)cos(m_vRotation.x) * (float)sin(m_vRotation.y) * m_Velocity;
m_vEyePt.z = m_vEyePt.z - (float)cos(m_vRotation.x) * (float)cos(m_vRotation.y) * m_Velocity;
}

// Strafe Left
if( bKeys[''A''] )
{
m_vEyePt.x = m_vEyePt.x + (float)cos(m_vRotation.x) * (float)sin(m_vRotation.y - 1.57079f) * m_Velocity;
m_vEyePt.z = m_vEyePt.z + (float)cos(m_vRotation.x) * (float)cos(m_vRotation.y - 1.57079f) * m_Velocity;
}

// Strafe Right
if( bKeys[''D''] )
{
m_vEyePt.x = m_vEyePt.x + (float)cos(m_vRotation.x) * (float)sin(m_vRotation.y + 1.57079f) * m_Velocity;
m_vEyePt.z = m_vEyePt.z + (float)cos(m_vRotation.x) * (float)cos(m_vRotation.y + 1.57079f) * m_Velocity;
}

return D3D_OK;
}

HRESULT Camera::MouseLook(HWND hWnd, IDirect3DDevice8* pd3dDevice, FLOAT EyeLevel)
{

// Get the mouse coords
POINT ptMouse; GetCursorPos(&ptMouse);
ScreenToClient(hWnd, &ptMouse);

// Get the client rect
RECT rcWindow; GetClientRect(hWnd, &rcWindow);

// Get the coords for the center of the screen
POINT ptCenter;
ptCenter.x = rcWindow.right/2; ptCenter.y = rcWindow.bottom/2;

// Reset the mouse to the center of the screen
SetCursorPos(ptCenter.x, ptCenter.y);

FLOAT ThetaX = abs(ptMouse.x - ptCenter.x) * m_Sensitivity;
FLOAT ThetaY = abs(ptMouse.y - ptCenter.y) * m_Sensitivity;

// Calculate the look at point depending on the mouse position
if( (ptMouse.x != ptCenter.x) || (ptMouse.y != ptCenter.y) )
{
if( ptMouse.x > ptCenter.x ) // is it to the left?
{
m_vRotation.y += ThetaX; // if so spin right
}
else if( ptMouse.x < ptCenter.x ) // is it to the right?
{
m_vRotation.y -= ThetaX; // if so spin left
}

if( ptMouse.y > ptCenter.y ) // is it to the top?
{
m_vRotation.x -= ThetaY; // if so look up
}
else if( ptMouse.y < ptCenter.y ) // is it to the bottom?
{
m_vRotation.x += ThetaY; // if so look down
}
}

if( m_vRotation.x > 0.85f)
{
m_vRotation.x = 0.85f;
}
if( m_vRotation.x < -1.00f)
{
m_vRotation.x = -1.00f;
}

// Give an adjustment to simulate an eye-high view
m_vEyePt.y = EyeLevel;

// Calculate look point
D3DXVECTOR3 vLookAtPt;
vLookAtPt.x = m_vEyePt.x + (float)cos(m_vRotation.x) * (float)sin(m_vRotation.y);
vLookAtPt.y = m_vEyePt.y + (float)sin(m_vRotation.x);
vLookAtPt.z = m_vEyePt.z + (float)cos(m_vRotation.x) * (float)cos(m_vRotation.y);

// The view matrix
D3DXMATRIX ViewMatrix;

// Create the view matrix
D3DXMatrixLookAtLH(&ViewMatrix,
&m_vEyePt,
&vLookAtPt,
&D3DXVECTOR3(0.0f, 1.0f, 0.0f)
);

// Set the view matrix
pd3dDevice->SetTransform(D3DTS_VIEW, &ViewMatrix);

return D3D_OK;
}


</td>
</tr>
</table>

Share this post


Link to post
Share on other sites
Thanks for the code. I do use classes in what I''m doing, so this is more than great. I will make sure the code compiles with Visual C++ 6 (if that''s what you''re using) and try to add it to my main routine. Unfortunately, I''m being shipped to Houston next week for training, so I wont be able to test it until I get back since my laptop doesnt have a 3D accelerator card (grr). Hopefully, I will figure out how your code works this week. That''s more important than just getting it running.
It seems pretty straight forward.

Thank you for your time and knowledge transfer.

Share this post


Link to post
Share on other sites
Thanks for the code. I do use classes in what I''m doing, so this is more than great. I will make sure the code compiles with Visual C++ 6 (if that''s what you''re using) and try to add it to my main routine. Unfortunately, I''m being shipped to Houston next week for training, so I wont be able to test it until I get back since my laptop doesnt have a 3D accelerator card (grr). Hopefully, I will figure out how your code works this week. That''s more important than just getting it running.
It seems pretty straight forward.

Thank you for your time and knowledge transfer.



Dave
_________________

There are three kinds of people in this world: Those who can count, and those who cant.

Share this post


Link to post
Share on other sites
Let me just had a quick explanation to the code given.

Basically before when you were rotating rectangles and other objects you were applying rotations to the world matrix no doubt.

All you have to do to adjust the camera rotation and position is do your transformations to the view matrix. As you''ll see in the code posted "pd3dDevice->SetTransform(D3DTS_VIEW, &ViewMatrix);" does what you want.

You set up a matrix, do a rotation on it, then apply the matrix to the view.

Hope this helps

Share this post


Link to post
Share on other sites