-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metamethods and utility functions for Vectors and Quaternions to simplify and clean up the code involving operations with them. Co-authored-by: G C <[email protected]>
- Loading branch information
Showing
21 changed files
with
1,047 additions
and
722 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- (#6438) Fix `TerrainUtils.GetTerrainSlopeAngles` returning roll on the -Z axis instead of the +Z axis. Previously, this required the roll to be negated when used to orient objects such as units with the terrain. Now roll no longer needs to be negated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
- (#5061, #6438) Add metamethods and utility functions for Vectors and Quaternions to simplify and clean up the code involving operations with them. | ||
- This **removes** the file `/lua/shared/quaternions.lua`, which was added in #4768 (Mar 4, 2023), so mods that use that file will have to be updated. | ||
- The metamethods (defined globally in `/lua/system/utils.lua`) include: | ||
- Vector/Vector2 addition/subtraction/negation | ||
- Vector/Vector2 * Scalar multiplication | ||
- Quaternion/Vector * Vector/Quaternion multiplication | ||
- Vector * Vector multiplication (cross product) | ||
- Since these are metamethods, they work on all instances of Vector/Vector2/Quaternion, without having to import anything. | ||
- The utility functions (have to be imported from `/lua/utilities.lua`) include: | ||
- Faster Lua versions of VDist2, VDist2Sq, VDot | ||
- `QuatFromRotation`: Creates a quaternion from an orientation axis and rotation angle. | ||
- `QuatFromXZDirection`: Returns the orientation quaternion given an XZ direction | ||
- `TranslateInXZDirection`: Translates the XZ coordinates of a position by a length in a given quaternion orientation. | ||
- `RotateVectorByQuat`: Rotates a vector representing a 3D point by a quaternion rotation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
-- UpvaluedEngineVDist2OnLocals: 0.043102264404297 | ||
-- UpvaluedEngineVDist2SqOnLocals: 0.040153503417969 | ||
-- UpvaluedLuaVDist2OnLocals: 0.029483795166016 | ||
-- UpvaluedLuaVDist2SqOnLocals: 0.014518737792969 | ||
|
||
-- Conclusion: Lua is much faster, especially when skipping the square root (useful in distance comparisons). | ||
|
||
local outerLoop = 1000000 | ||
|
||
function UpvaluedEngineVDist2OnLocals() | ||
local p1 = Random()*1000 | ||
local p2 = Random()*1000 | ||
local p3 = Random()*1000 | ||
local p4 = Random()*1000 | ||
|
||
-- upvalue | ||
local VDist2 = VDist2 | ||
|
||
local start = GetSystemTimeSecondsOnlyForProfileUse() | ||
|
||
for i=1, outerLoop do | ||
local dist = VDist2(p1, p2, p3, p4) | ||
end | ||
|
||
local final = GetSystemTimeSecondsOnlyForProfileUse() | ||
|
||
return final - start | ||
end | ||
function UpvaluedEngineVDist2SqOnLocals() | ||
local p1 = Random()*1000 | ||
local p2 = Random()*1000 | ||
local p3 = Random()*1000 | ||
local p4 = Random()*1000 | ||
|
||
-- upvalue | ||
local VDist2Sq = VDist2Sq | ||
|
||
local start = GetSystemTimeSecondsOnlyForProfileUse() | ||
|
||
for i=1, outerLoop do | ||
local dist = VDist2Sq(p1, p2, p3, p4) | ||
end | ||
|
||
local final = GetSystemTimeSecondsOnlyForProfileUse() | ||
|
||
return final - start | ||
end | ||
function UpvaluedLuaVDist2OnLocals() | ||
local p1 = Random()*1000 | ||
local p2 = Random()*1000 | ||
local p3 = Random()*1000 | ||
local p4 = Random()*1000 | ||
|
||
-- upvalue | ||
local MathSqrt = math.sqrt | ||
|
||
local start = GetSystemTimeSecondsOnlyForProfileUse() | ||
|
||
for i=1, outerLoop do | ||
local d1 = p3 - p1 | ||
local d2 = p4 - p2 | ||
local dist = MathSqrt(d1 * d1 + d2 * d2) | ||
end | ||
|
||
local final = GetSystemTimeSecondsOnlyForProfileUse() | ||
|
||
return final - start | ||
end | ||
function UpvaluedLuaVDist2SqOnLocals() | ||
local p1 = Random()*1000 | ||
local p2 = Random()*1000 | ||
local p3 = Random()*1000 | ||
local p4 = Random()*1000 | ||
|
||
local start = GetSystemTimeSecondsOnlyForProfileUse() | ||
|
||
for i=1, outerLoop do | ||
local d1 = p3 - p1 | ||
local d2 = p4 - p2 | ||
local dist = d1 * d1 + d2 * d2 | ||
end | ||
|
||
local final = GetSystemTimeSecondsOnlyForProfileUse() | ||
|
||
return final - start | ||
end |
Oops, something went wrong.