Use # operator for vector length
This commit is contained in:
@@ -44,7 +44,7 @@ ball = {
|
|||||||
for b in all(balls) do
|
for b in all(balls) do
|
||||||
if (b.id != self.id and (collisions[b] == nil or collisions[b][self] == nil)) then
|
if (b.id != self.id and (collisions[b] == nil or collisions[b][self] == nil)) then
|
||||||
local diff = self.pos - b.pos
|
local diff = self.pos - b.pos
|
||||||
if (diff:length() < self.rad + b.rad) then
|
if (#diff < self.rad + b.rad) then
|
||||||
collisions[self] = collisions[self] or {}
|
collisions[self] = collisions[self] or {}
|
||||||
collisions[self][b] = diff
|
collisions[self][b] = diff
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ function handle_collisions()
|
|||||||
rewind = 0.1 * steps
|
rewind = 0.1 * steps
|
||||||
s_xpoint = source.pos - source.vel * rewind
|
s_xpoint = source.pos - source.vel * rewind
|
||||||
t_xpoint = target.pos - target.vel * rewind
|
t_xpoint = target.pos - target.vel * rewind
|
||||||
until rewind > 1 or (s_xpoint - t_xpoint):length() >= source.rad + target.rad
|
until rewind > 1 or #(s_xpoint - t_xpoint) >= source.rad + target.rad
|
||||||
local s_newvel = collision_vel(s_xpoint, source.vel, t_xpoint, target.vel, source:mass(), target:mass())
|
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())
|
local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel, target:mass(), source:mass())
|
||||||
source.pos = s_xpoint + source.vel * rewind
|
source.pos = s_xpoint + source.vel * rewind
|
||||||
@@ -65,5 +65,5 @@ function collision_vel(x1,v1,x2,v2,m1,m2)
|
|||||||
m2 = m2 or 1
|
m2 = m2 or 1
|
||||||
local x_diff = x1 - x2
|
local x_diff = x1 - x2
|
||||||
local v_diff = v1 - v2
|
local v_diff = v1 - v2
|
||||||
return v1 - x_diff * (2 * m2 / (m1 + m2)) * v_diff:dot(x_diff) / x_diff:length()^2
|
return v1 - x_diff * (2 * m2 / (m1 + m2)) * v_diff:dot(x_diff) / (#x_diff)^2
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,8 +18,7 @@ vec2 = {
|
|||||||
end,
|
end,
|
||||||
|
|
||||||
normal = function(self)
|
normal = function(self)
|
||||||
local length = self:length()
|
return vec2:new(self.x/#self, self.y/#self)
|
||||||
return vec2:new(self.x/length, self.y/length)
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
bounce = function(self, n)
|
bounce = function(self, n)
|
||||||
@@ -44,6 +43,7 @@ vec2.__mul = function(o1,o2)
|
|||||||
end
|
end
|
||||||
vec2.__div = function(v,s) return vec2:new(v.x/s,v.y/s) end
|
vec2.__div = function(v,s) return vec2:new(v.x/s,v.y/s) end
|
||||||
vec2.__unm = function(v) return vec2:new(-v.x,-v.y) end
|
vec2.__unm = function(v) return vec2:new(-v.x,-v.y) end
|
||||||
|
vec2.__len = function(v) return v:length() end
|
||||||
vec2.__tostring = function(v) return "{x: "..v.x..",y: "..v.y.."}" end
|
vec2.__tostring = function(v) return "{x: "..v.x..",y: "..v.y.."}" end
|
||||||
vec2.__index = vec2
|
vec2.__index = vec2
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ ball = {
|
|||||||
for b in all(balls) do
|
for b in all(balls) do
|
||||||
if (b.id != self.id and (collisions[b] == nil or collisions[b][self] == nil)) then
|
if (b.id != self.id and (collisions[b] == nil or collisions[b][self] == nil)) then
|
||||||
local diff = self.pos - b.pos
|
local diff = self.pos - b.pos
|
||||||
if (diff:length() < self.rad + b.rad) then
|
if (#diff < self.rad + b.rad) then
|
||||||
collisions[self] = collisions[self] or {}
|
collisions[self] = collisions[self] or {}
|
||||||
collisions[self][b] = diff
|
collisions[self][b] = diff
|
||||||
end
|
end
|
||||||
@@ -185,7 +185,7 @@ function handle_collisions()
|
|||||||
rewind = 0.1 * steps
|
rewind = 0.1 * steps
|
||||||
s_xpoint = source.pos - source.vel * rewind
|
s_xpoint = source.pos - source.vel * rewind
|
||||||
t_xpoint = target.pos - target.vel * rewind
|
t_xpoint = target.pos - target.vel * rewind
|
||||||
until rewind > 1 or (s_xpoint - t_xpoint):length() >= source.rad + target.rad
|
until rewind > 1 or #(s_xpoint - t_xpoint) >= source.rad + target.rad
|
||||||
local s_newvel = collision_vel(s_xpoint, source.vel, t_xpoint, target.vel, source:mass(), target:mass())
|
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())
|
local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel, target:mass(), source:mass())
|
||||||
source.pos = s_xpoint + source.vel * rewind
|
source.pos = s_xpoint + source.vel * rewind
|
||||||
@@ -201,7 +201,7 @@ function collision_vel(x1,v1,x2,v2,m1,m2)
|
|||||||
m2 = m2 or 1
|
m2 = m2 or 1
|
||||||
local x_diff = x1 - x2
|
local x_diff = x1 - x2
|
||||||
local v_diff = v1 - v2
|
local v_diff = v1 - v2
|
||||||
return v1 - x_diff * (2 * m2 / (m1 + m2)) * v_diff:dot(x_diff) / x_diff:length()^2
|
return v1 - x_diff * (2 * m2 / (m1 + m2)) * v_diff:dot(x_diff) / (#x_diff)^2
|
||||||
end
|
end
|
||||||
|
|
||||||
__gfx__
|
__gfx__
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ vec2 = {
|
|||||||
end,
|
end,
|
||||||
|
|
||||||
normal = function(self)
|
normal = function(self)
|
||||||
local length = self:length()
|
return vec2:new(self.x/#self, self.y/#self)
|
||||||
return vec2:new(self.x/length, self.y/length)
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
bounce = function(self, n)
|
bounce = function(self, n)
|
||||||
@@ -41,6 +40,7 @@ vec2.__mul = function(o1,o2)
|
|||||||
end
|
end
|
||||||
vec2.__div = function(v,s) return vec2:new(v.x/s,v.y/s) end
|
vec2.__div = function(v,s) return vec2:new(v.x/s,v.y/s) end
|
||||||
vec2.__unm = function(v) return vec2:new(-v.x,-v.y) end
|
vec2.__unm = function(v) return vec2:new(-v.x,-v.y) end
|
||||||
|
vec2.__len = function(v) return v:length() end
|
||||||
vec2.__tostring = function(v) return "{x: "..v.x..",y: "..v.y.."}" end
|
vec2.__tostring = function(v) return "{x: "..v.x..",y: "..v.y.."}" end
|
||||||
vec2.__index = vec2
|
vec2.__index = vec2
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user