Panning the camera

homsi:

i just made “camera.getLocation().add(your vector)” not only “your vector” because if you move camera for example to x: 10000, then it will not always look at left. without “camera.getLocation().add” it would be affected by camera position.



lookAtVector is a vector(in world space) where camera need to look, so “your vector” is vector where camera should look at.



so for example do (x, 0, -1) and see if it work. you project XY so it should be (x, 0, -number) so i was wrong before about (x, -number, 0).



where x will be modified from (again for example) -100 to 100 by a keyboard analog keys (for example) “a” and “d”.



is that enough? or you can just give me a testcase and i will make it for you.

I looked at the video. I’m not sure what you do to the camera when you tilt it but it actually looks like it is rotating.

Think of it like this. Originally you rotate the camera around the UP-vector which happens to be aligned with the world Y-axis. Then you tilt it forwards (I guess) and then you rotate it again around the world Y-axis (like if it was hanging tilted from a string). But you want the “head shake” effect of rotating the camera around its own UP-axis?

I’m not sure what projectXY does but I suspect that is the problem.



LookAt will work of course but I think it might be simpler to just set the rotation matrix like nehon said. You just need to make sure it is the right axis :slight_smile:

you should use camera.setRotation(camera.getRotation().lookAt(x)); camera.lookAt (and spatial.lookAt) works different than quaternion.lookAt

@oxplay2 i’m so sorry but I still don’t get it. so If i was to write a method

[java]

private void panCamera(float value)

{

Vector3f lookAtVector = camera.getLocation().add((???); <<<<<< what do i put here?

camera.lookAt(lookAtVector, Vector3f.UNIT_Y);

}

[/java]



where A gives it a +1 and D gives a -1 (or whatever value that makes it smooth i.e. tpf)

The direction and up vectors. Check the “lookAt” section of https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

@homsi:

<<<<<< what do i put here?

you put there vector where you want camera to look, without offset of camera position.

you can just give me testcase with your projection and i will make it for you. ok? i think it will be faster to make and understand for you.
it would be good as zip or something as project. like in testcase definition.

but if you want, try to read lookAt doc like normen said, it will help you understand.

@oxplay2 no need for testcase if you run JMP and run the basic game example (blue box) notice how the panning and tilting works. If you go BELOW the blue box and try to pan left or right it rotates weirdly instead of left and right.



@nehon I am trying to use the open source method found in FlyByCamera.java but it doesn’t seem to work well in all the camera angles.

sigh This is all basic jme math and rotation, just because my post contained a link to a tutorial doesn’t mean you should ignore it. Your fixed UNIT_Y vector will mess up anything with a direction vector thats not y=0. If you want to tell me that you “read all the tutorials and they don’t help” then forget about game coding, this is the easy part.

if its THAT simple why isn’t it done correctly in FlyByCamera??? like I said give it a shot yourself. Use the baseGame example and go underneath the bluebox or on top of it and rotate the cam left / right, it won’t work.

it can be, but i wanted to give you simpler idea…



ahh too much for today



i tried to avoid this, but if you will not fix it yourself, then i will just make it for you. (tomorrow)



and wtf why normen again have -1… he said it right.

@homsi said:
if its THAT simple why isn't it done correctly in FlyByCamera??? like I said give it a shot yourself. Use the baseGame example and go underneath the bluebox or on top of it and rotate the cam left / right, it won't work.

"Correctly" is relative. The FlyByCamera assumes Y-UP just like your normal FPS control scheme in 99,9% of all games does. I don't say its simple, I even said its maybe too hard for you but its about the simplest thing in game development. This is why I spent hours putting together that tutorial.

homsi:



here, example for you.

use “a” and “d” keys to panning left and right.



[java]package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.light.AmbientLight;

import com.jme3.light.DirectionalLight;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;



public class TestPanning extends SimpleApplication {



private float x = 0f;

private float panningSeed = 4f;

private AmbientLight al = new AmbientLight();

private DirectionalLight dl = new DirectionalLight();



public static void main(String[] args) {

TestPanning app = new TestPanning();

app.start();

}

public AnalogListener analogListener = new AnalogListener() {



public void onAnalog(String name, float value, float tpf) {

if (name.equals(“keya”)) {

if (x > -10f) {

x -= tpf * panningSeed;

}

} else if (name.equals(“keyd”)) {

if (x < 10f) {

x += tpf * panningSeed;

}

}

System.out.println(name + " x: " + x);

}

};



public void projectXY(Vector3f vect) {

vect.z = 0;

}



public void initKeys() {

inputManager.addMapping(“keya”, new KeyTrigger(KeyInput.KEY_A));

inputManager.addMapping(“keyd”, new KeyTrigger(KeyInput.KEY_D));

inputManager.addListener(analogListener, “keya”, “keyd”);

}



private Geometry addBox(float xPos, float yPos) {

Box box = new Box(1, 1, 1);

Geometry geom2 = new Geometry(“boxx” + xPos + “y” + yPos, box);

Material mat_tl = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

mat_tl.setColor(“Diffuse”, new ColorRGBA(0.5f, 0.5f, 0.5f, 1f));

mat_tl.setColor(“Ambient”, new ColorRGBA(0.3f, 0.3f, 0.3f, 1f));

mat_tl.setColor(“Specular”, new ColorRGBA(0.5f, 0.5f, 0.5f, 1f));

mat_tl.setBoolean(“UseMaterialColors”, true);

geom2.setMaterial(mat_tl);

geom2.setLocalTranslation(xPos, yPos, 10000);

rootNode.attachChild(geom2);

return geom2;

}



@Override

public void simpleInitApp() {

dl.setDirection(new Vector3f(-1, -1, -1));

rootNode.addLight(al);

rootNode.addLight(dl);

flyCam.setEnabled(false);

initKeys();

Geometry g0 = addBox(0, 0);

Geometry g1 = addBox(2, 2);

Geometry g2 = addBox(4, 1);

Geometry g3 = addBox(6, 0);



projectXY(g0.getLocalTranslation());

projectXY(g1.getLocalTranslation());

projectXY(g2.getLocalTranslation());

projectXY(g3.getLocalTranslation());

}



@Override

public void simpleUpdate(float tpf) {

Vector3f lookAtVector = new Vector3f(x, 0, -1);

cam.lookAt(lookAtVector, Vector3f.UNIT_Y);

}

}

[/java]





just an easy example. don’t know what is hard for you.

1 Like

If you use a mouse, so I would suggest to use mouse button and screenCoordinates of a mouse. Much better effect.



Here is my little hint:



[java]

// center of the screen

float width = camera.getWidth() * 0.5f;

float height = camera.getHeight() * 0.5f;

Vector2f ceneterScr = new Vector2f(width, height);



endPosMouse = inputMan.getCursorPosition();

mouseDist = ceneterScr.distance(endPosMouse);



camMoveX = camera.getLeft();

if (endPosMouse.x > ceneterScr.x) camMoveX.negateLocal();

camMoveX.normalizeLocal();



camMoveY = camera.getUp();

if (endPosMouse.y < ceneterScr.y) camMoveY.negateLocal();

camMoveY.normalizeLocal();

[/java]

1 Like

i proposed before, but homsi want a keyboard control.

as he wish…

I already implemented the mouse, I just realized that all my world coordinates were flipped (y and z) which made all my calculations wrong. it’s fixed now and yes its much simpler than I though. Thanks guys and especially @oxplay2 you rock!

1 Like