Jump to content
  • Advertisement
Sign in to follow this  
DiscGolfer17

How to Create an ArcBall Camera

This topic is 2121 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

I am trying to allow the user to rotate the camera around my 3D object by keeping the camera located somewhere on the sphere surrounding it. My first question is whether or not I can use a 2D virtual joystick to control the camera movement, and will the joystick map to the arcball camera? (Most examples I have seen are in XNA and using mouse dragging to move the camera) My second question is more of how the arcball works, and how I should go about creating it? If I can set up my joystick to manipulate the arcball rotation then I can just position my camera using it, and set the look at point to the origin (0, 0, 0).

 

Note: I am using Java on Android, as well as the libGDX framework.

 

Share this post


Link to post
Share on other sites
Advertisement

I don't see why the joystick wouldn't work.

 

You can convert this arc ball camera implementation from DirectX SDK samples - look up CD3DArcBall on the Internet.

 

  class CMeshArcBall : public CD3DArcBall { public : void OnBegin ( int nX, int nY, D3DXMATRIXA16* pmInvViewRotate ); void OnMove ( int nX, int nY, D3DXMATRIXA16* pmInvViewRotate ); LRESULT HandleMessages ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, D3DXMATRIXA16* pmInvViewRotate ); };   // // Arc-ball for mesh viewing // void CMeshArcBall::OnBegin ( int nX, int nY, D3DXMATRIXA16* pmInvViewRotate ) { m_bDrag = true; m_qDown = m_qNow; m_vDownPt = ScreenToVector( (float)nX, (float)nY ); D3DXVECTOR4 v; D3DXVec3Transform( &v, &m_vDownPt, pmInvViewRotate ); m_vDownPt = (D3DXVECTOR3)v; } void CMeshArcBall::OnMove ( int nX, int nY, D3DXMATRIXA16* pmInvViewRotate ) { if (m_bDrag) { m_vCurrentPt = ScreenToVector( (float)nX, (float)nY ); D3DXVECTOR4 v; D3DXVec3Transform( &v, &m_vCurrentPt, pmInvViewRotate ); m_vCurrentPt = (D3DXVECTOR3)v; m_qNow = m_qDown * QuatFromBallPoints( m_vDownPt, m_vCurrentPt ); } } LRESULT CMeshArcBall::HandleMessages ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, D3DXMATRIXA16* pmInvViewRotate ) { // Current mouse position int iMouseX = (short)LOWORD(lParam); int iMouseY = (short)HIWORD(lParam); switch( uMsg ) { case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK: { SetCapture( hWnd ); OnBegin( iMouseX, iMouseY, pmInvViewRotate ); return TRUE; } case WM_LBUTTONUP: { ReleaseCapture(); OnEnd(); return TRUE; } case WM_RBUTTONDOWN: case WM_MBUTTONDOWN: { // Store off the position of the cursor when the button is pressed m_ptLastMouse.x = iMouseX; m_ptLastMouse.y = iMouseY; return TRUE; } case WM_MOUSEMOVE: if( MK_LBUTTON & wParam ) { OnMove( iMouseX, iMouseY, pmInvViewRotate ); } else if( (MK_RBUTTON & wParam) || (MK_MBUTTON & wParam) ) { // Normalize based on size of window and bounding sphere radius FLOAT fDeltaX = ( m_ptLastMouse.x - iMouseX ) * m_fRadiusTranslation / m_nWidth; FLOAT fDeltaY = ( m_ptLastMouse.y - iMouseY ) * m_fRadiusTranslation / m_nHeight; if( wParam & MK_RBUTTON ) { D3DXVECTOR3 v = D3DXVECTOR3( -2 * fDeltaX, 2 * fDeltaY, 0.0f ); D3DXVec3TransformCoord( &v, &v, pmInvViewRotate ); D3DXMatrixTranslation( &m_mTranslationDelta, v.x, v.y, v.z ); //-2*fDeltaX, // // // 2*fDeltaY, // // // 0.0f // // // ); D3DXMatrixMultiply( &m_mTranslation, &m_mTranslation, &m_mTranslationDelta ); } else // wParam & MK_MBUTTON { D3DXVECTOR3 v = D3DXVECTOR3( 0.0f, 0.0f, 5 * fDeltaY ); D3DXVec3TransformCoord( &v, &v, pmInvViewRotate ); D3DXMatrixTranslation( &m_mTranslationDelta, v.x, v.y, v.z ); //0.0f, // // // 0.0f, // // // 5*fDeltaY D3DXMatrixMultiply( &m_mTranslation, &m_mTranslation, &m_mTranslationDelta ); } // Store mouse coordinate m_ptLastMouse.x = iMouseX; m_ptLastMouse.y = iMouseY; } return TRUE; } return FALSE; }

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!