Difference between revisions of "FPS-like camera controls"

From JPCT
Jump to: navigation, search
(FPS-like camera controls)
 
Line 41: Line 41:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
[[Category:jPCT]]
 +
[[Category:jPCT-AE]]

Latest revision as of 23:37, 1 February 2013

FPS-like camera controls

A first person shooter like camera movement is a common thing. Here's some more or less pseudo code to get you started:

This simple code implements a fps-like camera rotation without any limits when looking up/down, i.e. you can turn upside-down with this code:

int dx = <mouse delta x>;
int dy = <mouse delta y>;

if (dx!=0) {
	camera.rotateAxis(camera.getYAxis(), dx / 500f);
}

if (dy!=0) {
	camera.rotateX(dy / 500f);
}


If you want to limit the up/down rotation, something like this may help:

float xAngle=0; // some class attribute

....


int dx = <mouse delta x>;
int dy = <mouse delta y>;

if (dx!=0) {
	camera.rotateAxis(camera.getYAxis(), dx / 500f);
}

if ((dy > 0 && xAngle < Math.PI / 4.2) || (dy < 0 && xAngle > -Math.PI / 4.2)) {
        float t=dy/500f
	rot.rotateX(t);
	xAngle += t;
}