Advertisement

# Untitled

a guest
Jun 24th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. -- pos - observer position
2. -- vec - observer vector
3. -- target - target position
4.
5. function angle (pos, vec, target)
6.
7.     -- Imagine that the observer is wearing spherical helmet of raduis 1.
8.     -- He looks on the target, and his line of sight crosses the helmet.
9.     -- We'll calculate this crossing point by normalizing his looking
10.     -- vector.
11.
12.     -- Normalization means making length of vector equal to 1, but
13.     -- still pointing to the same direction.
14.
15.     local vec_look = vector.normalize(vec)
16.
17.     -- We also calculate another vector, that is the difference
18.     -- between observer and target (= position relative to target).
19.
20.     local vec_diff = vector.subtract(target, pos)
21.
22.     -- Well, this requires normalization too.
23.
24.     vec_diff = vector.normalize(vec_diff)
25.
26.     -- Now both are starting from center and pointing to some point
27.     -- of surface of helmet. We will calculate the distance of
28.
29.     -- Calculate the length between two vectors.
30.
31.     local chord = vector.length(vector.subtract(vec_look, vec_diff))
32.
33.     -- What we have calculated is a chord on some circle of sphere.
34.     -- Having this we can calculate the angle.
35.     -- https://en.wikipedia.org/wiki/Chord_%28geometry%29
36.
37.     -- c = 2*r*sin(phi / 2)
38.
39.     -- because r = 1, it degenerates to:
40.
41.     -- c = 2 * sin(phi / 2)
42.
43.     -- c/2 = sin(phi / 2)
44.
45.     -- asin(c/2) = phi / 2
46.
47.     -- 2 * asin(c/2) = phi
48.
49.     local angle = 2 * math.asin(chord / 2)
50.
51.     -- note the result is in radians. Convert it to degrees.
52.
53.     return angle * 180 / math.pi
54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement