Skip to content

Commit

Permalink
Merge pull request #206 from RagnarokResearchLab/vector-math-addition
Browse files Browse the repository at this point in the history
Implement addition for Vector3D metatypes
  • Loading branch information
rdw-software authored Nov 23, 2023
2 parents 9a88258 + 3933d19 commit a2b6cf8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Core/VectorMath/Vector3D.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions Tests/VectorMath/Vector3D.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a2b6cf8

Please sign in to comment.