Search code examples
actionscript-3papervision3d

Papervision3D: Making a camera look at Plane


I have a wall of Planes at different angles and positions. I'd like to get the camera to rotate and look straight at the focused plane. I have created a dummy Plane (lookAtMe) that tweens to the Plane I click on as follows:

    private function planeClicked(e:InteractiveScene3DEvent):void 
    {
        lookAtTarget.copyTransform(this);

        var time:Number = 1;
        var tweenObject:Object = {};
        tweenObject.x = lookAtTarget.x;
        tweenObject.y = lookAtTarget.y;
        tweenObject.z = lookAtTarget.z;
        tweenObject.rotationX = lookAtTarget.rotationX;
        tweenObject.rotationY = lookAtTarget.rotationY;
        tweenObject.rotationZ = lookAtTarget.rotationZ;
        tweenObject.onUpdate = onSeatTween;
        tweenObject.ease = Cubic.easeInOut;
        TweenMax.to(lookAtMe, time, tweenObject);
    }

    private function onSeatTween():void
    {
        camera.lookAt(lookAtMe);
    }

The camera centers on the looAtMe Plane but doesn't rotate so that the selected Plane is straight on.

Please help! Thanks.


Solution

  • Did you know that your plane has a normal? It's true, it determines which side the plane is facing. A normal is a vector with a length of 1.

    You can calculate the normal of a plane if you know three three-dimensional points on it. For instance: the center, the center at the top and the center at the right. Let's call the center C, the center at the top M1 and the center at the right M2.

    Here's how to calculate the normal:

    Cross(C - M1, C - M2)
    

    (If you don't know the cross product, please look it up)

    This will give you the vector as defined by the arrow:

    alt text

    Alright, so how can we use that information? Knowing the normal of the plane and its center, we can say the following:

    camera_position = plane_position + (plane_normal * distance)

    This will put the camera at the correct position, some distance away from the plane.

    camera_direction = plane_normal * -1

    This makes the camera look at the plane.

    However, I don't think ActionScript 3 defines the camera look at function with a vector, I think it uses an object. You could, for example, place a small invisible object at the center of the plane and point the camera at that. That should give you the result you want.