Jump to content
  • Advertisement
Sign in to follow this  

[SOLVED] How to modify FPS Camera Matrix into CounterStrike FPS Camera

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

Here's the current code that I've been tinkering over the past few days:

public static void setFPSCamera(float[] matrix, int offset, float eyeX, float eyeY, float eyeZ, float pitch, float yaw, boolean isRadians)
//		if ( pitchDegrees<-90f)
//			pitchDegrees=-89.999999f;
//		if (pitchDegrees>90f)
//			pitchDegrees=89.999999f;
//		if (yawDegrees>360f)
//			yawDegrees=0f;
//		if (yawDegrees<0)
//			yawDegrees=360f;
		if (!isRadians){
		float cosPitch=FloatMath.cos(pitch);
		float sinPitch=FloatMath.sin(pitch);
		float cosYaw=FloatMath.cos(yaw);
		float sinYaw=FloatMath.sin(yaw);
		Vector3f xAxis=new Vector3f(cosYaw, -sinPitch*sinYaw, -cosPitch*sinYaw);
		Vector3f yAxis=new Vector3f(0, cosPitch, -sinPitch);
		Vector3f zAxis=new Vector3f(sinYaw, sinPitch*cosYaw, cosPitch*cosYaw);
		Vector3f eye=new Vector3f(eyeX, eyeY, eyeZ);

The commented portion of the code is what I thought I'm trying to do.

When the camera is facing north, the pitch values work correctly.


But when the camera is facing in other directions, the pitch values aren't affected by rotation.


Can anyone give me a hint on what I need to do to modify this matrix? Thanks in advance.

Share this post

Link to post
Share on other sites

This may help. Apparently your roll is 0, so cos(roll) = 1, sin(roll) = 0.


Also, don't know if you want the result to be row-major or column-major.

Share this post

Link to post
Share on other sites

Since I am using OpenGL ES, I need the column major matrix.


From the article, I can't tell if the Z axis the author had defined it to be the axis going through the screen, or being parallel to the screen when you are looking at the screen.


It also mentioned roll first, then pitch, and finally yaw. The order is the same when interpreting as code?

Share this post

Link to post
Share on other sites

The z-axis the author mentions is shown in the figure the author provides a link to. The page I linked to describes how to construct 3 matrices, and the result of multiplying them together. Each of the 3 matrices describes a 2D rotation about one of the orthogonal axes. He uses a right-hand system (axes follow the right-hand rule and positive angles are CCW). An example of one of the derivations is a couple links away.


Since you're using OpenGL, if you google for something like "view matrix yaw pitch roll" etc., there are 1000s of hits.

Share this post

Link to post
Share on other sites
The modified matrix still gives irregular rolls when turning around.

1. What matrix should I try to use that only yaws and pitches but does not roll? What are they called?

2. When a mouse moves in the X axis direction, I should rotate the view matrix so that objects in front rotate around me with an offset from the rotation pivot. But I notice my objects translate and rotate at the same time. What makes this happen?

Below is the code where I enter in the input values:

private void camera()
//		if (input.touchY + input.moveX > 45f)
//		{
//			input.touchY = 45f;
//			input.moveX = 0f;
//		}
//		if (input.touchY + input.moveY < -45f)
//		{
//			input.touchY = -45f;
//			input.moveY = 0f;
//		}
		Matrix.setLookAtM(viewMatrix, 0, 0, 0, 60,0, 0, 0f, 0, 1f, 0);
		//FPS camera: pitch and yaw, no roll. Order is important.
		//Matrix.rotateM(viewMatrix, 0, (input.x +input.distX) * 0.1f, 0f, -1f, 0f);
		//Matrix.rotateM(viewMatrix, 0, (input.y+input.distY) * 0.1f, -1f, 0f, 0f);
		//Matrix.translateM(viewMatrix, 0, -(input.x + input.distX) * 0.1f, 0f, 0f);
		Camera.setNewFPSCamera(viewMatrix, 0, 0, 0, 80f, 0f, -(input.y+input.distY)*0.1f, (input.x+input.distX)*0.1f, false);
		//Camera.setFPSCamera(viewMatrix, 0, 0, 0, 20, (input.y+input.distY)*movementFactor, (input.x+input.distX)*movementFactor, false);
		Matrix.multiplyMM(tempMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
		//Matrix.multiplyMM(modelViewProjectionMatrix, 0, projectionMatrix, 0, tempMatrix, 0);
		angle+= 0.1f;
		if (angle > 360f)
And here's the modified method:
	public static void setNewFPSCamera(float[] matrix, int offset, float eyeX, float eyeY, float eyeZ, float roll, float pitch, float yaw, boolean isRadians){
		if (!isRadians){
			//X value increase/decrease
			//yaw = (float)(Math.toRadians(yaw / Math.PI * 360f));
			roll = (float) Math.toRadians(roll);
			pitch = (float) Math.toRadians(pitch);
			yaw = (float) Math.toRadians(yaw);
		//A, B, C, D, E, F
		float cosPitch = (float) Math.cos(pitch);
		float sinPitch = (float) Math.sin(pitch);
		float cosYaw = (float) Math.cos(yaw);
		float sinYaw = (float) Math.sin(yaw);
		float cosRoll = (float) Math.cos(roll);
		float sinRoll = (float) Math.sin(roll);
		//AD, BD
		float cosPitchSinYaw = cosPitch * sinYaw;
		float sinPitchSinYaw = sinPitch * sinYaw;
		Vector3f xAxis = new Vector3f(cosYaw*cosRoll, -cosYaw*sinRoll, -sinYaw).normalize();
		Vector3f yAxis = new Vector3f(-sinPitchSinYaw*cosRoll+cosPitch*sinRoll,sinPitchSinYaw*sinRoll+cosPitch*cosRoll, -sinPitch*cosYaw).normalize();
		Vector3f zAxis = new Vector3f(cosPitchSinYaw*cosRoll+sinPitch*sinRoll, -cosPitchSinYaw*sinRoll+sinPitch*cosRoll, cosPitch*cosYaw).normalize();
		Vector3f eye = new Vector3f(eyeX, eyeY, eyeZ).normalize();
		matrix[0+offset] = cosYaw * cosRoll;
		matrix[1+offset] = -cosYaw * sinRoll;
		matrix[2+offset] = -sinYaw;
		matrix[3+offset] = 0f;
		matrix[4+offset] = -sinPitchSinYaw * cosRoll + cosPitch * sinRoll;
		matrix[5+offset] = sinPitchSinYaw * sinRoll + cosPitch * cosRoll;
		matrix[6+offset] = -sinPitch * cosYaw;
		matrix[7+offset] = 0f;
		matrix[8+offset] = cosPitchSinYaw * cosRoll + sinPitch * sinRoll;
		matrix[9+offset] = -cosPitchSinYaw * sinRoll + sinPitch * cosRoll;
		matrix[10+offset] = cosPitch * cosYaw;
		matrix[11+offset] = 0f;
		matrix[15+offset] = 1f;
I'm pretty sure that I am entering in the wrong input values. If I do a pitch, then a yaw, how should I negate the roll so that it's basically yawing at a specific pitch, and not rolling?

Share this post

Link to post
Share on other sites

Finally, I read somewhere in a book I borrowed said relevant things about FPS camera panning.  This usage is what I was trying to find. It is the first question's answer in my first post. The code below demonstrates the panning:

setIdentityM(viewMatrix, 0);
rotateM(viewMatrix, 0, -yRotation, 1f, 0f, 0f);
rotateM(viewMatrix, 0, -xRotation, 0f, 1f, 0f);
translateM(viewMatrix, 0, 0f, -1.5f, -5f);
multiplyMM(viewProjectionMatrix, 0, projectionMatrix, 0, viewMatrix, 0);

Rotating the matrix by the y rotation first and the x rotation second gives you an “FPS-style” rotation (where FPS stands for first person shooter), so rotating up or down always brings you toward your head or your feet, and rotating left or right always rotates you around in a circle about your feet.



Basically, I was doing the order of rotations wrong. Y comes first, before X. biggrin.png There's no need to modify the matrix's internal values using sines and cosines, but rather just switch around the rotation order, and that's it.


Thank you for your time, and I appreciated the help I've gotten. Problem solved!

Edited by tom_mai78101

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!