Skip to main content
gamedev.net
🔒 Locked

Camera rotation question...

Started by Eibro 4 replies 600+ views
Original Post
Eibro
Eibro
I have a question pertaining to camera yaw. It seems that the more I rotate in either direction, the harder it becomes to rotate. For example, it's near impossible to turn the camera completely around (180 degrees). Why does this happen? Pitching the camera is the same way, I can't get any further than looking straight up or straight down. How can I fix this? I'm sure this is a problem many people have, my code doesn't deviate much from the camera code out there. I'll post my code anyway.
void FirstPersonCamera::Pitch( FLOAT theta ) {

	// Rotation about X
	D3DXVECTOR3 view = look_ - eye_;
	D3DXVECTOR3 yaw;
	D3DXVec3Cross( &yaw, &view, &up_ );
	D3DXVec3Normalize( &yaw, &yaw );

	D3DXVECTOR4 newView;//, newUp;
	//D3DXVECTOR3	view = look_ - eye_;

	D3DXMATRIX rotationMatrix;
	D3DXMatrixRotationAxis( &rotationMatrix, &yaw, theta );
	D3DXVec3Transform( &newView, &view, &rotationMatrix ); 
	//D3DXVec3Transform( &newUp, &up_, &rotationMatrix );

	look_.y = eye_.y + newView.y;
	//up_ = *(D3DXVECTOR3*)&newUp
	//look_.z = eye_.z + newView.z;
}

void FirstPersonCamera::Yaw( FLOAT theta ) {

	D3DXVECTOR4 newView;
	D3DXVECTOR3	view = look_ - eye_;

	D3DXMATRIX rotationMatrix;
	D3DXMatrixRotationAxis( &rotationMatrix, &up_, theta );
	D3DXVec3Transform( &newView, &view, &rotationMatrix ); 

	//look_.y = eye_.y + newView.y;
	look_.x = eye_.x + newView.x;
}

void FirstPersonCamera::HandleMouseInput( const POINT& center ) {

	POINT p;
	assert( GetCursorPos( &p ) );

	if ( abs( p.x - center.x ) > threshHold ) {

		LONG difference = center.x - p.x;
		this->Yaw( -difference / 800.0f );
	}

	if ( abs( p.y - center.y ) > threshHold ) {

		LONG difference = center.y - p.y;
		this->Pitch( difference / 800.0f );
	} 
	
	SetCursorPos( center.x, center.y );
}
rohitgonsalves
rohitgonsalves
I AM REFINING YOUR CAMERA FOR YAW, ELSE YOU TRY.

eye is your eye position,
look is your look at point,
up vector is calculated y orienatation using cross product
of z-axis and x-axis u got.

Then,


void FirstPersonCamera::Yaw( FLOAT angle )
{

D3DXVECTOR4 newView;
D3DXVECTOR3 view = look - eye;

D3DXMATRIX rotationMatrix;
D3DXMatrixRotationAxis( &rotationMatrix, &up_, angle );
D3DXVec3Transform( &newView, &view, &rotationMatrix );

// why not adding z-component to rotate in xz plane
// around y-Axis?

look_.z = eye.z + newView.z;
look_.x = eye.x + newView.x;
}
MOV AL, 0x00MOV AL, ME
RenderTarget
RenderTarget
Have you tried filling in all of look_ and up_? All of x y and z, I mean. If some parts never get updated, your math will get trapped by values that can't change.
[sub]My spoon is too big.[/sub]
Eibro
Eibro
Nice! Filling all x, y, z positions in yaw and pitch fixed it. eg. look_ = eye_ + *(D3DXVECTOR3*)&newView
Thanks for the help guys.
Though I have another brief question pertaining to this. Does the up vector have to be updated if I pitch up or down? It seems like it does to me, but it doesn't make a difference whether or not I update it or not.
RenderTarget
RenderTarget
It sort of depends on whether you want total freedom of movement: When you pitch up more than 90 degrees, should you be able to keep pitching back around? If yes, then definitely update the up_ vector. The reason is that once you're tipped up past 90 degrees, your up vector should be pointing down.

Even if you want to restrict the up and down, like a Quake 1 style camera, you should consider updating the up_ anyway, and artificially cap the rotation. This ensures your numbers stay stable; taking the cross of two vectors (up_ and (look_ - eye_), say) that point the same direction will give you an arbitrary yaw vector, which will almost always be wrong. By updating up_ you ensure they're never the same.
[sub]My spoon is too big.[/sub]
rohitgonsalves
rohitgonsalves
We can always calculate Up vector at any time using
cross product of z and x axis of camera(local axes for
camera). it is not mandatory to update up vector always.

But good practice says that always remian up to date.

So please update your up vector too. that will never take
you in a problem.

Write a class that will hold members as
Xaxis, Yaxis AND Zaxis.

You pass eye,up and look vector to your constuctor.Then keep
processing your all three axis of camera within procedures
written for camera movements and rotations.

That will work fine without any confusion.
MOV AL, 0x00MOV AL, ME

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.