summaryrefslogtreecommitdiff
path: root/examples/resources/lib/quaternion.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/resources/lib/quaternion.lua')
-rw-r--r--examples/resources/lib/quaternion.lua135
1 files changed, 75 insertions, 60 deletions
diff --git a/examples/resources/lib/quaternion.lua b/examples/resources/lib/quaternion.lua
index dfb07f4..e95102e 100644
--- a/examples/resources/lib/quaternion.lua
+++ b/examples/resources/lib/quaternion.lua
@@ -13,19 +13,19 @@ local metatable = {
return "{"..tostring( q.x )..", "..tostring( q.y )..", "..tostring( q.z )..", "..tostring( q.w ).."}"
end,
__add = function( q1, q2 )
- return Quaternion:new( RL.QuaternionAdd( q1, q2 ) )
+ return Quaternion:newT( RL.QuaternionAdd( q1, q2 ) )
end,
__sub = function( q1, q2 )
- return Quaternion:new( RL.QuaternionSubtract( q1, q2 ) )
+ return Quaternion:newT( RL.QuaternionSubtract( q1, q2 ) )
end,
__mul = function( q1, q2 )
- return Quaternion:new( RL.QuaternionMultiply( q1, q2 ) )
+ return Quaternion:newT( RL.QuaternionMultiply( q1, q2 ) )
end,
__div = function( q1, q2 )
- return Quaternion:new( RL.QuaternionDivide( q1, q2 ) )
+ return Quaternion:newT( RL.QuaternionDivide( q1, q2 ) )
end,
__unm = function( q )
- return Quaternion:new( RL.QuaternionInvert( q ) )
+ return Quaternion:newT( RL.QuaternionInvert( q ) )
end,
__len = function()
return 4
@@ -39,33 +39,33 @@ local metatable = {
}
function Quaternion:new( x, y, z, w )
- if type( x ) == "table" then
- x, y, z, w = table.unpack( x )
- elseif type( x ) == "nil" then
- x, y, z, w = 0, 0, 0, 1 -- QuaternionIdentity.
- end
+ 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 = x
- object.y = y
- object.z = z
- object.w = w
+ object.x, object.y, object.z, object.w = table.unpack( t )
return object
end
function Quaternion:set( x, y, z, w )
- if type( x ) == "table" then
- x, y, z, w = table.unpack( x )
- elseif type( x ) == "nil" then
- x, y, z, w = 0, 0, 0, 1 -- QuaternionIdentity.
- end
+ self.x = x or 0
+ self.y = y or 0
+ self.z = z or 0
+ self.w = w or 1
+end
- self.x = x
- self.y = y
- self.z = z
- self.w = w
+function Quaternion:setT( t )
+ self.x, self.y, self.z, self.w = table.unpack( t )
end
function Quaternion:arr()
@@ -81,15 +81,15 @@ function Quaternion:clone()
end
function Quaternion:addValue( value )
- return Quaternion:new( RL.QuaternionAddValue( self, value ) )
+ return Quaternion:newT( RL.QuaternionAddValue( self, value ) )
end
function Quaternion:subValue( value )
- return Quaternion:new( RL.QuaternionSubtractValue( self, value ) )
+ return Quaternion:newT( RL.QuaternionSubtractValue( self, value ) )
end
function Quaternion:identity()
- return Quaternion:new( RL.QuaternionIdentity() )
+ return Quaternion:newT( RL.QuaternionIdentity() )
end
function Quaternion:length()
@@ -97,88 +97,103 @@ function Quaternion:length()
end
function Quaternion:normalize()
- return Quaternion:new( RL.QuaternionNormalize( self ) )
+ return Quaternion:newT( RL.QuaternionNormalize( self ) )
end
function Quaternion:invert()
- return Quaternion:new( RL.QuaternionInvert( self ) )
+ return Quaternion:newT( RL.QuaternionInvert( self ) )
end
function Quaternion:scale( scalar )
- return Quaternion:new( RL.QuaternionScale( self, scalar ) )
+ return Quaternion:newT( RL.QuaternionScale( self, scalar ) )
end
function Quaternion:lerp( q2, value )
- return Quaternion:new( RL.QuaternionLerp( self, q2, value ) )
+ return Quaternion:newT( RL.QuaternionLerp( self, q2, value ) )
end
function Quaternion:nLerp( q2, value )
- return Quaternion:new( RL.QuaternionNLerp( self, q2, value ) )
+ return Quaternion:newT( RL.QuaternionNLerp( self, q2, value ) )
end
function Quaternion:sLerp( q2, value )
- return Quaternion:new( RL.QuaternionSLerp( self, q2, value ) )
+ return Quaternion:newT( RL.QuaternionSLerp( self, q2, value ) )
end
function Quaternion:fromVector3ToVector3( from, to )
- return Vector3:new( RL.QuaternionFromVector3ToVector3( from, to ) )
+ return Vector3:newT( RL.QuaternionFromVector3ToVector3( from, to ) )
end
function Quaternion:fromMatrix( mat )
- return Quaternion:new( RL.QuaternionFromMatrix( mat ) )
+ return Quaternion:newT( RL.QuaternionFromMatrix( mat ) )
end
function Quaternion:toMatrix()
- return Matrix:new( RL.QuaternionToMatrix( self ) )
+ return Matrix:newT( RL.QuaternionToMatrix( self ) )
end
function Quaternion:fromAxisAngle( axis, angle )
- return Quaternion:new( RL.QuaternionFromAxisAngle( axis, angle ) )
+ return Quaternion:newT( RL.QuaternionFromAxisAngle( axis, angle ) )
end
function Quaternion:toAxisAngle()
- local axis, angle = Quaternion:new( RL.QuaternionToAxisAngle( self ) )
+ local axis, angle = Quaternion:newT( RL.QuaternionToAxisAngle( self ) )
return Vector3:new( axis ), angle
end
function Quaternion:fromEuler( pitch, yaw, roll )
- return Quaternion:new( RL.QuaternionFromEuler( pitch, yaw, roll ) )
+ return Quaternion:newT( RL.QuaternionFromEuler( pitch, yaw, roll ) )
end
function Quaternion:toEuler()
- return Vector3:new( RL.QuaternionToEuler( self ) )
+ return Vector3:newT( RL.QuaternionToEuler( self ) )
end
function Quaternion:transform( mat )
- return Quaternion:new( RL.QuaternionTransform( self, mat ) )
+ return Quaternion:newT( RL.QuaternionTransform( self, mat ) )
end
-function Quaternion:addEq( q2 )
- self.x = self.x + q2.x
- self.y = self.y + q2.y
- self.z = self.z + q2.z
- self.w = self.w + q2.w
+local TEMP_COUNT = 100
+local tempPool = {}
+local curTemp = 1
+
+for _ = 1, TEMP_COUNT do
+ table.insert( tempPool, Quaternion:new( 0, 0, 0, 1 ) )
end
-function Quaternion:subEq( q2 )
- self.x = self.x - q2.x
- self.y = self.y - q2.y
- self.z = self.z - q2.z
- self.w = self.w - q2.w
+-- 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
-function Quaternion:mulEq( q2 )
- self.x = self.x * q2.x
- self.y = self.y * q2.y
- self.z = self.z * q2.z
- self.w = self.w * q2.w
+-- 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:divEq( q2 )
- self.x = self.x / q2.x
- self.y = self.y / q2.y
- self.z = self.z / q2.z
- self.w = self.w / q2.w
+function Quaternion:getTempId()
+ return curTemp
end
return Quaternion