[SOLVED] AnalogListener lags every few seconds

Hello im new to JME engine and got into a bit of trouble with analog listener. Have this listener for mouse click as ActionListener and as AnalogListener for mouse movement. Everything works fine, but there is lag for the input AnalogListener every few seconds, between each lag its working as expected (smooth transition as the mouse moves). They are only related to the listener since everything else is going smoothly. Does anyone know, what could cause this lag?
Thanks in advance.

public class BuildListener implements AnalogListener,ActionListener{

Node planetCenterNode;
Geometry mark;

@Override
public void onAction(String name, boolean isPressed, float tpf) {
    
    if(name.equals(Constants.BUILDING_CONSTRUCT_ACTION) && !isPressed){
        placeBuildingAction(isPressed, tpf);
    }
}

@Override
public void onAnalog(String name, float value, float tpf) {
    if(name.equals(Constants.BUILDING_NEW_LOCATION_ACTION)){
        newLocationAction(value, tpf);
    }
}

private void placeBuildingAction(boolean isPressed, float tpf){
    
    HitPosNormalPack posHitAndNormal = getPosHitAndNormal();
    //if the mouse event didnt result in any planet hit
    //when changing states, the click event for planet selection will stay and invoke also new listener added
    if(!posHitAndNormal.isHit() || mark == null){
        return;
    }
    
    Vector3f posHit = posHitAndNormal.getPosHit();
    Vector3f contactNormal = posHitAndNormal.getContactNormal();
    
    //translate locally to the surface of hit planet
    mark.setLocalTranslation(posHit);
    
    Vector3f worldNormal = new Vector3f();
    planetCenterNode.localToWorld(contactNormal, worldNormal);
    //rotate along the normal vector on hit position of the planet
    mark.lookAt(worldNormal, contactNormal);
    mark = null;
}

/**
 * Triggered, when mouse pointer is moving along surface of some planet
 * @param isPressed
 * @param tpf 
 */
private void newLocationAction(float value, float tpf){
    
    HitPosNormalPack posHitAndNormal = getPosHitAndNormal();
    //if the mouse event didnt result in any planet hit
    if(!posHitAndNormal.isHit()){
        //if mouse pointer is not on planet anymore detach temp building, if there is any
        if(mark != null){
            planetCenterNode.detachChild(mark);
        }
        mark = null;
        return;
    }
    
    Vector3f posHit = posHitAndNormal.getPosHit();
    Vector3f contactNormal = posHitAndNormal.getContactNormal();
    
    if(mark == null){
        mark = initMark();
        planetCenterNode.attachChild(mark);
    }
    //translate locally to the surface of hit planet
    mark.setLocalTranslation(posHit);
    
    Vector3f worldNormal = new Vector3f();
    planetCenterNode.localToWorld(contactNormal, worldNormal);
    //rotate along the normal vector on hit position of the planet
    mark.lookAt(worldNormal, contactNormal);
    
    
}

private HitPosNormalPack getPosHitAndNormal(){
    Node shootables = PlayerInfo.selectedPlanet;
    InputManager inputManager = Core.getInputManager();
    Camera cam = Core.getCamera();
    
    // 1. Reset results list.
    CollisionResults results = new CollisionResults();
    // 2. Aim the ray from cam loc to cam direction.
    Vector2f mouseCoords = new Vector2f(inputManager.getCursorPosition());
    //creates a ray from mouse click on screen
    Ray ray =  new Ray(cam.getWorldCoordinates(mouseCoords, 0),cam.getWorldCoordinates(mouseCoords, 1).subtractLocal(cam.getWorldCoordinates(mouseCoords, 0)).normalizeLocal());
    // 3. Collect intersections between Ray and Shootables in results list.
    shootables.collideWith(ray, results);
    // 4. Print the results
    
    // 5. Use the results (we mark the hit object)
    if (results.size() > 0) {
        
        CollisionResult closest = null;
        for (CollisionResult result : results) {
            if(result.getGeometry().getName().equals("Planet")){
                
                if(closest == null){
                    closest = result;
                }
                else if(closest.getDistance() > result.getDistance()){
                    closest = result;
                }
                
            }
        }
        
        if(closest == null){return HitPosNormalPack.NO_HIT;}
        
        Geometry geom = closest.getGeometry();

        Vector3f posHit = closest.getContactPoint();
        Vector3f contactNormal = closest.getContactNormal();
        
        planetCenterNode = geom.getParent();
        
        planetCenterNode.worldToLocal(posHit, posHit);
        planetCenterNode.worldToLocal(contactNormal, contactNormal);
        //for some reason the normals are not affected by parent node translation
        //need to artificially add the translation unlike at the posHit
        contactNormal = contactNormal.add(planetCenterNode.getLocalTranslation());
        
        return new HitPosNormalPack(posHit, contactNormal, true);
    }
    
    return HitPosNormalPack.NO_HIT;
}


private Geometry initMark() {
    
    AssetManager assetManager = Core.getAssetManager();

  Box box = new Box(1, 1, 1);
  Geometry mark = new Geometry("BOOM!", box);
  Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  mark_mat.setColor("Color", ColorRGBA.Red);
  mark.setMaterial(mark_mat);
  
  return mark;
}

}

//EDIT

In further testing i discovered, that it does not lag when im doing mouse movement on small area of the sphere, but it seems to be lagging when im moving on a broader area of the sphere. Maybe my coordinates are wrong for position of the mouse on sphere based on where i started the movement for some reason.

//EDIT

The coordinations are correct, there just arent any events. I measured delay between each event and the coordinates and here is the result in the time of lag:

MOUSE MOVEMENT b_newLoc : 6557112504220 delay 16896599
PosHit (1.7289478, -2.191358, -4.1298966)
MOUSE MOVEMENT b_newLoc : 6557145522003 delay 33017783
PosHit (1.1331825, -2.1963446, -4.334965)
MOUSE MOVEMENT b_newLoc : 6557162258860 delay 16736857
PosHit (0.8095907, -2.2123504, -4.397333)
MOUSE MOVEMENT b_newLoc : 6557612296734 delay 450037874 <-------
PosHit (-2.7134838, 0.79821426, -4.1038175)
MOUSE MOVEMENT b_newLoc : 6557628868473 delay 16571739
PosHit (-2.6306572, 1.0229583, -4.1101756)
MOUSE MOVEMENT b_newLoc : 6557645617106 delay 16748633
PosHit (-2.5901968, 1.1663435, -4.104488)

As you can see there is considerably higher delay and ofc big jump in coordinates. (Its time in nano and delay is last event time - current time). Of course the time should differ a bit based on my mouse movement speed and smoothness, but there really should not be around 30-40 times difference at one point.

I figured it out myself. It was because i didnt listen to ALL movement of the mouse. Only to positive direction on each axis, which cause the lag.
I used only this in mapping:

 new MouseAxisTrigger(MouseInput.AXIS_X,true);
 new MouseAxisTrigger(MouseInput.AXIS_Y,true);

which listens only to positive direction on each axis. Correct way is this:

 new MouseAxisTrigger(MouseInput.AXIS_X,true);
 new MouseAxisTrigger(MouseInput.AXIS_Y,true);
 new MouseAxisTrigger(MouseInput.AXIS_X,false);
 new MouseAxisTrigger(MouseInput.AXIS_Y,false);

in mapping for mouse movement. Hope this will help somebody else to figure it out quickly. These kinds of “stupid” things are probably most annoying to figure out. :slight_smile:

Usually lag (though i hate the term lag with a passion - what does it even mean!?!?! - its a generalization for everything!) means that the application has temporarily stopped responding (think thread.sleep()) - whereas you are describing the application not responding to your input. Oh how I wish the world would rid itself of that word.

1 Like

Thats why i was talking about input lag, which perfectly describes the problem in my opinion, whereas you are talking about game lag or FPS drop. Lag in general means something stops responding or “lags” for some period of time. So you think, that instead of saying game lagged its better to say game stopped responding? Because lags for me means something temporary, for example sudden FPS drop, whereas the “stopped responding” is something long term such as the game crashed or similar. But forgive me my bad english, its not my native language so maybe you are right here. I just tried to describe the problem as precisely as i could.

No no, your english is perfectly fine. My response was more of a rant than a correction :slight_smile:

My favorites are from online games. “It took two seconds for my character stats window to open! I have an i7, what’s wrong with this piece of junk?!” “I get an FPS drop when 5 effects trigger at once. I’m gonna go upgrade my internet connection!”

@dratschek in the end how did you fix the issue?

I wrote the answer why it didnt work. It was caused by me using the MouseAxisTrigger in a wrong way.

oh sorry, I skipped your solution message. Thanks :slight_smile: