Sign in to follow this  

I need help! About camera

This topic is 4684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I able to translate the camera and able to rotate it with its own axis? Something like first person shooter view, but instead of using mouse to spin the camera, somewhat I want to use: Just translate it without rotation (W-to move camera forward) (S-to move camera backward) (A-to move camera right) (D-to move camera left) Just rotate it without translation (Up arrow-to rotate the camera and look upward) (Down arrow-to rotate the camera and look downward) (Right arrow-to rotate the camera and look right) (Left arrow-to rotate the camera and look left) The following code accomplishes both the camera translation and camera rotation but mouse was used to spin the camera. I tried some experiment to change the mouse control into keyboard control to spin the camera but the rotation was unpredictable. void getRealTimeUserInput( void ) { // // Get mouse input... // POINT mousePosit; GetCursorPos( &mousePosit ); ScreenToClient( g_hWnd, &mousePosit ); g_ptCurrentMousePosit.x = mousePosit.x; g_ptCurrentMousePosit.y = mousePosit.y; D3DXMATRIX matRotation; if( g_bMousing ) { int nXDiff = (g_ptCurrentMousePosit.x - g_ptLastMousePosit.x); int nYDiff = (g_ptCurrentMousePosit.y - g_ptLastMousePosit.y); if( nYDiff != 0 ) { D3DXMatrixRotationAxis( &matRotation, &g_vRight, D3DXToRadian((float)nYDiff / 3.0f)); D3DXVec3TransformCoord( &g_vLook, &g_vLook, &matRotation ); D3DXVec3TransformCoord( &g_vUp, &g_vUp, &matRotation ); } if( nXDiff != 0 ) { D3DXMatrixRotationAxis( &matRotation, &D3DXVECTOR3(0,1,0), D3DXToRadian((float)nXDiff / 3.0f) ); D3DXVec3TransformCoord( &g_vLook, &g_vLook, &matRotation ); D3DXVec3TransformCoord( &g_vUp, &g_vUp, &matRotation ); } } g_ptLastMousePosit.x = g_ptCurrentMousePosit.x; g_ptLastMousePosit.y = g_ptCurrentMousePosit.y; // // Get keyboard input... // unsigned char keys[256]; GetKeyboardState( keys ); D3DXVECTOR3 tmpLook = g_vLook; D3DXVECTOR3 tmpRight = g_vRight; // Up Arrow Key - View moves forward if( keys[VK_UP] & 0x80 ) g_vEye -= tmpLook*-g_fMoveSpeed*g_fElpasedTime; // Down Arrow Key - View moves backward if( keys[VK_DOWN] & 0x80 ) g_vEye += (tmpLook*-g_fMoveSpeed)*g_fElpasedTime; // Left Arrow Key - View side-steps or strafes to the left if( keys[VK_LEFT] & 0x80 ) g_vEye -= (tmpRight*g_fMoveSpeed)*g_fElpasedTime; // Right Arrow Key - View side-steps or strafes to the right if( keys[VK_RIGHT] & 0x80 ) g_vEye += (tmpRight*g_fMoveSpeed)*g_fElpasedTime; // Home Key - View elevates up if( keys[VK_HOME] & 0x80 ) g_vEye.y += g_fMoveSpeed*g_fElpasedTime; // End Key - View elevates down if( keys[VK_END] & 0x80 ) g_vEye.y -= g_fMoveSpeed*g_fElpasedTime; } void updateViewMatrix( void ) { D3DXMATRIX view; D3DXMatrixIdentity( &view ); D3DXVec3Normalize( &g_vLook, &g_vLook ); D3DXVec3Cross( &g_vRight, &g_vUp, &g_vLook ); D3DXVec3Normalize( &g_vRight, &g_vRight ); D3DXVec3Cross( &g_vUp, &g_vLook, &g_vRight ); D3DXVec3Normalize( &g_vUp, &g_vUp ); view._11 = g_vRight.x; view._12 = g_vUp.x; view._13 = g_vLook.x; view._14 = 0.0f; view._21 = g_vRight.y; view._22 = g_vUp.y; view._23 = g_vLook.y; view._24 = 0.0f; view._31 = g_vRight.z; view._32 = g_vUp.z; view._33 = g_vLook.z; view._34 = 0.0f; view._41 = -D3DXVec3Dot( &g_vEye, &g_vRight ); view._42 = -D3DXVec3Dot( &g_vEye, &g_vUp ); view._43 = -D3DXVec3Dot( &g_vEye, &g_vLook ); view._44 = 1.0f; g_pd3dDevice->SetTransform( D3DTS_VIEW, &view ); } I’m just beginner in C++ and D3D and I need comprehensive code to spin the camera using keyboard control. Please do not encapsulate the code. THANKS!!!

Share this post


Link to post
Share on other sites
You just need to adapt the inputs from the keyboard to return the same previous values (nXDiff and nYDiff).
From that code, when you move the mouse, nXDiff and/or nYDiff are different from 0. Else, these values are 0.

So, do something like this:

if ( PRESS_ARROW_UP == true )
{
nYDiff = valueY;
}
else if ( PRESS_ARROW_DOWN == true )
{
nYDiff = -valueY;
}
else
{
nYDiff = 0;
}

Do the same thing for spinning around x direction.

Share this post


Link to post
Share on other sites
This is the code that i tried, but it rapidly spin the camera.






D3DXMATRIX matRotation;
static int nXDiff,nYDiff;

//KEY RIGHT
if(GetAsyncKeyState(VK_RIGHT))
{ nXDiff=nXDiff+1; }

//KEY LEFT
else if(GetAsyncKeyState(VK_LEFT))
{ nXDiff=nXDiff-1; }

else
{ nXDiff=0; }


if( nYDiff != 0 )
{
D3DXMatrixRotationAxis( &matRotation, &g_vRight, D3DXToRadian((float)nYDiff / 10.0f));

D3DXVec3TransformCoord( &g_vLook, &g_vLook, &matRotation );
D3DXVec3TransformCoord( &g_vUp, &g_vUp, &matRotation );
}

if( nXDiff != 0 )
{
D3DXMatrixRotationAxis( &matRotation, &D3DXVECTOR3(0,1,0), D3DXToRadian((float)nXDiff / 10.0f) );

D3DXVec3TransformCoord( &g_vLook, &g_vLook, &matRotation );
D3DXVec3TransformCoord( &g_vUp, &g_vUp, &matRotation );
}



Share this post


Link to post
Share on other sites
Quote:
//KEY RIGHT
if(GetAsyncKeyState(VK_RIGHT))
{ nXDiff=nXDiff+1; }

//KEY LEFT
else if(GetAsyncKeyState(VK_LEFT))
{ nXDiff=nXDiff-1; }

else
{ nXDiff=0; }


It was not that I had posted [smile]. You are increasing/decreasing nXDiff. Try this:

if(GetAsyncKeyState(VK_RIGHT))
{
nXDiff = 1;
}
else if(GetAsyncKeyState(VK_LEFT))
{
nXDiff = -1;
}
else
{
nXDiff = 0;
}

Share this post


Link to post
Share on other sites
I see! It really works and absolutely works and it works properly.
The big mistake was on me, because I misunderstood what you mean.


Your help is 100% working, it spin the camera using keyboard control.


Thank you so much ADRIANO_USP!

Share this post


Link to post
Share on other sites

This topic is 4684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this