-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoords2Rotation.lua
More file actions
139 lines (132 loc) · 4.9 KB
/
Coords2Rotation.lua
File metadata and controls
139 lines (132 loc) · 4.9 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
--[[
Coords2Rotation.lua
Coordinates To Rotation
version: 16.12.17
Copyright (C) 2016 Jeroen P. Broks
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
]]
--[[
Coords2Rotation.lua
Coordinates to rotation degrees
version: 16.09.19
Copyright (C) 2016 Jeroen P. Broks
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
]]
----------------------------------------------------------
-- Coords to Rotation --
-- This is a routine originally written in BlitzMax --
-- For the TARI engine, which is now cancelled --
-- Left to die, to never live again! (LAURA II is --
-- MUCH better. That's why). However this routine --
-- inside it was already planned (and cancelled) for --
-- BOTH Dyrt and Star Story, and NOW I shall use it --
----------------------------------------------------------
--[[ Original BlitzMax Code
Function GameFindRot(RPX#,RPY#,RCX#,RCY#,W)
Local Overstaande#
Local Aanliggende#
Local Tanges#
Select W
Case 1 'North-East
overstaande = RPY-RCY
aanliggende = RCX-RPX
Tanges = Overstaande/Aanliggende
SetRotation 90-ATan(Tanges)
Case 2 'North-West
Overstaande = RPY-RCY
aanliggende = RpX-RCX
Tanges = overstaande/aanliggende
SetRotation (-90)+ATan(Tanges)
Case 3 'South-East
Overstaande = RCY-RPY
Aanliggende = RCX-RPX
Tanges = Overstaande/Aanliggende
'DrawText Int(ATan(Tanges)),100,100
SetRotation 90+ATan(Tanges)
Case 4 'North-West
Overstaande = RpY-RcY
aanliggende = RpX-RCX
Tanges = overstaande/aanliggende
SetRotation (-90)+ATan(Tanges)
Default
SetRotation Rand(0,360)
End Select
End Function
]]
function GetAngle(A,B) -- A routine Jani sent me.
return math.deg(math.atan2(B.y-A.y,B.x-A.x))
end
-- Returns in radians
function CoordsToAngle(RPX,RPY,RCX,RCY)
--[[ on rem to try Jani's method
local Overstaande
local Aanliggende
local Tanges
local ATan = math.atan -- Arc Tangent (Yeah, I'm lazy)
local angle,altangle
if RPX==RCX and RPY>RCY then -- North
angle = 0
elseif RPX==RCX and RPY<RCY then -- South
angle = 180
elseif RPX>RCX and RPY==RCY then -- West
angle = -90
elseif RPX<RCX and RPY==RCY then -- East
angle = 90
elseif RPY>RCY and RPX<RCX then -- North-East
Overstaande = RPY-RCY
Aanliggende = RCX-RPX
Tanges = Overstaande/Aanliggende
angle = 90-ATan(Tanges)
altangle = 45
elseif RPY>RCY and RCX<RPX then -- North-West
Overstaande = RPY-RCY
Aanliggende = RPX-RCX
Tanges = Overstaande/Aanliggende
angle = (-90)+ATan(Tanges)
altangle = -45
elseif RPY<RCY and RCX>RPX then -- South-East
Overstaande = RCY-RPY
Aanliggende = RCX-RPX
Tanges = Overstaande/Aanliggende
angle = 90+ATan(Tanges)
altangle = 135
elseif RPY<RCY and RCX<RPX then -- South-West
Overstaande = RPY-RCY
Aanliggende = RPX-RCX
Tanges = Overstaande/Aanliggende
angle = (-90)+ATan(Tanges)
altangle = -135
else
angle = rand(0,360)
end
altangle = altangle or angle
return math.deg(angle),altangle
-- ]]
local a = {x= RPX , y = RPY}
local b = {x= RCX , y = RCY}
return GetAngle(a,b) + 90
end