Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: ninogenio on May 06, 2007

Title: pointing an one entity at another 3dquestion
Post 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.
Title: Re: pointing an one entity at another 3dquestion
Post by: Jim on May 06, 2007
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
Title: Re: pointing an one entity at another 3dquestion
Post by: ninogenio on May 07, 2007
very very handy maths that jim!! thanks very much k+
Title: Re: pointing an one entity at another 3dquestion
Post by: Emil_halim on May 07, 2007


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.
Title: Re: pointing an one entity at another 3dquestion
Post by: ninogenio on May 10, 2007
does this look ok jim
Code: [Select]
      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?
Title: Re: pointing an one entity at another 3dquestion
Post by: Jim on May 10, 2007
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
Code: [Select]
     
      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
Title: Re: pointing an one entity at another 3dquestion
Post by: ninogenio on May 10, 2007
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.
Title: Re: pointing an one entity at another 3dquestion
Post by: ninogenio on May 10, 2007
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>
Title: Re: pointing an one entity at another 3dquestion
Post by: Jim on May 10, 2007
Quote
what have i done wrong on the second cross product jim?
It's not a cross product!  All 3 lines are the same!

Quote
also you said a bit of it could be made easier how so mate?
Quote
     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
Code: [Select]
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
Title: Re: pointing an one entity at another 3dquestion
Post by: Jim on May 10, 2007
You can use it to point one model at another model, yep.

Jim
Title: Re: pointing an one entity at another 3dquestion
Post by: ninogenio on May 10, 2007
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.
Title: Re: pointing an one entity at another 3dquestion
Post by: ninogenio on May 11, 2007
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?
Title: Re: pointing an one entity at another 3dquestion
Post by: Jim on May 11, 2007
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
Title: Re: pointing an one entity at another 3dquestion
Post by: ninogenio on May 11, 2007
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.
Title: Re: pointing an one entity at another 3dquestion
Post by: Jim on May 11, 2007
That looks very much like it's working!  Nice demo!

Jim
Title: Re: pointing an one entity at another 3dquestion
Post by: ninogenio on May 12, 2007
cheers jim, your maths works exactly like you said it would so im happy.

 :cheers: