Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
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  
  Free-flying camera Y axis movement too fast, is tangent vector calc wrong?  (Read 1322 times)
0 Members and 1 Guest are viewing this topic.
Offline pixelprime

Junior Devvie

Medals: 3

« Posted 2013-02-20 10:33:23 »

Hi All,

I've written a camera class to handle FPS-style movement throughout the game world, but for some reason I don't seem to be traveling the correct distance along the Y axis when pointing in that direction. Here's the code that handles the calculation of 'moving forward' along the current vector:

protected void moveForward(float distance)
      // track value (for debugging)
      lastMoveForward = distance;
      speedX -= (distance * (float)Math.sin(Math.toRadians(yaw)));
      speedZ += (distance * (float)Math.cos(Math.toRadians(yaw)));
       if (flyMode) { speedY += distance * (float) Math.tan(Math.toRadians(pitch)); }

Some quick code notes:
yaw: a value between 0-359
pitch: a value clamped between -80 (looking down) and 80 (looking up)
speedX/Y/Z: values that are added to the camera's X, Y and Z positions respectively, then zeroed-out ready for the next tick.

Moving along the X and Z axes (when looking along those axes) works beautifully. The distance travelled is ~4 units per second.

However, when flyMode is switched on (for a free-flying camera), and looking up or down, the distance travelled is vastly different - more like 20-30 units per second.

Am I calculating the speed increment along the Y axis totally wrong? Or maybe a combination of them all?

Many thanks for any help anyone can offer. I'm already learning how tremendously resourceful this community can be : )
Offline DazKins
« Reply #1 - Posted 2013-02-20 12:23:12 »

You should not be using Math.tan() to calculate the new y value, You should be using Math.sin


Check out my Dev Blog:
Offline pixelprime

Junior Devvie

Medals: 3

« Reply #2 - Posted 2013-02-20 13:10:40 »

That's perfect! Thanks for your help!
Pages: [1]
  ignore  |  Print  

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

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

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

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

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

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

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

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

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

nelsongames (2569 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!