Walk character minimal path

Hello ,help help !:slight_smile:
I 'm trying to move an Sinbad character from a location to Another location in the physics space or more clearly I’m doing the AI movement, someone correct me, if I think wrong.

Now I calculate the minimal path and set the physics location in this way :

[java]
//update loop
@Override
public void simpleUpdate(float tpf) {
// player
camDir = cam.getDirection().clone().multLocal(0.6f);
camLeft = cam.getLeft().clone().multLocal(0.4f);
walkDirection.set(0,0,0);
if(left)
walkDirection.addLocal(camLeft);
if(right)
walkDirection.addLocal(camLeft.negate());
if(up)
walkDirection.addLocal(camDir);
if(down)
walkDirection.addLocal(camDir.negate());
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());

	//oto
	moveSinbad(tpf);
}

public void moveSinbad(float tpf){
	  Vector3f walk = new Vector3f();
   //physics position 
		currentX = character.getPhysicsLocation().x;
		currentZ = character.getPhysicsLocation().z;
	System.out.println("position Sinbad--->>>   "+currentX+ "  "+currentZ);	
		
     if(currentX == -340 && currentZ == 50 ){
    	 //destination
        System.out.println("********************* POSIZIONE RAGGIUNTAAAAAAAA *************************");	 
     }
     else if(!graphLand.getMininalPath().isEmpty()){
    	 
    	currentX = graphLand.minimalPath.getFirst().row;
    	currentZ = graphLand.minimalPath.getFirst().col;

			    	//--------------------------------------------
			    	/*
			    	walk.x = ?    <<<----- PROBLEM
			    	walk.z = ?    <<<-----
			    	 if (!character.onGround()) {
			             airTime = airTime + tpf;
			         } else {
			             airTime = 0;
			         }
			         if (walk.length() == 0) {
			             if (!"IdleTop".equals(animationChannel.getAnimationName())) {
			                 animationChannel.setAnim("IdleTop", 1f);
			             }
			         } else {
			             character.setViewDirection(walk);
			             if(airTime> .5f){
			                  if(!"RunBase".equals(animationChannel.getAnimationName())){
			                       animationChannel.setAnim("RunBase", 0.7f);
			                  }
			              }
			         }
			         character.setWalkDirection(walk);
			         */
        //-------------------------------------------------    	
    	//remove the cell occupated
    	graphLand.getMininalPath().removeFirst();
    	//set physics location
    	character.setPhysicsLocation( new Vector3f( currentX ,character.getPhysicsLocation().y,currentZ ) );
     }
}

[/java]

The result is that the Character is moved so I’m happy , but I don’t know how to set/modify the walkX and walkZ for walk of character??
thanks in advance

find the direction in world space (destination - current) and that is the walk direction vector. If you want to accumulate movement, then just add the 2 β€œwalk” direction vectors together (I notice you already set the walk direction previously, only the last one will take effect)

If you want to accumulate movement ?? I don’t understand scuse me, how I consider the cell of minimal path when I set the walk?
I may have been unclear,I wanna that for every cell (x,z)of path, the character walk

please can you explain me clearly and speak about the code …It’s my first time

[java]
…
else if(!graphLand.getMininalPath().isEmpty()){
//cell to occupy
currentX = graphLand.minimalPath.getFirst().row;
currentZ = graphLand.minimalPath.getFirst().col;

                    //——————————————–
                    /*
                    walk.x = ?    <<<—– PROBLEM
                    walk.z = ?    <<<—–
                     if (!character.onGround()) {
                         airTime = airTime + tpf;
                     } else {
                         airTime = 0;
                     }
                     if (walk.length() == 0) {
                         if (!”IdleTop”.equals(animationChannel.getAnimationName())) {
                             animationChannel.setAnim(β€œIdleTop”, 1f);
                         }
                     } else {
                         character.setViewDirection(walk);
                         if(airTime> .5f){
                              if(!”RunBase”.equals(animationChannel.getAnimationName())){
                                   animationChannel.setAnim(β€œRunBase”, 0.7f);
                              }
                          }
                     }
                     character.setWalkDirection(walk);
                     */
        //β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-
        //remove the cell occupated
        graphLand.getMininalPath().removeFirst();
        //set physics location
        character.setPhysicsLocation( new Vector3f( currentX ,character.getPhysicsLocation().y,currentZ ) );
     }

[/java]

The direction can be calculated by subtracting the current position from the destination.

[java]
Vector3f destination = …
Vector3f currentPos = …

Vector3f direction = destination.subtract(currentPos);
[/java]

The specifics exactly on how to get a character from position A to position B at a constant speed is something I have not toyed with yet, but a starting point would be:

[java]
Vector3f walkSpeed = new Vector3f(0, 0, 0);

if (direction.getX()) > .1f) walkSpeed.addLocal(1f, 0, 0);
if (direction.getZ()) > .1f) walkSpeed.addLocal(0, 0, 1f);
[/java]

Up till direction you were doing fine.

For constant speed just do direction.normalizeLocal().multLocal(speed)

1 Like