From 3933d193ac71168fb5f359606109431bd82ebcab Mon Sep 17 00:00:00 2001 From: RDW Date: Thu, 23 Nov 2023 18:28:11 +0100 Subject: [PATCH] Core: Implement addition for Vector3D metatypes Trivial but missing primitive that will come in handy shortly. --- Core/VectorMath/Vector3D.lua | 8 ++++++++ Tests/VectorMath/Vector3D.spec.lua | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Core/VectorMath/Vector3D.lua b/Core/VectorMath/Vector3D.lua index e027400d..46ec2fb4 100644 --- a/Core/VectorMath/Vector3D.lua +++ b/Core/VectorMath/Vector3D.lua @@ -32,6 +32,14 @@ Vector3D.__call = function(_, x, y, z) return vector end +function Vector3D:Add(anotherVector) + local result = ffi_new("Vector3D") + result.x = self.x + anotherVector.x + result.y = self.y + anotherVector.y + result.z = self.z + anotherVector.z + return result +end + function Vector3D:Subtract(anotherVector) local result = ffi_new("Vector3D") result.x = self.x - anotherVector.x diff --git a/Tests/VectorMath/Vector3D.spec.lua b/Tests/VectorMath/Vector3D.spec.lua index 95a29fce..1c403753 100644 --- a/Tests/VectorMath/Vector3D.spec.lua +++ b/Tests/VectorMath/Vector3D.spec.lua @@ -21,6 +21,26 @@ describe("Vector3D", function() end ) + describe("Add", function() + it("should return a new 3D vector that contains the sum of the two given inputs", function() + local initialPosition = Vector3D() + local finalPosition = Vector3D() + + initialPosition.x = 2 + initialPosition.y = 4 + initialPosition.z = 6 + + finalPosition.x = 1 + finalPosition.y = 2 + finalPosition.z = 3 + + local resultant = finalPosition:Add(initialPosition) + assertEquals(resultant.x, 3) + assertEquals(resultant.y, 6) + assertEquals(resultant.z, 9) + end) + end) + describe("Subtract", function() it("should return a new 3D vector that contains the displacement between two given inputs", function() local initialPosition = Vector3D()