From 491d94d6475ce12624c40bcb061a66eca4c76f8c Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 28 Dec 2021 15:40:08 -0800 Subject: [PATCH] Change mag to length --- ball.lua | 2 +- main.lua | 4 ++-- qphys.p8 | 28 +++++++++++++--------------- vec2.lua | 22 ++++++++++------------ 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/ball.lua b/ball.lua index 00f4e29..eef9fec 100644 --- a/ball.lua +++ b/ball.lua @@ -44,7 +44,7 @@ ball = { for b in all(balls) do if (b.id != self.id and (collisions[b] == nil or collisions[b][self] == nil)) then local diff = self.pos - b.pos - if ((diff):mag() < self.rad + b.rad) then + if (diff:length() < self.rad + b.rad) then collisions[self] = collisions[self] or {} collisions[self][b] = diff end diff --git a/main.lua b/main.lua index 269e02c..869a7de 100644 --- a/main.lua +++ b/main.lua @@ -49,7 +49,7 @@ function handle_collisions() rewind = 0.1 * steps s_xpoint = source.pos - source.vel * rewind t_xpoint = target.pos - target.vel * rewind - until rewind > 1 or (s_xpoint - t_xpoint):mag() >= source.rad + target.rad + until rewind > 1 or (s_xpoint - t_xpoint):length() >= source.rad + target.rad local s_newvel = collision_vel(s_xpoint, source.vel, t_xpoint, target.vel, source:mass(), target:mass()) local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel, target:mass(), source:mass()) source.pos = s_xpoint + source.vel * rewind @@ -65,5 +65,5 @@ function collision_vel(x1,v1,x2,v2,m1,m2) m2 = m2 or 1 local x_diff = x1 - x2 local v_diff = v1 - v2 - return v1 - x_diff * (2 * m2 / (m1 + m2)) * v_diff:dot(x_diff) / x_diff:mag()^2 + return v1 - x_diff * (2 * m2 / (m1 + m2)) * v_diff:dot(x_diff) / x_diff:length()^2 end diff --git a/qphys.p8 b/qphys.p8 index 4e2214e..dc4c597 100644 --- a/qphys.p8 +++ b/qphys.p8 @@ -5,7 +5,11 @@ vec2 = { x = 0, y = 0, - mag = function(self) + angle = function(self) + return atan2(y, x); + end, + + length = function(self) return sqrt(self.x^2+self.y^2) end, @@ -13,12 +17,12 @@ vec2 = { return self.x * v.x + self.y * v.y end, - normal = function(self) - local mag = self:mag() - return vec2:new(self.x/mag, self.y/mag) + normal = function(self) + local length = self:length() + return vec2:new(self.x/length, self.y/length) end, - bounce = function(self, n) + bounce = function(self, n) local n = n:normal() return n * -2 * self:dot(n) + self end, @@ -55,19 +59,13 @@ end function vec2:print_debug(v) local v = v or vec2:new(0,1) print("vec: "..vec2.__tostring(self)) - print("mag: "..self:mag()) + print("length: "..self:length()) print("dot: "..self:dot(v)) print("normal: "..vec2.__tostring(self:normal())) print("bounce: "..vec2.__tostring(self:bounce(v))) print("inv: "..vec2.__tostring(-self)) end -local v = vec2:new(1,1) -local s = 3 -print(v * s) -print(s * v) -assert(false, "If two lines above are identical, mul operands are now interchangeable!") - -->8 ball = { __count = 0, @@ -115,7 +113,7 @@ ball = { for b in all(balls) do if (b.id != self.id and (collisions[b] == nil or collisions[b][self] == nil)) then local diff = self.pos - b.pos - if ((diff):mag() < self.rad + b.rad) then + if (diff:length() < self.rad + b.rad) then collisions[self] = collisions[self] or {} collisions[self][b] = diff end @@ -187,7 +185,7 @@ function handle_collisions() rewind = 0.1 * steps s_xpoint = source.pos - source.vel * rewind t_xpoint = target.pos - target.vel * rewind - until rewind > 1 or (s_xpoint - t_xpoint):mag() >= source.rad + target.rad + until rewind > 1 or (s_xpoint - t_xpoint):length() >= source.rad + target.rad local s_newvel = collision_vel(s_xpoint, source.vel, t_xpoint, target.vel, source:mass(), target:mass()) local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel, target:mass(), source:mass()) source.pos = s_xpoint + source.vel * rewind @@ -203,7 +201,7 @@ function collision_vel(x1,v1,x2,v2,m1,m2) m2 = m2 or 1 local x_diff = x1 - x2 local v_diff = v1 - v2 - return v1 - x_diff * (2 * m2 / (m1 + m2)) * v_diff:dot(x_diff) / x_diff:mag()^2 + return v1 - x_diff * (2 * m2 / (m1 + m2)) * v_diff:dot(x_diff) / x_diff:length()^2 end __gfx__ diff --git a/vec2.lua b/vec2.lua index 0d35dce..3b7c92d 100644 --- a/vec2.lua +++ b/vec2.lua @@ -2,7 +2,11 @@ vec2 = { x = 0, y = 0, - mag = function(self) + angle = function(self) + return atan2(y, x); + end, + + length = function(self) return sqrt(self.x^2+self.y^2) end, @@ -10,12 +14,12 @@ vec2 = { return self.x * v.x + self.y * v.y end, - normal = function(self) - local mag = self:mag() - return vec2:new(self.x/mag, self.y/mag) + normal = function(self) + local length = self:length() + return vec2:new(self.x/length, self.y/length) end, - bounce = function(self, n) + bounce = function(self, n) local n = n:normal() return n * -2 * self:dot(n) + self end, @@ -52,15 +56,9 @@ end function vec2:print_debug(v) local v = v or vec2:new(0,1) print("vec: "..vec2.__tostring(self)) - print("mag: "..self:mag()) + print("length: "..self:length()) print("dot: "..self:dot(v)) print("normal: "..vec2.__tostring(self:normal())) print("bounce: "..vec2.__tostring(self:bounce(v))) print("inv: "..vec2.__tostring(-self)) end - -local v = vec2:new(1,1) -local s = 3 -print(v * s) -print(s * v) -assert(false, "If two lines above are identical, mul operands are now interchangeable!")