Find the angle between 2 vectors in 3d

Asked

Viewed 1,144 times

3

I want the X Y Z angles between the two vectors

To make a line between the two of them.

math.atan2(t2.y - t1.y, t2.x - t1.x) * 180 / math.pi)-90

This is the account I used to find Z, but the others I can’t do, X and Y.

The size is

C.Size=Vector3.new(0.2,math.sqrt((t2.z-t1.z)^2+(t2.y-t1.y)^2+(t2.z-t1.z)^2),0.2) 

Example:

I did it on the pad because I have to go fast.

I need to figure out the angle to rotate the orange line between the two points, connecting them by the same.

cubo

  • There is only one angle between two vectors, no?

  • You want the angles of the projection of the plane formed by the two vectors in the normal planes?

  • Mr Felix . I want the projectile angles . pq is in 3d

  • The projections of each vector or the projection of the plane formed by the two vectors?

  • I’m sorry I can’t answer because I’m kind of a beginner in trigonometry and.e

1 answer

5

The size (or magnitude) of a vector is calculated by the Pythagoras formula, i.e., the square root of the sum of the squares of each component of the vector.

The scalar product of two vectors is calculated as the sum of the products of each component, and what it represents is the product of the length of the two vectors with the cosine of the angle formed between them. Therefore, when dividing the scalar product by the size of the vectors, the angle cosine is reached.

Having the angle cosine, math.acos(x) will give you the angle in radians. This function acos means arc-cosine, or reverse cosine.

Having the angle in radians, math.deg(x) will give you the angle in degrees. This function precisely serves to convert radians in degrees.

So this must be what you want:

function vetor3D(x, y, z)
    local v = {}
    v.x = x
    v.y = y
    v.z = z
    return v
end

function magnitude(v)
    return math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
end

function produto_escalar(a, b)
    return a.x * b.x + a.y * b.y + a.z * b.z
end

function angulo(a, b)
    ta = magnitude(a)
    tb = magnitude(b)
    if ta == 0.0 or tb == 0.0 then
        return 0.0
    end
    return math.deg(math.acos(produto_escalar(a, b) / ta / tb))
end

# Teste
a = vetor3D(1, 0, 0)
b = vetor3D(0, 1, 0)
print(angulo(a, b))

c = vetor3D(1, 1, 1)
print(angulo(a, c))

See here working on ideone.

Note that at the end this angulo(a, c) will give the angle between one of the edges of the cube and the diagonal. The program displays as output for this 54.735610317245 (in degrees).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.