Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement addition for Vector3D metatypes #206

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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