ChaseCamera - Show followed object at bottom of screen

I’m toying around with the ChaseCam. I have a model that moved forward along the Z-axis. The player can move the ship left and right (over the X-axis). That all works :slight_smile:



However, the ChaseCam puts the followed node, in this case the ship, in the center of the screen.







The question is, how can I get the ship to be shown at the X (see image)?



Here’s my code for setting up the camera:



[java]

private void setupCamera() {

// Disable the default flyCam.

flyCam.setEnabled(false);



// Create a ChaseCamera for the player node.

playerCam = new ChaseCamera(cam, player, inputManager);

playerCam.setTrailingEnabled(true);

// playerCam.setSmoothMotion(true);



// Keep your distance

playerCam.setDefaultDistance(20f);

playerCam.setMinDistance(10f);

playerCam.setMaxDistance(25f);



// Rotate the camera to be behind the player

// TODO: Use -FastMath.HALF_PI or use player for reference?

playerCam.setDefaultHorizontalRotation(-FastMath.HALF_PI);

playerCam.setDefaultVerticalRotation(3 * FastMath.ONE_THIRD);

}[/java]

You could attach the ship to a node, set the local translation to 0,-2,0 or something and then make the chasecam actually chase the node.

normen said:
You could attach the ship to a node, set the local translation to 0,-2,0 or something and then make the chasecam actually chase the node.


I was just thinking to do it the other way around: attach a special node to the ship and focus the cam on that:

[java] private void setupCamera() {
// Disable the default flyCam.
flyCam.setEnabled(false);

Node playerCamPoint = new Node();
playerCamPoint.setLocalTranslation(0, 40f, 0);
player.attachChild(playerCamPoint);

// Create a ChaseCamera for the player node.
playerCam = new ChaseCamera(cam, playerCamPoint, inputManager);
playerCam.setTrailingEnabled(true);
// playerCam.setSmoothMotion(true);

// Keep your distance
playerCam.setDefaultDistance(20f);
playerCam.setMinDistance(10f);
playerCam.setMaxDistance(25f);

// Rotate the camera to be behind the player
// TODO: Use -FastMath.HALF_PI or use player for reference?
playerCam.setDefaultHorizontalRotation(-FastMath.HALF_PI);
playerCam.setDefaultVerticalRotation(3 * FastMath.ONE_THIRD);
}[/java]

Works like a charm :) Except that the cam controls now are a bit dodgy (e.g. they rotate around the fake node, not the ship), but I still have to disable rotation anyway, so it's not a problem for now. Thanks!

The camera always looks at the target so that’s why the target is always in the center of the screen.

Maybe i should implement some kind of offset for the look at, usually you want the cam to look at the player’s head, it could be a common need…

Anyway, I’m glad that you’ve found your way around this.



Looking at your code, playerCam.setTrailingEnabled(true); only works when smooth motion is enabled.



If you want to disable the rotation don’t pass the inputManager to the ChaseCamera constructor.

This way inputs won’t be registered.

However if you want to keep the zoom in/out behavior just do this :



playerCam = new ChaseCamera(cam, playerCamPoint);

inputManager.addMapping(“ZoomIn”, new MouseAxisTrigger(2, true));

inputManager.addMapping(“ZoomOut”, new MouseAxisTrigger(2, false));

inputManager.addListener(playerCam , “ZoomIn”,“ZoomOut”);

I think that would be a much used feature indeed.



For my purpose I’ve found an easier way:



Edit: This follows the ship at set distance down the Z-axis, but allowing the ship to move across the screen from left to right (X-axis). No zoom, no rotation. Just the way I like it :slight_smile:



[java] private void setupCamera() {

// Disable the default flyCam.

flyCam.setEnabled(false);



// Set location behind the player ship and look at it

cam.setLocation(new Vector3f(0, 25f, -25f));

cam.lookAt(player.getWorldTranslation().clone().add(0, 10f, 0),

Vector3f.UNIT_Y);

}[/java]



With this simpleUpdate method:



[java] public void simpleUpdate(float tpf) {

// Move ‘forward’ down the Z-axis

float x = ((left) ? 22 * tpf : 0) +

((right) ? -22 * tpf : 0);



Vector3f playerDirection = new Vector3f(

x, 0, 2 * tpf);

Vector3f camDirection = new Vector3f(

0, 0, 2 * tpf);



player.move(playerDirection);

cam.setLocation(cam.getLocation().clone().add(camDirection));

}[/java]



I half expected there to be a cam.move(), but there isn’t.