FPS-like camera controls

From JPCT
Revision as of 00:37, 2 February 2013 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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