huongtt1986 / min3d

Automatically exported from code.google.com/p/min3d
0 stars 0 forks source link

Please, example camera follow?. #61

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago

How to create camera follow in min3d
example: camera 3rd person follow a box.

thank you.

Original issue reported on code.google.com by ggpe...@gmail.com on 23 Sep 2011 at 7:16

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
I answer myself 

I got it!!!

this is the code

public void updateScene() 
    {
        if (_dx != 0)
        {
            /**
             * When trackball moves horizontally... 
             * 
             * Rotate camera around center of the box in a circle.
             * Its position is 9.0f above the box, but its target() position 
             * remains at the scene origin, so the camera always looks towards the center. 
             */

            _rot += _dx * 40f;
            steering=_rot;
            scene.camera().position.y=2;
            scene.camera().position.x = cube.position().x + (float)Math.sin(cube.rotation().y)*cameraDistance;
            scene.camera().position.z = cube.position().z - (float)Math.cos(cube.rotation().y)*cameraDistance;

            _dx = 0;

        }

        if (_dy != 0)
        {
            // When trackball moves vertically, change camera's frustum "shortSideLength" property 
            // This effectively changes field of view. (??)

            float len = scene.camera().frustum.shortSideLength() + _dy;
            if (len < 0.1f) len = 0.1f;
            if (len > 50f) len = 5f;
            scene.camera().frustum.shortSideLength(1f);

            speed=_dy;

            cube.position().x += speed*Math.sin(cube.rotation().y);
            cube.position().z -= speed*Math.cos(cube.rotation().y);

            scene.camera().position.x = cube.position().x + (float)Math.sin(cube.rotation().y)*cameraDistance;
            scene.camera().position.z = cube.position().z - (float)Math.cos(cube.rotation().y)*cameraDistance;

            _dy = 0;
        }
    }

Original comment by ggpe...@gmail.com on 25 Sep 2011 at 1:13