views:

614

answers:

3

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.

A: 

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.

knight666
A: 

I think you want to work backwards - first pick a point on a plane (X,Y,Z) (by averaging?) that you want to look at. Then pick a distance D away from that point that you wish to be.

Next take the equation of a plane http://mathworld.wolfram.com/Plane.html ax+by+cz=d (different d) and from there deduce a normal vector n = (a,b,c). Normalize it and multiply by D to obtain N = (A,B,C). Now there are only two points from which you could be looking from: (X,Y,Z) +/- (A,B,C).

So, now pick whether you want to look from behind or from the front - that will affect the choice of + vs -, I just do not know which is which.

Hamish Grubijan
A: 

The PV3D.org blog has a working example that should be exactly what you're looking for.

Scott Smith