Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


How to Create an ArcBall Camera


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 DiscGolfer17   Members   -  Reputation: 133

Like
0Likes
Like

Posted 24 December 2012 - 08:38 PM

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.

 



Sponsor:

#2 Steve_Segreto   Crossbones+   -  Reputation: 1551

Like
0Likes
Like

Posted 24 December 2012 - 09:16 PM

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;
}






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS