Need some advice on Map mouvement code

Hello, i think i just created a monster and i kinda need someone to tell me if this code is right or kinda garbage. am realy new to all of this but it fell to me like it should consume a little bit to much of the machine power but as it work perfecly (after many test) am not realy sure…

note thas everything that is hide is something i did try to put in the programm but for some reason fail me. As said in the title, this code is use to move my camera aroud to look on the map at a kinda 3 perso view point. As also said, the problem here is not the fact that it do not work, my problem is efficacy.

[java]

  private void calculeVector()
  {
	  //gauche droite
	 
	 gauche =  (app.getCamera().getLeft().clone().mult(mul)) ;
//	 droite = gauche.mult(-1f*mul) ; 
	 
	 arrier = new Vector3f(gauche.getZ()-gauche.getX(),0f,gauche.getX()-gauche.getZ());
	 avant = arrier.mult(-1) ;
         
  }
  
  
  
  private AnalogListener analogListener = new AnalogListener() {
    public void onAnalog(String name, float value, float tpf) {
      if (isRunning) {
       
    	  
        if(name.equals("Top"))
	    {
        	  Vector3f vec = new Vector3f(app.getCamera().getLeft());
    		  app.getCamera().setLocation(app.getCamera().getLocation().add(new Vector3f(vec.getZ()-vec.getX(),0f,vec.getX()-vec.getZ()).mult(3*mul*tpf)));
    		 
        //	app.getCamera().setLocation(app.getCamera().getLocation().add((avant).mult(tpf)));    
	        	
	    }
	    else if (name.equals("Bot")) 
	    	
	    {
	    	
	    	Vector3f vec = new Vector3f(app.getCamera().getLeft());
    		  app.getCamera().setLocation(app.getCamera().getLocation().add(new Vector3f(vec.getZ()-vec.getX(),0f,vec.getX()-vec.getZ()).mult(3*-mul*tpf)));
    		 
	    	// app.getCamera().setLocation(app.getCamera().getLocation().add((arrier).mult(tpf)));  	
	    }
        if (name.equals("Right")) 
        {
        	// app.getCamera().setLocation(app.getCamera().getLocation().add((droite).mult(tpf))); 
        	app.getCamera().setLocation(app.getCamera().getLocation().add(app.getCamera().getLeft().mult(-mul*tpf))) ;
        }
        else if (name.equals("Left")) 
        {
        	// app.getCamera().setLocation(app.getCamera().getLocation().add((gauche).mult(tpf))); 
        	app.getCamera().setLocation(app.getCamera().getLocation().add(app.getCamera().getLeft().mult(mul*tpf))) ;
        }
        if(name.equals("Center"))
        {
    //    /*
        	Vector3f vecGauche = new Vector3f(app.getCamera().getLeft());
        	Vector3f recul = new Vector3f((vecGauche.getZ()-vecGauche.getX()),0f,vecGauche.getX()-vecGauche.getZ()) ;
        	Vector3f vec = new Vector3f(perso.forme.getWorldTranslation().x - (recul.x*300) ,// x
        			app.getCamera().getLocation().y,  // y
        			perso.forme.getWorldTranslation().z - (recul.z*300)) ;  // z
        	//		*/
        	//app.getCamera().
        	app.getCamera().setLocation(vec);
        }
    	  
    	  [/java]

Only thing I can see at first glance is that you create a lot of Vector3f. This code is gonna be called on each loop, so potentially you generate a lot of garbage.
Using a temp vector, created in the class could save some cycle/garbage. Not a big deal though.

I didn’t go in to the details of the computation, but if it works as you expect it’s already a nice form of efficiency :wink:

1 Like

On quick glance it looks ok though you make a lot of vector3f copies unnecessarily.

For example:
[java]
gauche = (app.getCamera().getLeft().clone().mult(mul)) ;
[/java]
Could just be:
[java]
gauche = app.getCamera().getLeft().mult(mul) ;
[/java]
…and the effect is the same.

Likewise:
[java]
Vector3f vec = new Vector3f(app.getCamera().getLeft());
app.getCamera().setLocation(app.getCamera().getLocation().add(new Vector3f(vec.getZ()-vec.getX(),0f,vec.getX()-vec.getZ()).mult(3multpf)));
[/java]

Can be shortened to:
[java]
Vector3f vec = app.getCamera().getLeft();
app.getCamera().setLocation(app.getCamera().getLocation().add(new Vector3f(vec.getZ()-vec.getX(),0f,vec.getX()-vec.getZ()).multLocal(3multpf)));
[/java]

Though now that I look at it, the (z-x, 0, x-z) thing looks extremely strange. What is it that you think that is doing?

Also, you will have more fun with your code if you go ahead and grab some values ahead of time. For example, the above would be even clearer as:
[java]
Camera cam = app.getCamera();
Vector3f left = cam.getLeft();
Vector3f pos = cam.getLocation();
pos = pos.add(new Vector3f(left.getZ()-left.getX(),0f,left.getX()-left.getZ()).multLocal(3multpf)));
cam.setLocation(pos);
[/java]

…and a hundred times easier to debug if there are issues.

1 Like

Nehon, thanks for the encouragement, but my problem is for later on :stuck_out_tongue: if this code consume to much no matter how well it work, the program won’t so it will be a problem. But Il try the temp.
ps i Did try the temp and look like it work even better.

For psseed Ya. i must admit that my calculeVector fonction is quite a mess, and it do not work anyway, so i guess i should remove it. Not even sure why i put that clone there. I think your also right for the simplification part. Even if i doubt anyone will have any correction to do execpt me, it is good to have a clear coding. For what is of the (z-x, 0, x-z ) for some reason this was the only way i fond to make the camera go foward and backward. I know it is wierd, but for some reason it work great. But if you can propose me any orther way around, it could be great.

Anyway as no one seem thromathise per the fact that i call Vector and make cacul on them a million time i guess i sould not be to worry. so here is the code now
[java]

int mul = 20 ;
Vector3f vec ;

  float recul = 100 ;
  Vector3f vect ;
  Vector3f avant ;
  
  Camera cam ; 
  
  private AnalogListener analogListener = new AnalogListener() {
    public void onAnalog(String name, float value, float tpf) {
      if (isRunning) 
      {
       
    	vec =  app.getCamera().getLeft();
    	cam = app.getCamera() ;
    	avant = app.getCamera().getDirection() ;
        if(name.equals("Top"))
	    {
    	
        	cam.setLocation(cam.getLocation()
        			.add(new Vector3f(avant.x,0f,avant.z)
        			.mult(mul*tpf))) ;
	        	
	    }
	    else if (name.equals("Bot")) 
	    	
	    {
	  
	    	cam.setLocation(cam.getLocation()
	    			.add(new Vector3f(avant.x,0f,avant.z)
	    			.mult(-mul*tpf))) ;
	    	
	    	
	    }
        if (name.equals("Right")) 
        {
        	cam.setLocation(
        			cam.getLocation()
        			.add(vec.mult(-mul*tpf))) ;
        }
        else if (name.equals("Left")) 
        {
        	cam.setLocation(
        			cam.getLocation()
        			.add(vec.mult(mul*tpf))) ;
        }
        if(name.equals("Center"))
        {
   
        
        	vect = new Vector3f(
        			perso.forme.getWorldTranslation().x - (avant.x*recul) ,// x
        			cam.getLocation().y,  // y
        			perso.forme.getWorldTranslation().z - (avant.z*recul)) ;  // z
        	
        	cam.setLocation(vect);
        }
    	
        
        
      } 
      
    }
  };
  
 [/java] 

Execpt for the perso part in the center option it should work great for anyone i belive

To move left and right, use getLeft() as you are.

To move forward and back, use getDir().

To move up and down, use getUp()…

If it’s that you just need to move in the x,z plane then 0 out y as needed (and renormalize).

The z-x, 0, x-z thing is strange I’m not sure why it works.

If you haven’t been through this yet then I recommend it: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

1 Like

After some test, where i changed the angle i was working whit, I realise that my calcul had a little(huge) flaw, and did not realy work. Thanks for the getDir(), this one work and am not realy sure i would had guess it. Anyway i did reports my code, so now it would work and i fell.

Thanks again.