Sign in to follow this  

Strange rotation near origin

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

Hi guys.

To make it short: I'm currently programming a camera-view to move around freely in world space, and I'm getting a strange rotation-behaviour when my camera is close to the world origin (0,0,0).

Normally I would want it to rotate around itself (around an axis pointing to its right and one pointing upwards) when moving the mouse the corresponding directions, which it does, until the view gets close to 0,0,0, then it starts rotating around some other axis, but definitely not around itself.

Hoping someone has advice smile.png

 

 

Heres the code doing the work:

Update function for the camera, being called every frame:

void Camera::Update()
{
	if( fwd || back || left || right || rotate )
	{
		View = XMMatrixLookAtLH( EyePos, Focus, UpDir );

		ForwardVec = XMFLOAT3( View._13, View._23, View._33);
		RightVec = XMFLOAT3( View._11, View._21, 0.0f );

		if( rotate )
			Rotate();

		View = XMMatrixInverse( &dump, XMMatrixMultiply( XMMatrixInverse( &dump, View ), Rotation) ); //Rotation being applied here

		ForwardVec = XMFLOAT3( View._13, View._23, View._33);
		RightVec = XMFLOAT3( View._11, View._21, 0.0f );

		if( fwd )
			Forward();

		if( back )
			Backwards();

		if( left )
			Left();

		if( right )
			Right(); 

		Translation = XMMatrixTranslation( Offset.x, Offset.y, Offset.z );

		View = XMMatrixInverse( &dump, XMMatrixMultiply( XMMatrixInverse( &dump, View ), Translation) );
		
		cb.mView = XMMatrixTranspose( View );
		m_pImmediateContext->UpdateSubresource( pConstantBuffer, 0, NULL, &cb, 0, 0 );
	}
}

Heres the Rotate() function

void Camera::Rotate()
{
	UpVec = XMVector3Cross( XMLoadFloat3( &ForwardVec ) , XMLoadFloat3( &RightVec ) );

	AngleYaw += (float)pInput->GetMouseDelta().x * RotationSpeed;
	AnglePitch += (float)pInput->GetMouseDelta().y * RotationSpeed;
	
	Clamp( &AnglePitch, -90.0f * RotationSpeed * 4, 90.0f * RotationSpeed * 4 );

	Rotation = XMMatrixRotationAxis( XMLoadFloat3( &RightVec ), AnglePitch );
	Rotation *= XMMatrixRotationAxis( UpVec, AngleYaw );
}

Thanks in advance.

Share this post


Link to post
Share on other sites

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