# Agents.elastic\_collision!() makes ageents move faster and faster

**URL:** <https://discourse.julialang.org/t/agents-elastic-collision-makes-ageents-move-faster-and-faster/84740>\
**Category:** Modelling & Simulations\
**Tags:** question\
**Created:** [July 25, 2022, 8:06am UTC](https://discourse.julialang.org/t/agents-elastic-collision-makes-ageents-move-faster-and-faster/84740 "2022-07-25T08:06:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wen-Wei\_Tseng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wen-wei_tseng/32/28839_2.png) [@Wen-Wei\_Tseng](https://discourse.julialang.org/u/Wen-Wei_Tseng)\
**Post date:** [July 25, 2022, 8:06am UTC](https://discourse.julialang.org/t/agents-elastic-collision-makes-ageents-move-faster-and-faster/84740/1 "2022-07-25T08:06:54Z")

</div>

I tried replicating the results of [social distancing example](https://juliadynamics.github.io/AgentsExampleZoo.jl/dev/examples/social_distancing/) in Agents.jl Example Zoo. And the agents (balls) moved faster and faster in the output video. I was using Julia 1.8.0-rc3 (I’ve tried Julia 1.7.3, but the results were the same.)

```julia
using Agents
using Random
using InteractiveDynamics
using CairoMakie

mutable struct Ball <: AbstractAgent
    id::Int # Mandatory Agent identifier
    pos::NTuple{2,Float64} # Position, required for agents in the ContinuousSpace
    vel::NTuple{2,Float64} # Moving speeds
    mass::Float64 # Can move or not
end

function ball_model(; speed = 0.002, seed = 42)
    space2d = ContinuousSpace((1, 1); spacing = 0.02)
    model = ABM(Ball, space2d, properties = Dict(:dt => 1.0), rng = MersenneTwister(seed))

    # Add agents to the model
    for i in 1:500
        pos = Tuple(rand(model.rng, 2))
        vel =sincos(2π * rand(model.rng)) .* speed
        mass = 1.0
        add_agent!(pos, model, vel, mass)
    end
    return model
end

agent_step!(agent, model) = move_agent!(agent, model, model.dt)

# Billiard-like interaction
function model_step!(model)
    for (a1, a2) in interacting_pairs(model, 0.012, :nearest)
        elastic_collision!(a1, a2, :mass)
    end
end

model2 = ball_model()

abmvideo(
    "socialdist2.mp4",
    model2,
    agent_step!,
    model_step!;
    title = "Billiard-like",
    frames = 50,
    spf = 2,
    framerate = 25,
)

```

Package version

```julia
[46ada45e] Agents v5.4.2
[13f3f980] CairoMakie v0.8.12
[a93c6f00] DataFrames v1.3.4
[31c24e10] Distributions v0.25.66
[86223c79] Graphs v1.7.1
[ec714cd0] InteractiveDynamics v0.21.10

```

---

<div class="post-metadata">

**Author:** ![Wen-Wei\_Tseng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wen-wei_tseng/32/28839_2.png) [@Wen-Wei\_Tseng](https://discourse.julialang.org/u/Wen-Wei_Tseng)\
**Post date:** [August 14, 2022, 12:18pm UTC](https://discourse.julialang.org/t/agents-elastic-collision-makes-ageents-move-faster-and-faster/84740/2 "2022-08-14T12:18:41Z")

</div>

Replying to myself:  
The fix is under review

> <https://github.com/JuliaDynamics/Agents.jl/pull/676>
>
> fix #675
> 
> I also renewed the facing logic to:
> 
> \`\`\`julia
> !(dot(r2, v1) \> 0 &…& dot(r1, v2) \> 0) && return false
> \`\`\`
