Jumpy / Glitchy Spatial Translation

I’m trying to move certain NPCs around the map without using too many character controls as I’ve found that significantly lowers FPS, but I’m having an error using intersection and ray casting.

Here’s a snippet of my code located in the parent NPC class i’m using. Everything works fine and I’ve almost got this to work perfectly. When an NPC with a physics control chases you next to a non-physics NPC they appear to work identically, except there seems to be something causing the non-physics NPCs to occasionally jump up or down by 6-10 Y value, as if i’m modifying its local translation elsewhere.

 walkDir.setY(0f);
          if(charControl != null){
              charControl.setWalkDirection(walkDir);
          }
          else{
             Vector3f headLoc = headNode.getWorldTranslation();
             Ray ray = new Ray(headLoc, new Vector3f(0f,-.1f,0f));
             Vector3f intersectionPoint = gameState.getSightCollision(ray);  // returns the closest collision with my world
                 Vector3f dif = intersectionPoint.subtract(headLoc);
                 dif.setY(dif.getY() + distFromHeadToGround); //distFromHeadToGround is assigned just once in NPC constructor 
                 centerNode.move(dif);
             }
             
             centerNode.move(walkDir.mult(tpf));
          }

I also tried another way with no rays, but used a bounding box placed at the NPCs feet to check for intersection with the world, and I got the same results.

Does anyone have any insight or ideas as to what could be causing this? When i use physics body controls for everything I can barely use 20 before my game is unplayable, but I can easily spawn 50-60+ NPCs using ray collision as a replacement, I just can’t figure out why my terrain is returning inconsistent collision Y values for the collision location on a flat test map. I use the same type of code elsewhere for placing a rooting effect and spatial at the base of an npc: it casts a ray to the ground and places the effect on the ground correctly every time, but when I try to use the same methodology like i posted above for controlling a character it turns out very glitchy and the npc sometimes hovers or sinks into the ground. I can post pictures or a short video if that would be any help explaining what’s happening

Could be a hundred different things, in your code or JME’s… you will have to narrow it down by debugging.

I started debugging some more and disabled a lot of extra code running with the NPCs until I narrowed it down and tested just one trigger. But you were right it was something else in my code that i didn’t think was related since it never gave me issues when I was using physics controls. It ended up being a spot I needed to clone the target’s location and forgot to, previously I had this line of code without .clone().

   Vector3f spot = target.getHead().getWorldTranslation().clone();
   spot.setY(spot.getY() + yDifference);
   dir = spot.subtract(centerNode.getWorldTranslation());

And I guess when I had the spatial and its headNode attached to a BetterCharacterControl it didn’t cause this problem. Although that does have me somewhat confused still and that’s probably why I made this mistake in the first place, I just lucked out that it wasn’t a problem until now. But was I somehow altering the location of my targets headNode when I stored it’s location as a vector? I would have assumed that if I didn’t clone the node ( the method target.getHead() returns a node) then that would reference the headNode, but does altering spot also alter the location of the headNode then?

When objects are physical, their positions are constantly reset from their rigid bodies… so nothing you do to the local translation matters.

When they are not physical then you need to avoid modifying their vector like this (without cloning).

1 Like

That makes sense I appreciate the help, I’m messing with alot of uncloned vectors for many of my spells and npcs without physics that i need to change then. I appreciate the help and explanation :smiley: