3

I am trying to implement smooth 3D camera in my game. So, I have a few reserved positions and directions and I want to change view of my camera by applying them to my camera. Now I am doing it like below:

//Reserved positions and directions

//View #1
Vector3 pos1 = new Vector3(0, 5, -10);
Vector3 dir1 = new Vector3(0, 0, 10);

//View #2
Vector3 pos2 = new Vector3(5, 2, 5);
Vector3 dir2 = new Vector3(-2, 2, 7);

//Applying view #2
camera.position.set(pos2);
camera.lookAt(dir2);

It works, but I need to do it smoothly. I've seen a similar question for 2D camera, but it doesn't appropriate for me, because orthographic camera doesn't need direction and Z-axis.

UPDATE:

I tried to use method which is described for 2D camera. So, positioning works as needed, but I can't say the same about direction. Behaviour of direction is wrong. It rotates camera on Z-axis. Seems to me I have to update Up vector too.

float lerp = 0.01f;
Vector3 position = camera.position;
position.x += (pos2.x - position.x) * lerp;
position.y += (pos2.y - position.y) * lerp;
position.z += (pos2.z - position.z) * lerp;

Vector3 direction = camera.direction;
direction.x += (dir2.x - direction.x) * lerp;
direction.y += (dir2.y - direction.y) * lerp;
direction.z += (dir2.z - direction.z) * lerp;

//CODE FROM LIBGDX'S CAMERA
//LookAt function
/*
tmpVec.set(x, y, z).sub(position).nor();
    if (!tmpVec.isZero()) {
        float dot = tmpVec.dot(up); // up and direction must ALWAYS be orthonormal vectors
        if (Math.abs(dot - 1) < 0.000000001f) {
            // Collinear
            up.set(direction).scl(-1);
        } else if (Math.abs(dot + 1) < 0.000000001f) {
            // Collinear opposite
            up.set(direction);
        }
        direction.set(tmpVec);
        normalizeUp();
    }
*/

What is the correct way to set direction?

Community
  • 1
  • 1
Nolesh
  • 6,848
  • 12
  • 75
  • 112
  • maybe this can help you http://php-c.org/fletcher/2015/04/05/java-libgdx-camera-smooth-translation/ and this https://github.com/libgdx/libgdx/wiki/Interpolation – Angel Angel Apr 29 '15 at 11:02
  • @AngelAngel, these links are helpful for orthographic camera only. – Nolesh Apr 29 '15 at 12:10
  • @Nolesh why should this work for Orthographic camera only? At least the `Interpolation` should be usable for `Vector3` to, so it can be used in your case. – Robert P Apr 29 '15 at 14:02

1 Answers1

-1

I would use the Univesal Tween Engine, which is a library for interpolating pretty much anything.

You'd have to write a Vector3Accessor class (which would be very simple), but then you'd have all sorts of controls and options for smooth movement. It has a very rich API and is used a lot for exactly this sort of thing.

I'd strongly recommend you put a little time into investigating it as an option -It'll give you a better result, and once you know it you'll find all sorts of other cool uses for it in your app.

Phil Anderson
  • 3,146
  • 13
  • 24