我想编写一个函数(我们称之为 [filter "hack-debug-lines-in-python"]
clean = ...
smudge = ...
),更新盒子内粒子的位置和速度。
我已经有一个函数来描述两个粒子之间的碰撞。
.git/config
update!
我知道函数 mutable struct Particle
pos :: Vector{Float64}
vel :: Vector{Float64}
end
p1 = Particle( rand(2) , rand(2) )
p2 = Particle( rand(2) , rand(2) )
必须:
function collision!(p1::Particle, p2::Particle)
# Find collision vector
n = p1.pos - p2.pos
# Normalize it, since you want an orthonormal basis
n ./= sqrt(n[1]^2 + n[2]^2)
# Construct M
M = [n[1] n[2]; -n[2] n[1]]
# Find transformed velocity vectors
v1ₙ = M*p1.vel
v2ₙ = M*p2.vel
# Swap first component
v1ₙ[1], v2ₙ[1] = v2ₙ[1], v1ₙ[1]
# Calculate and store new velocity vectors
p1.vel .= M'*v1ₙ
p2.vel .= M'*v2ₙ
return nothing
end
处理与其他粒子的所有可能碰撞。 update!
来完成)collsion!
xlims, ylims
我认为我的理论是正确的,但我不确定如何实施它。任何帮助将不胜感激。