Camera Movements

I implemented hotkeys to control my camera like A,S,D,W, etc



I need to implement a button that triggers the camera moving AROUND a certain point like the following:



http://i.imgur.com/S7zBh.png





I want the camera to rotate around a given point (or axis) in a circular motion. How do I go to implement that?





thanks!!!

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

OK so I’ve done the math for dummies and got a better understand of quaternions and how things work. However, I still don’t know whats the approach to take on rotating around the Z-Axis from the camera.


  • how can I get the center of the scene I am looking at as I want to rotate around the center of the map or whatever scene I am seeing.


  • the harder questions is how do I do it?





    could it be something as simple as adding a quat or vect to the cam like:

    [java]cam.setLocation(cam.getLocation().add(x, y, z));[/java]

    [java]cam.setRotation(quat);[/java]



    thanks

No, you simply rotate a vector with length=radius to get the locations and then you use lookAt to determine the direction.

not sure I got you, can you provide an example?



[java] Quaternion qRot = cam.getRotation();

Quaternion rot = new Quaternion();

rot.fromAngleNormalAxis(5, Vector3f.UNIT_Z); // something like this??? that flips it over to the other side of the map

cam.setRotation(rot);[/java]





thanks

sigh… a Vector…

[java]Vector3f camPos = new Vector3f(0,0,1);

Quaternion camRot = new Quaternion();

Quaternion tempQuat = new Quaternion();

simpleupdate(tpf){

//rotate the vector

tempQuat.fromAngles(0,tpf,0).multLocal(camPos);

camRot.lookAt(center.subtract(camPos));

cam.setLocation(camPos);

cam.setRotation(camRot);

}[/java]

it doesn’t work :S I am sorry I’m still new at this.



is center a Vector3f(0,0,0) ??



I used camRot.lookAt(center.subtract(camPos)); but need to add another Vector3f to it as lookAt takes two vectors anyways my code is:



[java]

else if (name.equals(“moveAroundPositive”) && keyPressed) {



System.out.println(“Rotating camera around Z axis Positively”);



// position must be on a circle around the Z axis

// Vector with radius length = 10

Vector3f camPos = new Vector3f(0,0,10);

Vector3f center = new Vector3f (0,0,0);



Quaternion camRot = new Quaternion();

Quaternion tempQuat = new Quaternion();



// create a quaternion based on the angles passed

// and multiplied by camPos

tempQuat.fromAngles(0,tpf,0).multLocal(camPos);

//tempQuat.fromAngles(0,rota,0).multLocal(camPos);



camRot.lookAt(center.subtract(camPos), camPos);

//camRot.lookAt(new Vector3f(0,0,0), new Vector3f(0,1,0));

//rota +=1;



cam.setLocation(camPos);

cam.setRotation(camRot);

}





[/java]



again, i’m sorry but although I have some expertise in java, I never played with graphics before, thanks for your help

Like said in the javadoc, its the up-vector, so Vector3f.UNIT_Y or new Vector(0,1,0); Also how am I supposed to know where the center in your image is?

well thats what I used (I had it there uncommented) and my center is (0,0,0) however it does go BELOW the object as if I am rotating around the x or Y axes.

…the x-axis is that whats “right” with the default cam view, y is up and z is towards you…

as if I am rotating around the x or Y axes


in case you misunderstood me.


In other words, say the center is (0,0,0) and my object is at the center also, I want to rotate my camera AROUND it.

[java]
if (name.equals("moveAroundPositive") && keyPressed) {
Vector3f center = new Vector3f (0,0,0);
Quaternion camRot = new Quaternion();
Quaternion tempQuat = new Quaternion();
tempQuat.fromAngles(0,rota,0).multLocal(camPos); //rota = 1 initially
camRot.lookAt(new Vector3f(0,0,0), new Vector3f(Vector3f.UNIT_Y));
rota +=1;
cam.setLocation(camPos);
cam.setRotation(camRot);
}
[/java]

This won't work as intended - it will rotate around the object but not even consistently.

why you do that “rota” shit? you don’t even consider the framerate… you cannot just add 1 every frame and then expect to run smoothly. Also the camPos vector obviously has to be kept so you can keep rotating it, so a field in the class. Also to make the center work properly you should add it to the

[java]if (name.equals(“moveAroundPositive”) && keyPressed) {

Vector3f center = new Vector3f (0,0,0);

Quaternion camRot = new Quaternion();

Quaternion tempQuat = new Quaternion();

tempQuat.fromAngles(0,tpf,0).multLocal(camPos);

camRot.lookAt(center.subtract(camPos), new Vector3f(Vector3f.UNIT_Y));

cam.setLocation(center.add(camPos));

cam.setRotation(camRot);

}[/java]

I think I only misunderstood you when I thought you said you the read math for dummies doc. Try to see what I am doing here, I keep rotating a vector bit by bit each frame so that I get a circle in the end. Each frame the tip of the vector is where the cam should be.

Lol, do you still have the flyCam enabled? xD

Also, its still “center.subtract(camPos)” for the lookAt (corrected above)… Why do you keep changing my code? You are the one making the mistakes…

flycam is off





and I tried camRot.lookAt(center.subtract(camPos), new Vector3f(Vector3f.UNIT_Y));



what will happen is that it will place the camera in a 2D view with a camPos distance :S

There, a whole application where the cam rotates around the box, go figure it out. -.-

[java]public class TestCamRot extends SimpleApplication {



Vector3f camPos = new Vector3f(0, 0, 10);

Vector3f center = new Vector3f(0, 0, 0);

Quaternion camRot = new Quaternion();

Quaternion tempQuat = new Quaternion();



public static void main(String[] args) {

TestCamRot app = new TestCamRot();

app.start();

}



@Override

public void simpleInitApp() {

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

Geometry geom = new Geometry(“Box”, b);

Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mat.setTexture(“ColorMap”, assetManager.loadTexture(“Interface/Logo/Monkey.jpg”));

geom.setMaterial(mat);

rootNode.attachChild(geom);

}



@Override

public void simpleUpdate(float tpf) {

tempQuat.fromAngles(0, tpf, 0).multLocal(camPos);

camRot.lookAt(center.subtract(camPos), Vector3f.UNIT_Y);

cam.setLocation(center.add(camPos));

cam.setRotation(camRot);

}

}[/java]

Try to understand what happens:

Am I allowed to say “This is some funny $h1t” ? Because I am getting a kick out of this.

1 Like

I havent really read the above codes and things

personally to solve this problem i would use jmonkey’s node



1-the middle point is a node (M-middle) - simpleinit

2-then create another node at camera(CN-cam node) and attach it to M- simpleinit

3-place and attach the camera to CN

4- rotate M and make cam.lookat(M.getlocation) - simpleupdate or in actionlistener



that way i wont have to use quaternion

:roll: If it rotates around the wrong axis for you then change the axis. Really dude, try to understand it. Meditate about it, sit down and just read it again and again and again, also math for dummies. If you can’t understand the code this whole stuff is futile anyway, just stop it then, its too much for you. @xieu’s solution is the one for babies really, wasting resources for a node just to compute a circular motion.

@nightwolf911 said:
Vector3f camPos = new Vector3f(0, 10, 0); it's standstill - this is really confusing

...also how exactly is it confusing that rotating a vector that points in y direction around the y-axis doesn't change the vector? :roll:
(also again: Y = up)