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.