• Advertisement
Sign in to follow this  

Strange rotation near origin

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

Hm, no ideas ? Can someone at least tell me if I'm rotating the view-matrix correctly ?

Share this post


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

  • Advertisement