Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: ninogenio on May 06, 2007
-
just wondering whats the best way of pointing an entity at another i have an entity thats -20 units on the zpos now i want it to point towards an entity thats infront of it at whatever x,y,z pos.
but im not sure how to go about this and was wondering if someone could give me an idea.
cheers.
-
Create a vector which is the line between you and the object and normalize it, so if the object is at ox,oy,oz and the camera is at cx,cy,cz, that gives you
Z = normalize ( (ox-cx) , (oy-cy), (oz-cz) )
Then pick an up-vector, let's make that Y' = (0,1,0).
Then take the cross product of Y' and Z and normalize it
X = normalize( Y' x Z )
Then take the cross product again to get
Y = Z x X
So now you have 3 vectors, X,Y,Z which form the matrix which will orient the camera to point at the object.
The 3x3 matrix is
X.x X.y X.z
Y.x Y.y Y.z
Z.x Z.y Z.z
The 4x4 matrix is
X.x X.y X.z 0
Y.x Y.y Y.z 0
Z.x Z.y Z.z 0
0 0 0 1
Jim
-
very very handy maths that jim!! thanks very much k+
-
Nice simple demo Jim , :)
one thing that i want to tell , the cross product of any 2 vectors preduce a vector that Normal with the plane which include the 2 original vectors.
-
does this look ok jim
Dim As Double Zx , Zy , Zz , Xx , Xy , Xz , Yx , Yy , Yz , Length , y
Zx = Sphere->Position->x - Camera->Position->x
Zy = Sphere->Position->y - Camera->Position->y
Zz = Sphere->Position->z - Camera->Position->z
Length = sqr( Zx * Zx + Zy * Zy + Zz * Zz )
Zx /= Length
Zy /= Length
Zz /= Length
Yx = 0
Yy = 1
Yz = 0
Xx = Zy*Yz - Zz*Yy
Xy = Zz*Yx - Zx*Yz
Xz = Zx*Yy - Zy*Yx
Length = sqr( Xx * Xx + Xy * Xy + Xz * Xz )
Xx /= Length
Xy /= Length
Xz /= Length
Yx = Zy*Xz - Zz*Xy
Yy = Zy*Xz - Zz*Xy
Yz = Zy*Xz - Zz*Xy
Camera->Matrix( m00 ) = Xx : Camera->Matrix( m01 ) = Xy : Camera->Matrix( m02 ) = Xz : Camera->Matrix( m03 ) = 0
Camera->Matrix( m10 ) = Yx : Camera->Matrix( m11 ) = Yy : Camera->Matrix( m12 ) = Yz : Camera->Matrix( m13 ) = 0
Camera->Matrix( m20 ) = Zx : Camera->Matrix( m21 ) = Zy : Camera->Matrix( m22 ) = Zz : Camera->Matrix( m23 ) = 0
Camera->Matrix( m30 ) = 0 : Camera->Matrix( m31 ) = 0 : Camera->Matrix( m32 ) = 0 : Camera->Matrix( m33 ) = 1
and the orientated camera matrix does it go in place of the rotation matrix and get muld by the translation?
-
You have the 2nd cross product wrong. Looks like a cut & paste error. You should really write this stuff in terms of vector functions to avoid mistakes like that.
This replaces the camera matrix.
Two other things to note.
1. When you get it working
Yx = 0
Yy = 1
Yz = 0
Xx = Zy*Yz - Zz*Yy
Xy = Zz*Yx - Zx*Yz
Xz = Zx*Yy - Zy*Yx
can be simplified quite a lot.
2. The code won't work well when you are looking straight up at an object, this is why in some 3d games when you look straight up the camera goes spastic. It can be fixed but you need to know what you're doing.
Jim
-
what have i done wrong on the second cross product jim? i thought there might have been some errors as i did this from memory just to see if i could.
and yeah ill use some vector functions once i get it going.
also you said a bit of it could be made easier how so mate.
cheers btw.
-
woops i see what ive done my bad :)
so is this effectivly a look at function andalso can i aplly this to a model instead of the camera?
<edit i can see where i can simplify the up vector also because x and z are zero i can get rid of alot of the calcs>
-
what have i done wrong on the second cross product jim?
It's not a cross product! All 3 lines are the same!
also you said a bit of it could be made easier how so mate? Yx = 0
Yy = 1
Yz = 0
Subsitute those in to this
Xx = Zy*Yz - Zz*Yy
Xy = Zz*Yx - Zx*Yz
Xz = Zx*Yy - Zy*Yx
and you get
Xx = Zy * 0 - Zz * 1
Yy = Zz * 0 - Zx * 0
Yz = Zx * 1 - Zy * 0
I'm pretty sure you can take it from there ;)
Jim
-
You can use it to point one model at another model, yep.
Jim
-
K+ jim this really is awsome it really simplifys the camera stage getting rid of the rotation and translation.
obviously it wont be ideal for every type of situation but for me it suits for a lot of stuff.
-
right heres an exe and the source i think its working as it should but if i lift my camera up or down the cube isnt centred anymore is this correct?
-
I guess not, the cube should stay in the centre, since you're always looking at it - it'll appear to rotate though. I suspect you've not got the translation part right - you still need to apply the camera matrix to the object's offset from the camera, the code I've posted only deals with the orientation of the camera, not the translations. I'll try to have a look at it this weekend for you.
Jim
-
right ill have a little play around and see if i can get at whats went wrong cheers jim.
< edit just fixed it! i had commented out the translation part of my camera in my engine lol >
here is the working update incase anyone else need something like this.
-
That looks very much like it's working! Nice demo!
Jim
-
cheers jim, your maths works exactly like you said it would so im happy.
:cheers: