Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
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 2378 times)
0 Members and 1 Guest are viewing this topic.
Offline justRoss

Senior Newbie


Medals: 2
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
« 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: 438
Projects: 7
Exp: 7 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  
 
 

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

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

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

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

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

nelsongames (1704 views)
2018-04-24 18:14:32

ivj94 (2471 views)
2018-03-24 14:47:39

ivj94 (1686 views)
2018-03-24 14:46:31

ivj94 (2770 views)
2018-03-24 14:43:53

Solater (909 views)
2018-03-17 05:04:08
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
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!