Jump to content
  • Advertisement
Sign in to follow this  
moodiez

Strange rotation near origin

This topic is 1691 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
Advertisement
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!