Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (775)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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  
  Why does my camera's forward motion 'snap' to weird yaw angles?  (Read 1136 times)
0 Members and 1 Guest are viewing this topic.
Offline pixelprime

Junior Devvie

Medals: 3

« Posted 2014-01-11 20:43:05 »

Hi all,

I've been using this camera movement code (below) for ages now, but it has a strange bug that I've never been able to crack.

First, here's the code. The variables pitch and yaw represent the camera's yaw and pitch angles in degrees (in the 0-359 range).

public void moveCamForwardWithValue(float value)
      double cameraPitchInRadians = Math.toRadians(pitch);
      double cameraYawInRadians = Math.toRadians(yaw);
      float f = (float)Math.cos(cameraPitchInRadians);
      // prevent pitch angle from affecting forward speed
      if (!flyMode) { f = 1; }
      float addX = value  * f * (float)Math.sin(cameraYawInRadians);
      float addZ = value  * f * (float)Math.cos(cameraYawInRadians);
      float addY = value  * (float)Math.sin(cameraPitchInRadians);
      speedX -= addX;
      speedZ += addZ;
       if (flyMode) { speedY += addY; }

What happens is that the user presses the 'forward' key (such as W) and moveCamForwardWithValue is called with a suitable input value.

Every frame the camera's position is modified by the speedX and speedZ values (and speedY if flight mode is enabled).

The camera moves forward fine, but the YAW angle it moves along doesn't always seem to be correct. Even stranger is that if I stay moving long enough, the movement 'snaps' to another yaw angle, almost as if it's eventually snapping to the yaw angle it should've been on all along.

I should add that the flyMode variable is a toggle to allow the camera to travel up / down along the pitch vector (as if flying) for debugging purposes.

I hope all of the above makes sense, please let me know if any further clarification is needed.

Tearing my hair out on this one!

Thanks Smiley
Offline Drenius
« Reply #1 - Posted 2014-01-11 23:34:54 »

This code looks ok so far, could you ... maybe make a video or a more detailed explanation of your problem?
Oh, and must addX really be subtracted?
Offline zFollette

Junior Devvie

Exp: 2 years

I like jokes

« Reply #2 - Posted 2014-01-12 17:11:18 »

Assuming you are using LWJGL, are you calling glLoadIdentity() before you call glTranslate()?

Humor will keep you alive.
Pages: [1]
  ignore  |  Print  

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

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

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

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

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

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

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

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

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

nelsongames (2047 views)
2018-04-24 18:14:32
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

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 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‑
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!