Camera Movements

since scene graph for dummies doesnt work I have to show him that scene graph for baby

I wonder if that is still too much for him

Okay I figured it out, so the cam rotates around the center just fine but I donā€™t want camPos to be fixed at Vector3f camPos = new Vector3f(0, 10, 0);



I want to have a cam at a certain location and then when I click a button, be able to rotate it like weā€™re doing here. By multiplyingthe cam location vector by a certain quaternion - using the location of the cam instead of camPos



Being more clear, I donā€™t want to go back to a 0,0,10 position everytime I want to rotate the cam, but I actually want it to rotate from its current position AROUND the center.

so ā€œcenterā€ is the location of the object and ā€œcamPosā€ is currentCamPos-center :roll:

bla bla bla, whats all this fuss for only 3 lines of code.

why do you write so many lines of code for something as simple :



[java]

@Override

public void update()

{

Vector3f cameraPosition = new Vector3f(0f, 0f, -cameraOffset);

Scene.getScene().getCam().setLocation(node.localToWorld(cameraPosition, cameraPosition));

Scene.getScene().getCam().lookAt(node.getWorldTranslation(), Vector3f.UNIT_Y);

}

[/java]



[java]

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

{

if (name.equals("RotateLeft")) rotateNormal(node, 0, FastMath.HALF_PI, value);

else if (name.equals("RotateRight")) rotateNormal(node, 0, -FastMath.HALF_PI, value);

else if (name.equals("RotateUp")) rotateNormal(node,-FastMath.HALF_PI, 0, value);

else if (name.equals("RotateDown")) rotateNormal(node,FastMath.HALF_PI, 0, value);

}



public static rotateNormal(Node node, float rotateX, float rotateY, float value)

{

rotate(node, rotateX * value, rotateY * value);

}



/**

  • We force character to not be able to exceed rotation limits. Because it
  • will turn the world upside down.

    /

    public static final float MIN_ANGLE_X = -89 * FastMath.DEG_TO_RAD;

    public static final float MAX_ANGLE_X = 89 * FastMath.DEG_TO_RAD;



    /
    * rotates a node around x,y. Note: Z rotation=0, MIN_ANGLE_X <= rotation.x <= MAX_ANGLE_X
  • @param rotateX : rotation in radians. if input is in degrees multiply by * FastMath.DEG_TO_RAD

    /

    public static void rotate(Node node, float rotateX, float rotateY)

    {

    float[] rotation = node.getLocalRotation().toAngles(null);

    setRotation(node, rotation[0] + rotateX, rotation[1] + rotateY);

    }



    /
    * sets rotation of a node to (x,y,0). Note: MIN_ANGLE_X <= rotation.x <= MAX_ANGLE_X
  • @param rotateX : rotation in radians. if input is in degrees multiply by * FastMath.DEG_TO_RAD

    */

    public static void setRotation(Node node, float rotateX, float rotateY)

    {

    float rotX = rotateX;

    if (rotX < MIN_ANGLE_X) rotX = MIN_ANGLE_X;

    if (rotX > MAX_ANGLE_X) rotX = MAX_ANGLE_X;

    setRotation(node, new Vector3f(rotX, rotateY, 0));

    }

    [/java]

ok one more question. I know cam.lookAt() is where the camera will be looking. How can I get that point? I need to get the vector where the camera IS LOOKING and use it instead of the center.



thanks

cam.getDirection() gets the look direction.

But that doesnā€™t return the point it is looking at does it?

Iā€™d use a ray for that.

Hereā€™s an example of rays :slight_smile:

So if this code would rotate the camera around the origin, how would I rotate the camera around where it is currently looking?

[java]

if (name.equals("moveAroundPositive") && keyPressed) {



camPos = cam.getLocation();



//TODO: need to replace center with where the camera is looking

tempQuat.fromAngles(0, 0, tpf + 0.05f).multLocal(camPos);

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

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

cam.setRotation(camRot);

}[/java]



thanks

I am currently working on that myself, but I think Iā€™m gonna have a node with the camera and set the center to be the point of the ray, and the use the rotate function. Havenā€™t got there in reality yet though, just came up with it in my mind :stuck_out_tongue:

Just managed to get it to work with chaseCamera which already has all these features built in, think that is a smarter way of doing it instead of reinventing the wheel :slight_smile:

can you post some code?



I use chaseCamera in order to chase around my spatials. But I want to have a button that I press in order to rotate camera around the object.



let me know

thanks

Of course!

My project is fully open source and you can find it here!

The class you specifically want to look at for the camera movement is custom.camera.DungeonMasterCamera.java.

To implement it you only need these two lines

[java]flyCam.setEnabled(false);

DungeonMasterCamera dmCam = new DungeonMasterCamera(cam, inputManager, rootNode, rootNode);[/java]

Hope it helps :slight_smile:

Oh and all the settings in the initialization of DungeonMasterCamera is the settings for the camera, like for setting a button to rotate around the object:

[java]chaseCam.setToggleRotationTrigger(Triggers.toggleRotate);[/java]

where Triggers is a class with (for me) predefined triggers to use globally.

Triggers.toggleRotate is actually pointing to

[java]new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE)[/java]

which is the middle mouse button.



Also, worth mentioning is that I had to change in the ChaseCamera class for it to not rotate infinite when fully zoomed in and rotating the camera down (like it apparently is default, would be nice to know why if someone would be kind and tell me). You can find the CustomChaseCamera, which has this problem fixed in the same folder as the DungeonMasterCamera.

but that seems like a lot of work to create a custom chaseCam then your function and in order to do a simple thing.



I already have this working great, I just need to replace the center variable by wherever the camera is looking. Obviously, the cam.getDirection does not work :S



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



camPos = cam.getLocation();



//TODO: need to replace center with where the camera is looking

tempQuat.fromAngles(0, 0, tpf + 0.05f).multLocal(camPos);

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

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

cam.setRotation(camRot);

}[/java]