Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (623)
Games in Android Showcase (176)
games submitted by our members
Games in WIP (676)
games currently in development
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Why does my camera's forward motion 'snap' to weird yaw angles?  (Read 431 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  

basil_ (40 views)
2015-09-30 17:04:40

shadowstryker (20 views)
2015-09-29 15:55:06

TheSpaceHedgehog (26 views)
2015-09-29 01:58:48

GamerC4 (50 views)
2015-09-24 21:10:38

GamerC4 (69 views)
2015-09-24 21:09:48

htuy (24 views)
2015-09-24 04:57:24

htuy (34 views)
2015-09-24 04:56:35

htuy (25 views)
2015-09-24 04:56:09

htuy (25 views)
2015-09-24 04:47:33

Roquen (62 views)
2015-09-21 12:54:28
Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej