-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrig.elm
111 lines (73 loc) · 2.68 KB
/
Trig.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
module Trig
exposing
( xDelta
, yDelta
, dDelta
, targetDirection
, targetDistance
, turnDirection
, facing
, normalize
)
-- Module for calculating various useful values for the game.
-- Bear with me on the embarrassing math, and please submit
-- pull requests if you see something stupid. It's been a long
-- time since I took trig.
-- Note that many functions take a time delta ratio for modifying
-- the effect based on the time diff since the last frame. So, for
-- example, if we're at 60 fps then it might be * 1.0, or 30 fps it
-- might be * 2.0. This is to keep a consistent feel despite different
-- framerates.
-- Given a speed and direction, calculates the x delta
-- which is basically how much the x increases or decreases
-- Note that the direction is rotated 90 degrees.
xDelta : Float -> Float -> Float -> Float
xDelta diff s d =
s * (cos <| degrees <| d - 90) * diff
-- Given a speed and direction, calculates the y delta
-- which is basically how much the y increases or decreases
-- Note that the direction is rotated 90 degrees.
yDelta : Float -> Float -> Float -> Float
yDelta diff s d =
s * (sin <| degrees <| d - 90) * diff
-- Given two directions, calculates how many degrees apart
-- they are, negative if left or positive if right.
dDelta : Float -> Float -> Float
dDelta td d =
toFloat ((floor ((d - td) + 180)) % 360 - 180)
-- Given a target x and target y, calculates the direction
-- toward that target from the origin. Returns degrees.
targetDirection : Float -> Float -> Float
targetDirection x y =
normalize 0 360 (-1 * ((atan2 x y) * (180 / pi)))
-- Given a target direction and my direction, return the best
-- direction to turn to point toward the target eventually.
-- Will return 0 if we're within 5 degrees.
turnDirection : Float -> Float -> Float
turnDirection td d =
if facing td d 5 then
0
else if ((floor (td - d + 360)) % 360) > 180 then
-1
else
1
-- Given a target direction and my direction and a tolerance,
-- are we pointed the right direction yet?
facing : Float -> Float -> Float -> Bool
facing td d tolerance =
(abs (dDelta td d)) <= tolerance
-- Given a target x and y, calculates the distance toward
-- that target from the origin.
targetDistance : Float -> Float -> Float
targetDistance x y =
sqrt ((x * x) + (y * y))
-- Given a low and high bounds and a current value, will
-- "wrap" to stay within the bounds.
normalize : Float -> Float -> Float -> Float
normalize low high curr =
if curr < low then
curr + (high - low)
else if curr >= high then
curr - (high - low)
else
curr