-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation2.rb
More file actions
executable file
·50 lines (42 loc) · 1.19 KB
/
simulation2.rb
File metadata and controls
executable file
·50 lines (42 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env ruby
# https://vectr.com/tmp/b1vPZfoE0b/a1Q2d7s6e
require './lib/render'
require './lib/physics'
def Vector.random(range = 0..1.0)
Vector[rand(range), rand(range)]
end
class TailedObject
include Physics::PhysicsObject
attr_accessor :path
def initialize(position:, velocity: Vector[0, 0])
@path = []
@path << position.dup << position.dup
sim_params(position: position, velocity: velocity)
end
def step(time)
@last ||= time
if time - @last < 0.2
@path[0] = position.dup unless @path.empty?
return
end
@last = time
@path.unshift position.dup
@path.slice!(12..-1)
end
end
objects = []
objects += Array.new(10) { TailedObject.new position: Vector.random(0..500), velocity: Vector.random(-1.0..1.0)*0.2 }
renderer = Render.new
renderer.scene = { objects: objects,
types: { TailedObject => { circle: ->(o) { o.position }, path: ->(o) { o.path } } } }
physics = Physics.new
physics.forces << proc do |obj|
center = Vector[300, 300]
k = 0.0001
(center - obj.position).normalize * k
end
renderer.run do
time = Time.now.to_f
objects.each { |obj| obj.step(time) if obj.respond_to? :step }
physics.step objects, time
end