Difference between revisions of "FPS-like camera controls"

From JPCT
Jump to: navigation, search
(Created page with '=== 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 implem...')
 
(FPS-like camera controls)
Line 19: Line 19:
 
</pre>
 
</pre>
  
If you want to limit the up/down rotation, something like this may help:
 
  
 +
If you want to limit the up/down rotation, something like this may help:
  
 
<pre>
 
<pre>

Revision as of 21:18, 15 June 2009

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