View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1asperatology  Members

Posted 20 February 2014 - 06:33 AM

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;

}

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);

matrix[0+offset]=cosYaw;
matrix[1+offset]=0;
matrix[2+offset]=sinYaw;
matrix[3+offset]=0;

matrix[4+offset]=-sinPitch*sinYaw;
matrix[5+offset]=cosPitch;
matrix[6+offset]=sinPitch*cosYaw;
matrix[7+offset]=0;

matrix[8+offset]=-cosPitch*sinYaw;
matrix[9+offset]=-sinPitch;
matrix[10+offset]=cosPitch*cosYaw;
matrix[11+offset]=0;

matrix[12+offset]=-xAxis.dotProduct(eye);
matrix[13+offset]=-yAxis.dotProduct(eye);
matrix[14+offset]=-zAxis.dotProduct(eye);
matrix[15+offset]=1;
}

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.

### #2Buckeye  GDNet+

Posted 20 February 2014 - 07:39 AM

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #3asperatology  Members

Posted 20 February 2014 - 05:04 PM

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?

### #4Buckeye  GDNet+

Posted 20 February 2014 - 08:49 PM

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #5asperatology  Members

Posted 22 February 2014 - 08:48 PM

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)
angle=0f;
}

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){
//X value increase/decrease
//yaw = (float)(Math.toRadians(yaw / Math.PI * 360f));
}

//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);

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[12+offset]=-xAxis.dotProduct(eye);
matrix[13+offset]=-yAxis.dotProduct(eye);
matrix[14+offset]=-zAxis.dotProduct(eye);
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?

### #6asperatology  Members

Posted 25 February 2014 - 08:37 AM

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.  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, 25 February 2014 - 08:41 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.