Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (797)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  FPS Camera Tutorial  (Read 201332 times)
0 Members and 1 Guest are viewing this topic.
Offline Wiki Duke

?





« Posted 2016-05-22 05:40:58 »

I've noticed the serious lack of good explanation of FPS camera methods. I've seen everything from using cross product to using sketchy pi calculations and back around to poorly explained trigonometry. I am writing this forum post for novices in hopes that a random google search will pick it up... but whoever browses is good too! Smiley

This tutorial is not based off OpenGL and doesn't use 3rd party libraries. I've seen many people use OpenGL fixed function pipeline and many other inadapatable means of showing what entails a FPS camera. So.. that was a problem. I am going the trig route because its fast, simple, and easy to understand. Pure math.

Things you should know before this:
Using trigonometry functions sin, cos, and tan (sin and cos are to be used)

I am writing this in Java, because well... this is JGO.
Okay let's get started!



We are in the main game loop, what we want to do is this:
If a key is down move, in the direction the object is facing, with respect to the key's direction. Also, strafing.



Simple enough. As you see in the picture, I've put out a right triangle on a smiley face. Since, in game, we allot a predefined variable for speed, we can set this as the Hypotenuse, or the distance. In the game, the object has rotation. Let the Yaw value (pitch = x, yaw = y, roll = z) be the angle between the hypotenuse and the adjacent side. I am calling this angle A for alpha, as per the angles name. We have everything we need now.

This is what they mean, in case you had no idea. Sine works with opposite and hypotenuse, Cosine works with adjacent and hypotenuse, and tangent works with opposite and adjacent. We are going to use both Sine and Cosine.
S.oh C.ah T.oa
Sine Cosine Tangent


Looking back at the picture, let's imagine the hypotenuse, coming out of Mr. Smiley, is the direction that we want to go. Now look at the adjacent side and the opposite side... they describe how much you need to "go over" and "go up" in order to reach that goal. Genius right?

So if the adjacent comes out to be 5 and the opposite comes out as 5 (no you don't just get an equilateral triangle) you need to translate your object's x by 5 and y by 5.
Opposite = y
Adjacent = x


We need to do maths for that now.

Here is the formula for the adjacent side (what we are trying to find)
Cos(A) = adj / hyp

Here is the formula for the opposite side (what we are trying to find)
Sin(A) = opp / hyp

Since we have the hypotenuse, and opp and adj are variables, we should multiply hyp over.
hyp * Cos(A) = adj
hyp * Sin(A) = opp


Now we have obvious means.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
// define some variables

float hypotenuse = 1f; // our distance we want to travel
float rotation = 0f; // the rotation of our object around the y axis (yaw), in degrees

// do the calculations

float adjacent = hypotenuse * Math.cos(Math.toRadians(rotation)); // note, toRadians because we were working in degrees
float opposite = hypotenuse * Math.sin(Math.toRadians(rotation)); // cos and sin only accept radians

// move the camera

Camera.x += opposite;
Camera.z += adjacent;


That wasn't so bad. You want to make this code run when you have arrow key up or W pressed. This allows you to move forward. If you want to move backwards instead, repeat the code... but switch the +='s with -=, naturally.

If you increase and decrease the rotation with keys A and D plus move the object as shown, you get a normal FPS camera, but you maybe want strafing. Strafing is the act of looking forward, but moving to the side, still looking forward. Like the hammer time dance.

<a href="http://www.youtube.com/v/otCpCn0l4Wo?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/otCpCn0l4Wo?version=3&amp;hl=en_US&amp;start=</a>

If you think about it, we are going to rotate 90 degrees to the right and 90 degrees to the left then move. Naturally, we want to not alter the rotation permanently, because we don't want to instantly face the way we are walking... or have to translate the rotation back. This would be an example of redundancy. So we will just add 90 degrees to our Yaw (y) temporarily.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
// code as before

float hypotenuse = 1f;
float rotation = 0f;

// manipulated rotation with not permanent 90 degrees

float adjacent = hypotenuse * Math.cos(Math.toRadians(rotation+90));
float opposite = hypotenuse * Math.sin(Math.toRadians(rotation+90));

Camera.x += opposite;
Camera.z += adjacent;


That wasn't so bad. You want to make this code run when you have arrow key left or A pressed. This allows you to move left. If you want to move right instead, repeat the code... but switch the +='s with -=, naturally.

Alternatively, you can do use ternary operations.

1  
float hypotenuse = isADown() || isWDown() ? 1f : isDDown() || isSDown() ? -1f : 0f;


Anything I didn't go over? Any questions? I will edit this post.

This wiki entry has had 0 revisions with contributions from 1 members. (more info)
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (3673 views)
2018-11-16 13:46:03

hadezbladez (1316 views)
2018-11-16 13:41:33

hadezbladez (3679 views)
2018-11-16 13:35:35

hadezbladez (710 views)
2018-11-16 13:32:03

EgonOlsen (4020 views)
2018-06-10 19:43:48

EgonOlsen (4546 views)
2018-06-10 19:43:44

EgonOlsen (2706 views)
2018-06-10 19:43:20

DesertCoockie (3575 views)
2018-05-13 18:23:11

nelsongames (3722 views)
2018-04-24 18:15:36

nelsongames (4753 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!