Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (223)
games submitted by our members
Games in WIP (813)
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  
  laggy camera movement  (Read 1132 times)
0 Members and 1 Guest are viewing this topic.
Offline justRoss

Senior Newbie


Medals: 1
Exp: 6 years



« Posted 2017-03-17 23:04:33 »

hello guys.
I'm working on some tiny 3d game with fps camera and lately I stuck with this annoying bug That I can't find fix to.
the problem is that when I move my camera around and move to any direction I got some render stutters/lags while the fps is stable fixed at 60fps.
I found out that this happens only when I move the mouse and not from raw keyboard movement.

I think it's have something to do with my math but I'm not sure about it.
can someone take a look at my mouse class?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
package com.raise3d.engine.scene.behaviors;

import org.joml.Vector3f;
import com.raise3d.engine.assets.AssetsManager;
import com.raise3d.engine.input.Input;
import com.raise3d.engine.input.Keyboard;
import com.raise3d.engine.main.MainLoop;
import com.raise3d.engine.scene.Actor;
import com.raise3d.engine.scene.Behavior;

public class Camera implements Behavior
{
   private float camera_speed;
   private float sensitivity;
   public static float yaw, pitch;
   
   public void OnInit(Actor self, AssetsManager assets, Input input) // init method
   {
      camera_speed=0.005f;
      sensitivity=0.15f;
      yaw=0;
      pitch=0;
      self.setDirection(new Vector3f(0,0,0));
      self.setLocation(new Vector3f(0,1.75f,0));
   }
   
   public void OnStep(Actor self, Input input, float delta) // update method
   {
      float speed=camera_speed*delta;
      float xoffset=(input.getMouse().getMouseOffsetX()*sensitivity);
      float yoffset=(input.getMouse().getMouseOffsetY()*sensitivity);
      if (xoffset>10)
      {
         xoffset=10;
      }
     
      if (xoffset<-10)
      {
         xoffset=-10;
      }
     
      if (yoffset>10)
      {
         yoffset=10;
      }
     
      if (yoffset<-10)
      {
         yoffset=-10;
      }

      if (xoffset!=0 || yoffset!=0)
      {
         yaw+=xoffset;
         pitch+=yoffset;        
         if(pitch > 89.0f)
         {
            pitch =  89.0f;
         }
         if(pitch < -89.0f)
         {
            pitch = -89.0f;
         }
         Vector3f d = new Vector3f();
         d.x = (float) (Math.cos(Math.toRadians(pitch)) * Math.cos(Math.toRadians(yaw)));
         d.y = (float) Math.sin(Math.toRadians(pitch));
         d.z = (float) (Math.cos(Math.toRadians(pitch)) * Math.sin(Math.toRadians(yaw)));
         d.normalize();  
         self.setDirection(d);
      }
     
      Vector3f motion = new Vector3f();
      Vector3f moved = new Vector3f();
      moved.x = (float) (Math.cos(Math.toRadians(0)) * Math.cos(Math.toRadians(yaw)));
      moved.y = (float) Math.sin(Math.toRadians(0));
      moved.z = (float) (Math.cos(Math.toRadians(0)) * Math.sin(Math.toRadians(yaw)));
      moved.normalize();  
     
      if (input.getKeyboard().isKeyDown(Keyboard.KEY_W))
      {
         motion.add(moved);
      }
     
      if (input.getKeyboard().isKeyDown(Keyboard.KEY_S))
      {
         motion.sub(moved);
      }
     
      if (input.getKeyboard().isKeyDown(Keyboard.KEY_D))
      {
         motion.add(moved.cross(new Vector3f(0,1.0f,0)).normalize());
      }
     
      if (input.getKeyboard().isKeyDown(Keyboard.KEY_A))
      {
         motion.sub(moved.cross(new Vector3f(0,1.0f,0)).normalize());
      }
     
      if (input.getKeyboard().isKeyDown(Keyboard.KEY_LEFT_SHIFT))
      {
         speed*=2;
      }
     
      if (input.getKeyboard().isKeyDown(Keyboard.KEY_LEFT_CONTROL))
      {
         speed/=2;
      }

      self.setLocation(self.getLocation().add(motion.mul(speed)));
      MainLoop.getAudioManger().setListenerPosition(self.getLocation());
      MainLoop.getAudioManger().setListenerDirection(self.getDirection());
      MainLoop.getRender().setCameraLocation(self.getLocation());
      MainLoop.getRender().setCameraDirection(self.getDirection());
      MainLoop.getRender().UpdateFrustum();
     
   }

   public String getName()
   {
      return "camera";
   }

   public void OnCollide(Actor self, Actor other)
   {
     
   }
}
Offline cygnus

Junior Devvie


Medals: 7
Exp: 1 year



« Reply #1 - Posted 2017-05-04 00:52:48 »

If the keyboard input does, as you say it does, experience no lag, then the problem seems it must be with your input system's updating. I don't know what could be wrong with that, that's just where I would look next.
Offline orange451

JGO Kernel


Medals: 342
Projects: 6
Exp: 6 years


Your face. Your ass. What's the difference?


« Reply #2 - Posted 2017-05-05 18:07:17 »

Are you using lwjgl3 or lwjgl2?

If you are using lwjgl2, there was a bugged version which reading the polled mouse position would lag. I told Spasi about this a few months (a year?) ago and he fixed it.

First Recon. A java made online first person shooter!
Pages: [1]
  ignore  |  Print  
 
 

 
cybrmynd (120 views)
2017-08-02 12:28:51

cybrmynd (144 views)
2017-08-02 12:19:43

cybrmynd (138 views)
2017-08-02 12:18:09

Sralse (154 views)
2017-07-25 17:13:48

Archive (624 views)
2017-04-27 17:45:51

buddyBro (733 views)
2017-04-05 03:38:00

CopyableCougar4 (1260 views)
2017-03-24 15:39:42

theagentd (1239 views)
2017-03-24 15:32:08

Rule (1216 views)
2017-03-19 12:43:22

Rule (1268 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!