Camera looking direction vector

106 views
Skip to first unread message

Flyon

unread,
Aug 24, 2010, 4:57:26 AM8/24/10
to Away3D.dev
After moving the camera around, I want to calculate the coordinates of
a point right in front of the camera, at say distance 100..
Is there any way to get the vector of my 'looking direction' ?

Rich Elmes

unread,
Aug 24, 2010, 11:44:55 AM8/24/10
to Away3D.dev
maybe there is something that i didn't find in away3d, but i achieved
this with this method :

create a dummy object (ObjectContainer3D)
copy position and rotation values from the camera to the dummy object
call moveForward(100) on the dummy object
now the dummy object is your point in front of the camera at distance
100 and difference in position between camera & dummy object is a
vector of your "looking direction"

David Lenaerts

unread,
Aug 24, 2010, 11:55:33 AM8/24/10
to away3...@googlegroups.com
There's an easier way if you directly use the camera's transformation matrix. The 3rd column of a transformation matrix is always the local Z-axis in the transformation's destination space. So the camera's transform object has its local z-axis (which is the "forward" vector) in scene coordinates. Getting that is pretty straightforward:

var camDirVector : Number3D = new Number3D(camera.transform.sxz, camera.transform.syz, camera.transform.szz);

Since your camera isn't (or shouldn't be) scaled, it's also unit-length, so a position 100 in front of the cam is simply as follows:

pos.x = cam.x + camDirVector.x*100;
pos.y = cam.y + camDirVector.y*100;
pos.z = cam.z + camDirVector.z*100;

Alternatively, you can also do something like this:

pos = new Number3D();
tgt = new Number3D(0, 0, 100);
pos.transform(tgt, camera.transform);

This literally means: I want an object 100 in front of the cam, but expressed in the camera's parent (ie: scene) coordinates. Both do the same thing, but the former method should be a bit faster, if it matters.

Hope that helps!
David
--
David Lenaerts
Flash platform developer
http://www.derschmale.com

John Brookes

unread,
Aug 24, 2010, 12:58:59 PM8/24/10
to away3...@googlegroups.com
Which brings me to the question isn't forward wrong in MatrixAway3D class?

As in, you should be able to do
pos.x = camera.position.x + camera.transform.forward.x*10;
pos.y = camera.position.y + camera.transform.forward.y*10;
pos.z = camera.position.z + camera.transform.forward.z*10;

but that doesn't work.

Change MatrixAway3D forward to

public function get forward():Number3D
{
    _forward.x = sxz //szx;
    _forward.y = syz  //szy;
    _forward.z = szz;
    return _forward;
}

Above works.

Flyon

unread,
Aug 25, 2010, 12:35:52 AM8/25/10
to Away3D.dev
Excelent!

The first solution worked perfect for me, and the second made me
better understand the transform function :)

tnx!
Reply all
Reply to author
Forward
0 new messages