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

fix: some more division by zero errors #57

Merged
merged 1 commit into from
Dec 23, 2024
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
4 changes: 2 additions & 2 deletions scripts/geometry/Vector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ end
-- return the scalar projection of v on self
---@return number
function Vector:scalarProjection(v)
return self:dot(v) / self:length()
return CpMathUtil.divide(self:dot(v), self:length())
end

---@return Vector
function Vector:projection(v)
return (v:dot(self) / self:dot(self)) * self
return CpMathUtil.divide(v:dot(self), self:dot(self)) * self
end

---@return Vector
Expand Down
4 changes: 2 additions & 2 deletions scripts/util/CpMathUtil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ function CpMathUtil.getIntersectionPoint(A1x, A1y, A2x, A2y, B1x, B1y, B2x, B2y)
s2_y = B2y - B1y

local s, t
s = (-s1_y * (A1x - B1x) + s1_x * (A1y - B1y)) / (-s2_x * s1_y + s1_x * s2_y)
t = ( s2_x * (A1y - B1y) - s2_y * (A1x - B1x)) / (-s2_x * s1_y + s1_x * s2_y)
s = CpMathUtil.divide(-s1_y * (A1x - B1x) + s1_x * (A1y - B1y), -s2_x * s1_y + s1_x * s2_y)
t = CpMathUtil.divide( s2_x * (A1y - B1y) - s2_y * (A1x - B1x), -s2_x * s1_y + s1_x * s2_y)

if (s >= 0 and s <= 1 and t >= 0 and t <= 1) then
--Collision detected
Expand Down
Loading