Distance

Whats the best way for calculating the distance between two vectors??


Math.sqrt((v2.y-v1.y)

What will this give me? will it give me the distance between the two vectors as a float, i want to use this to determine the distnace between a player and the bot

Vector 1 is the player.getLocalTranslation

Vector 2 is the bot.getLocalTranslation



I found this somewhere which works well.


public float getDistance(Spatial s1, Spatial s2) {

        Vector3f s1Coords = (Vector3f)s1.getWorldTranslation().clone();

        Vector3f s2Coords = (Vector3f)s2.getWorldTranslation().clone();

        Ray s1Tos2 = new Ray(s1Coords, s2Coords.subtractLocal(s1Coords).normalizeLocal());

        //Get s2's surface position of model bound
        IntersectionRecord rayToS2 = s2.getWorldBound().intersectsWhere(s1Tos2);

        Vector3f s2SurfacePos = rayToS2.getIntersectionPoint(rayToS2.getFarthestPoint());

        //Get s1's surface position
        IntersectionRecord rayToS1 = s1.getWorldBound().intersectsWhere(s1Tos2);

        Vector3f s1SurfacePos = rayToS1.getIntersectionPoint(rayToS1.getFarthestPoint());

        //Draw line between the two objects
        FloatBuffer lineBuf = BufferUtils.createFloatBuffer(s2SurfacePos, s1SurfacePos);

        l.setVertexBuffer(0, lineBuf);

        return s1SurfacePos.distance(s2SurfacePos);
    }

… so without the hassles this would simply be:  dist = Vector3f_1.distance(Vector3f_2);

this is completely of the ball, but i am also tryna get the bot to follow the player, but it follows a bit then disappears into the ground or just freezes, do you no if i am doing anything wrong with this code







import com.jme.scene.Spatial;

import com.jme.math.Quaternion;

import com.jme.math.Vector3f;

import com.jme.math.FastMath;

import com.jme.math.Matrix3f;

import com.jme.scene.Node;

public class LOSController{



      private float predvel = 0.125f;//, preyvel = 0.003f;

        public PatternController pc;

    public Spatial predator;

    Spatial prey;

       

    Vector3f predpos = new Vector3f();

      Vector3f preypos = new Vector3f();

   



    public LOSController(Spatial predator, Spatial prey) {

        this.predator = predator;

        this.prey = prey;

       

    }



    public void updateCycle() {

        // Get The Positions of our Spatials

        predpos.set(predator.getLocalTranslation());

        preypos.set(prey.getLocalTranslation());

       

        doLineOfSightChase();

       

       

    }



    private void doLineOfSightChase() {

        Vector3f u = preypos.subtract(predpos).normalize();

        predator.lookAt(u, Vector3f.UNIT_Y);

        predator.setLocalTranslation(predpos.add(u.mult(1.0f)));

        //predator.lookAt(u, Vector3f.UNIT_Y);

    }

   

   

   

}


following seem to work for me (testcase based on Your code without speed adjustment)



note that predator and prey need to be centered at their local coordinate system to work properly.

You will also have to set the hight over terrain at each update cycle to avoid diving into the terrain or hovering above ground.




import com.jme.scene.Spatial;
import com.jme.math.Vector3f;

public class LOSController{
 
       private float predvel = 0.125f;//, preyvel = 0.003f;
//        public PatternController pc;
      
     public Spatial predator;
     public Spatial prey;

    
    public LOSController(Spatial predator, Spatial prey) {
        this.predator = predator;
        this.prey = prey;
      
    }
 
    public void updateCycle() {
      
        doLineOfSightChase();
      
        predator.updateGeometricState(1.0f,true);
    }
 
    private void doLineOfSightChase() {
       
        Vector3f u = prey.getLocalTranslation().subtract(predator.getLocalTranslation()).normalize();
        predator.lookAt(u, Vector3f.UNIT_Z);
        predator.setLocalTranslation(predator.getLocalTranslation().add(u));
        //predator.lookAt(u, Vector3f.UNIT_Y);
    }
}

Thanks for the help, appreciate it

could you explain what you mean with this please



note that predator and prey need to be centered at their local coordinate system to work properly.

Just a note but should be clear, if the center of the local coordinate system of a model is … well … out of body ? then the center follows the prey not the visible model.

This applies eg. if You create a box with: 

Box b = new Box("box", new Vector3f(-20, -100, 0), 20, 20, 20);



then the center of the box is off by -20, -100, 0 and the box seem to follow the prey way off.

(instead You could create a box with center 0,0,0 and set the local translation to -20, -100, 0 which works ok)



hope that explains it.

Just a question about your code, is it for an actual human model for a first person shooter, i implemented it and it still does the same thing, it just flies around the environment

plus, my view will be in firstperson, so how would i get the bot to follow the cam, coz when i used the code you gave to me the bot just stayed in the camera view

Just a question about your code, is it for an actual human model for a first person shooter, i implemented it and it still does the same thing, it just flies around the environment


Not really, this was just based on Your code to verify that it works. 
To prevent the predator flying over or sinking into the ground based on environment You will have to get the height of the environment (terrain?) at the location of the predator and adjust the local translation of the predator accordingly ( in LOSController.updateCycle() )

plus, my view will be in firstperson, so how would i get the bot to follow the cam, coz when i used the code you gave to me the bot just stayed in the camera view


Depends on what exactly You are trying to achive, You could for example define a node that follows the cam at some distance and use this as "bait" for the predator.

For a real FPS bot You will have to use something more advanced than that which switches between more than one behavior according to external influences ...

I don't know of the top of my head of a bot steering example, maybe You could take a look at shingoki's aircarrier source code for that ?