PDA

View Full Version : Normilze a Vector



Daikrys
02-07-2005, 01:49 PM
hi,

how can i rotate a object with a normalized vector?
have anyone a function?

thanks

Paulius
02-07-2005, 04:59 PM
You can?¢_~t describe a rotation with a vector. Normalized just means that it?¢_Ts length is equal to one. Could you explain in more detail what you want to accomplish and with what.

Sascha Willems
02-07-2005, 05:07 PM
Paulius, rotations can be stored in a vector. That's done when using euler angles which represent rotations in such a way that they can be stored in a vector data structure.

See http://skal.planet-d.net/demo/matrixfaq.htm#Q36 to see how you get a rotation matrix out of such euler angles.

Paulius
02-07-2005, 07:37 PM
From a data structure point of view you could also call a quaternion a vector, but not in a mathematical sense.

Daikrys
03-07-2005, 09:17 AM
thanks :)

but it isnt what i mean ^^"

i was thinking i can get the direction over a normalized vector

i will rotate an object in the direction of an other object
how to do?

Daikrys
03-07-2005, 02:56 PM
i mean the following:

i have this situation:
http://daikrys.funpic.de/01.JPG

and i will have this situation:
http://daikrys.funpic.de/02.JPG

that means i must rotate the first object with the position of the other

thanks
daiki

Paulius
03-07-2005, 09:34 PM
So what you want to do is transform an object so one of it?¢_Ts own axis is normalized(TargetPosition - ObjectPosition), we still need more information if you want to do it in 2D or 3D and using which API.

Sly
03-07-2005, 11:04 PM
Ahh, so you want an object to turn to be facing another object? Here is some C code I found for creating a look-at matrix. But better still, it actually has comments to say what it is doing! :)



//-----------------------------------------------------------------------------
// Name: buildLookAtMatrix()
// Desc: Creates a Left-Handed view matrix
//
// Building a View Matrix:
//
// 1. Subtract the camera's position from the position to look at, to
// create a view vector called 'l'.
// 2. Take the cross-product of the new view vector and the y axis of
// world space to get a right vector called 'r'.
// 3. The cross-product of vectors 'l' and 'r' will give you a up vector
// called 'u'.
// 4. Compute translation factors by taking the negative of the
// dot-product between the camera's position, called 'c', and our new
// orientation or basis vectors called u, r, and l.
//
// Here's what the final matrix should look like:
//
// | rx ux lx 0 |
// | ry uy ly 0 |
// | rz uz lz 0 |
// | -(r.c) -(u.c) -(l.c) 1 |
//
// Where r = Right vector
// u = Up vector
// l = Look vector
// c = Camera's world space position
// . = Dot-product operation
//-----------------------------------------------------------------------------
void buildLookAtMatrix( D3DXMATRIX *pOut,
const D3DXVECTOR3 &pEye,
const D3DXVECTOR3 &pAt,
const D3DXVECTOR3 &pUp )
{
D3DXVECTOR3 vRight; // New x basis
D3DXVECTOR3 vUp; // New y basis
D3DXVECTOR3 vView; // New z basis

// Subtract the camera's position from the viewer's position to
// create a direction or view vector for the camera. We'll call
// this vector, vView.
D3DXVec3Subtract(&vView, &pAt, &pEye);

// Normalize our new z basis vector.
D3DXVec3Normalize(&vView, &vView);

// Take the cross-product of the new direction vector and the
// x axis of the world space to get a right vector.
// We'll call this vector vRight.
D3DXVec3Cross(&vRight, &pUp, &vView);
D3DXVec3Normalize(&vRight, &vRight);

// The cross-product of the direction vector (vView) and the
// right vector (vRight) will give us our new up vector, which
// we'll call vUp.
D3DXVec3Cross(&vUp, &vView, &vRight);

// We now build the matrix. The first three columns contain the
// basis vectors used to rotate the view to point at the look-at
// point. The fourth row contains the translation values.
// Rotations are still about the eye point.
//
// m11 m12 m13 m14
// m21 m22 m23 m24
// m31 m32 m33 m34
// m41 m42 m43 m44

pOut->_11 = vRight.x;
pOut->_21 = vRight.y;
pOut->_31 = vRight.z;
pOut->_41 = -D3DXVec3Dot( &vRight, &pEye );

pOut->_12 = vUp.x;
pOut->_22 = vUp.y;
pOut->_32 = vUp.z;
pOut->_42 = -D3DXVec3Dot( &vUp, &pEye );

pOut->_13 = vView.x;
pOut->_23 = vView.y;
pOut->_33 = vView.z;
pOut->_43 = -D3DXVec3Dot( &vView, &pEye );

pOut->_14 = 0.0f;
pOut->_24 = 0.0f;
pOut->_34 = 0.0f;
pOut->_44 = 1.0f;
}

Daikrys
13-07-2005, 01:26 PM
hi and thanks

sorry for waiting :wink:

ok i dont understanding anything from the C source :toothy:

i will create an Aim-Target like Sly said
it can be used in strategie and rpg

i use opengl and found this from delphigl.com:



//Cross-product
function VectorCross(v1, v2 : TVec): TVec;

var
Temp : TVec;

begin
Temp.x := V1.y*V2.z - V1.z*V2.y;
Temp.y := V1.z*V2.x - V1.x*V2.z;
Temp.z := V1.x*V2.y - V1.y*V2.x;
Result := Temp;
end;


//Angle in Float(Extended)
function VectorAngle(v1, v2 : TVec) : float;

var
v, w : float;

begin
v := v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
w := (sqr(v1.x)+sqr(v1.y)+sqr(v1.z)) * (sqr(v2.x)+sqr(v2.y)+sqr(v2.z));
result := ArcCos(v / sqrt(w));
end;


or smaller:



ArcCos(v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);


this can be used by 2 normalized vectors

ok now how to get an correct angle for glrotate?

thanks
Daiki

JSoftware
13-07-2005, 03:51 PM
Edit: sort of repost of the thread..

Sly
13-07-2005, 10:37 PM
From what I can tell, that code gives you the angle between the two vectors. But this angle could be a rotation around any axis, that axis being the cross-product of the two vectors. That axis would most likely not be co-planar with any of the x, y or z axes, making the angle not much use when it comes to making an object look at something.

I'll try to port the C code I gave before to Pascal.

procedure BuildLookAtMatrix(var pOut: D3DXMATRIX;
const pEye: D3DXVECTOR3;
const pAt: D3DXVECTOR3;
const pUp: D3DXVECTOR3)
var
vRight: D3DXVECTOR3; // New x basis
vUp: D3DXVECTOR3; // New y basis
vView: D3DXVECTOR3; // New z basis
begin
// Subtract the camera's position from the viewer's position to
// create a direction or view vector for the camera. We'll call
// this vector, vView.
D3DXVec3Subtract(vView, pAt, pEye);

// Normalize our new z basis vector.
D3DXVec3Normalize(vView, vView);

// Take the cross-product of the new direction vector and the
// x axis of the world space to get a right vector.
// We'll call this vector vRight.
D3DXVec3Cross(vRight, pUp, vView);
D3DXVec3Normalize(vRight, vRight);

// The cross-product of the direction vector (vView) and the
// right vector (vRight) will give us our new up vector, which
// we'll call vUp.
D3DXVec3Cross(vUp, vView, vRight);

// We now build the matrix. The first three columns contain the
// basis vectors used to rotate the view to point at the look-at
// point. The fourth row contains the translation values.
// Rotations are still about the eye point.
//
// m11 m12 m13 m14
// m21 m22 m23 m24
// m31 m32 m33 m34
// m41 m42 m43 m44

pOut._11 = vRight.x;
pOut._21 = vRight.y;
pOut._31 = vRight.z;
pOut._41 = -D3DXVec3Dot(vRight, pEye);

pOut._12 = vUp.x;
pOut._22 = vUp.y;
pOut._32 = vUp.z;
pOut._42 = -D3DXVec3Dot(vUp, pEye);

pOut._13 = vView.x;
pOut._23 = vView.y;
pOut._33 = vView.z;
pOut._43 = -D3DXVec3Dot(vView, pEye);

pOut._14 = 0.0;
pOut._24 = 0.0;
pOut._34 = 0.0;
pOut._44 = 1.0;
end;

Daikrys
14-07-2005, 11:52 AM
thanks sly, but i dont know how to rotate then i dont know anything
in D3D ^^"

ok i upload a programm:
http://daikrys.funpic.de/rotations.rar

i work with it some days
maybe someone find something wrong(i hope)

Daikrys
14-07-2005, 07:57 PM
thanks for helping

some guys of www.indiegamer.com helped me also

i think thats it

alpha := ArcTan2(v1.y-v2.y , v1.x-v2.x);
alpha := -(-90+(-1* alpha * 57.29578));

this will work(i hope)

to open my links please put it manually in the addressfield

much thanks
Daiki