Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Getting angle between two vector3's? [closed]

Asked by 10 years ago

I need to get the angle between two vector3 points, how would I go about doing it?

Locked by theCJarmy7, Avigant, Programical, Thundermaker300, User#19524, User#1007, and Zafirua

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

We can use the fact that the dot product of two vectors has two equivalent definitions.

The first definition is the sum of the products of the components, and ROBLOX provides this for you: If a and b are vectors, then the dot product of a and b is a:Dot(b).

The second definition is the product of the vectors' magnitudes and the cosine of the angle between them. So we have some simple algebra here:

-- Let a and b be vectors and d be the dot-product of them.
-- t is the angle for which we are solving, and |u| is taken to mean the
-- magnitude of u (in ROBLOX, u.magnitude)

-- |a| * |b| * cos(?) = d
-- cos(t) = d / (|a| * |b|)
-- t = arccos(d / (|a| * |b|))

-- To clean this math up further, we can make a and b be
-- unit vectors, so that their magnitudes are one and don't matter.
-- cos(t) = d
-- t = arccos(d)

-- So here it is in Lua.


-- a and b are the unit vectors
local theta = math.acos( a.unit:Dot(b.unit) );
-- theta is now the angle between a and b, measured in radians.

Ad
Log in to vote
0
Answered by
Maxomega3 106
10 years ago

BlueTaslem really knows what he's doing. I don't understand dot points, however. Doesn't it take 3 points/ 2 intersecting lines to make an angle?

0
You can interpret vectors are lines coming out of the origin. So you have three points: the origin, and the endpoint of each vector-arrow. BlueTaslem 18071 — 5y