• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
tom_mai78101

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

5 posts in this topic

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){
			pitch=(float)Math.toRadians(pitch);
			yaw=(float)Math.toRadians(yaw);
		}
		
		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.

iBGMn6M.png
TPy5fcr.png

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

ty9U3vF.png
ud1bR7V.png

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

0

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.

0

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?

0

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.

0

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)
			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){
		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[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?
0

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0