BasicGame camera question

Hi,
I am new to JMonkey. Using the BasicGame example the camera rotates. Now I want the camera to NOT rotate and instead move the camera in the direction.

Hi tareq,

Try to do something like that in your update loop :

[java]
public void simpleUpdate(float tpf) {

    Vector3f camTempPosition = cam.getLocation().clone();
    camTempPosition.addLocal(cam.getDirection().clone().mult(tpf));
    cam.setLocation(camTempPosition);
}

[/java]

And be sure to set flyCam.setEnabled(false);

@stomrage said: Hi tareq,

Try to do something like that in your update loop :

[java]
public void simpleUpdate(float tpf) {

    Vector3f camTempPosition = cam.getLocation().clone();
    camTempPosition.addLocal(cam.getDirection().clone().mult(tpf));
    cam.setLocation(camTempPosition);
}

[/java]

And be sure to set flyCam.setEnabled(false);

Not to pick at knits here… but the code above make multiple duplication’s of Vector3f when it is not necessary

getDirection().clone().mult() makes two copies of the V3.

This could be done in a single statement without cloning any of the vector3fs:

[java]
cam.setLocation(cam.getLocation().add(cam.getDirection().mult(tpf)));
[/java]

And actually… since you are altering the location vector itself… just use:

[java]
cam.getLocation().addLocal(cam.getDirection().mult(tpf));
[/java]

At least I’m fairly sure the last one works. /shrug There should be no magic behind setLocation as apposed to getLocation.alterThisShit()

1 Like

Ok captain thanks for the advice. At least i’m not an expert in term of optimisation so you help me too.

My feeling when i saw this answer :

@stomrage said: Ok captain thanks for the advice. At least i'm not an expert in term of optimisation so you help me too.

My feeling when i saw this answer :

Neither am I… that’s why I posted it. I was so proud that I noticed it, I had to share! :stuck_out_tongue:

With a few days of experience I found a way to get what I want:

[java]
import com.jme3.input.FlyByCamera;
import com.jme3.renderer.Camera;

public class MyFlyCamera extends FlyByCamera {

/**
 * Creates a new FlyByCamera to control the given Camera object.
 * @param cam
 */
public MyFlyCamera(Camera cam){
	super(cam);
}

@Override
public void onAnalog(String name, float value, float tpf) {
    if (!enabled) {
        return;
	}

	if (name.equals("FLYCAM_Up")) {
        moveCamera(value, false);
    } else if (name.equals("FLYCAM_Down")) {
        moveCamera(-value, false);
    } else if (name.equals("FLYCAM_Left")) {
        moveCamera(value, true);
    } else if (name.equals("FLYCAM_Right")) {
        moveCamera(-value, true);
	} else if (name.equals("FLYCAM_ZoomIn")) {
        zoomCamera(value);
    } else if (name.equals("FLYCAM_ZoomOut")) {
        zoomCamera(-value);
    }
}

}
[/java]

FlyByCamera.java

@t0neg0d said: Not to pick at knits here... but the code above make multiple duplication's of Vector3f when it is not necessary

getDirection().clone().mult() makes two copies of the V3.

This could be done in a single statement without cloning any of the vector3fs:

[java]
cam.setLocation(cam.getLocation().add(cam.getDirection().mult(tpf)));
[/java]

And actually… since you are altering the location vector itself… just use:

[java]
cam.getLocation().addLocal(cam.getDirection().mult(tpf));
[/java]

At least I’m fairly sure the last one works. /shrug There should be no magic behind setLocation as apposed to getLocation.alterThisShit()

i thought whenever you modify transforms you had to use the setXXX methods so itd internally flag update the transforms?

@icamefromspace said: i thought whenever you modify transforms you had to use the setXXX methods so itd internally flag update the transforms?

This is more than likely the case… I was guessing at all of this >.<

Yes, do set(get()) if you want to change the vector without having your own temp var. The flags need to be updated.