# Community Forum

## 3d vector direction

Forum Overview >> Farming Simulator 2009

 Category Farming Simulator 2009 Created 23.08.2010 12:08

Andreas Großschedl (Unknown) 23.08.2010 12:21
Hi!

I need some help with the 3d vector:

i tried to calculate the angle of a vector but the result is: -1.#IND ?

I tried this: take 2 points in the 3d space, then sub one from the other, then take the unit vector (1,0,0)... for each axis and make the dotproduct. x and y were correct but with the z axis i have the problem, that the dotproduct is > 1 or < -1 and then i cannot calculate the acos!

formula:

rz=z/math.sqrt(math.pow(x , 2) + math.pow(y , 2) + math.pow(z-1, 2));
x=-0.16616821289063
y=0.86651611328125
z=22.773193359375

rz=1.0450703452324

maybe someone can help me?

thx Andreas

Stefan Geiger - GIANTS Software 26.08.2010 08:56
You need to normalize your vector first.

local len = Utils.vector3Length(x,y,z);
if len ~= 0 then
x = x / len;
y = y / len;
z = z / len;

local dotx = x; -- dot product with 1,0,0
local doty = y; -- dot product with 0,1,0
local dotz = z; -- dot product with 0,0,1
end;

Btw: math.pow is quite slow. To calculate the square you might want to use x*x. It's even shorter :)

Note: Log in to post. Create a new account here.