summaryrefslogtreecommitdiff
path: root/examples/resources/lib/quaternion.lua
blob: e95102e0f23a58e6d53b1f293120de78df9aac07 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
-- For luaJit compatibility.
if table.unpack == nil then
	table.unpack = unpack
end

local Vector3 = require( "vector3" )
local Matrix = require( "matrix" )

local Quaternion = {}
local metatable = {
	__index = Quaternion,
	__tostring = function( q )
		return "{"..tostring( q.x )..", "..tostring( q.y )..", "..tostring( q.z )..", "..tostring( q.w ).."}"
	end,
	__add = function( q1, q2 )
		return Quaternion:newT( RL.QuaternionAdd( q1, q2 ) )
	end,
	__sub = function( q1, q2 )
		return Quaternion:newT( RL.QuaternionSubtract( q1, q2 ) )
	end,
	__mul = function( q1, q2 )
		return Quaternion:newT( RL.QuaternionMultiply( q1, q2 ) )
	end,
	__div = function( q1, q2 )
		return Quaternion:newT( RL.QuaternionDivide( q1, q2 ) )
	end,
	__unm = function( q )
		return Quaternion:newT( RL.QuaternionInvert( q ) )
	end,
	__len = function()
		return 4
	end,
	__eq = function( q1, q2 )
		return RL.QuaternionEquals( q1, q2 ) == 1
	end,
	__concat = function( a, b )
		return tostring( a )..tostring( b )
	end,
}

function Quaternion:new( x, y, z, w )
	local object = setmetatable( {}, metatable )

	object.x = x or 0
	object.y = y or 0
	object.z = z or 0
	object.w = w or 1

	return object
end

function Quaternion:newT( t )
	local object = setmetatable( {}, metatable )

	object.x, object.y, object.z, object.w = table.unpack( t )

	return object
end

function Quaternion:set( x, y, z, w )
	self.x = x or 0
	self.y = y or 0
	self.z = z or 0
	self.w = w or 1
end

function Quaternion:setT( t )
	self.x, self.y, self.z, self.w = table.unpack( t )
end

function Quaternion:arr()
	return { self.x, self.y, self.z, self.w }
end

function Quaternion:unpack()
	return self.x, self.y, self.z, self.w
end

function Quaternion:clone()
	return Quaternion:new( self.x, self.y, self.z, self.w )
end

function Quaternion:addValue( value )
	return Quaternion:newT( RL.QuaternionAddValue( self, value ) )
end

function Quaternion:subValue( value )
	return Quaternion:newT( RL.QuaternionSubtractValue( self, value ) )
end

function Quaternion:identity()
	return Quaternion:newT( RL.QuaternionIdentity() )
end

function Quaternion:length()
	return RL.QuaternionLength( self )
end

function Quaternion:normalize()
	return Quaternion:newT( RL.QuaternionNormalize( self ) )
end

function Quaternion:invert()
	return Quaternion:newT( RL.QuaternionInvert( self ) )
end

function Quaternion:scale( scalar )
	return Quaternion:newT( RL.QuaternionScale( self, scalar ) )
end

function Quaternion:lerp( q2, value )
	return Quaternion:newT( RL.QuaternionLerp( self, q2, value ) )
end

function Quaternion:nLerp( q2, value )
	return Quaternion:newT( RL.QuaternionNLerp( self, q2, value ) )
end

function Quaternion:sLerp( q2, value )
	return Quaternion:newT( RL.QuaternionSLerp( self, q2, value ) )
end

function Quaternion:fromVector3ToVector3( from, to )
	return Vector3:newT( RL.QuaternionFromVector3ToVector3( from, to ) )
end

function Quaternion:fromMatrix( mat )
	return Quaternion:newT( RL.QuaternionFromMatrix( mat ) )
end

function Quaternion:toMatrix()
	return Matrix:newT( RL.QuaternionToMatrix( self ) )
end

function Quaternion:fromAxisAngle( axis, angle )
	return Quaternion:newT( RL.QuaternionFromAxisAngle( axis, angle ) )
end

function Quaternion:toAxisAngle()
	local axis, angle = Quaternion:newT( RL.QuaternionToAxisAngle( self ) )
	return Vector3:new( axis ), angle
end

function Quaternion:fromEuler( pitch, yaw, roll )
	return Quaternion:newT( RL.QuaternionFromEuler( pitch, yaw, roll ) )
end

function Quaternion:toEuler()
	return Vector3:newT( RL.QuaternionToEuler( self ) )
end

function Quaternion:transform( mat )
	return Quaternion:newT( RL.QuaternionTransform( self, mat ) )
end

local TEMP_COUNT = 100
local tempPool = {}
local curTemp = 1

for _ = 1, TEMP_COUNT do
	table.insert( tempPool, Quaternion:new( 0, 0, 0, 1 ) )
end

-- Uses pre generated objects to avoid "slow" table generation.
function Quaternion:temp( x, y, z, w )
	local object = tempPool[ curTemp ]
	curTemp = curTemp + 1

	if TEMP_COUNT < curTemp then
		curTemp = 1
	end

	object.x = x or 0
	object.y = y or 0
	object.z = z or 0
	object.w = w or 1

	return object
end

-- Uses pre generated objects to avoid "slow" table generation.
function Quaternion:tempT( t )
	local object = tempPool[ curTemp ]
	curTemp = curTemp + 1

	if TEMP_COUNT < curTemp then
		curTemp = 1
	end

	object.x, object.y, object.z, object.w = table.unpack( t )

	return object
end

function Quaternion:getTempId()
	return curTemp
end

return Quaternion