-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation4.rb
executable file
·162 lines (139 loc) · 4.48 KB
/
simulation4.rb
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env ruby
# https://vectr.com/tmp/b1vPZfoE0b/a1Q2d7s6e
require './lib/render'
require './lib/physics_verlet'
$stdout.sync = true
class Scene
include Enumerable
def initialize(*args)
@array = Array.new(*args)
end
def <<(element)
@array.push element
end
def each(&block)
recursive @array, block
end
def recursive(array, visitor)
array.each do |el|
el.respond_to?(:children) ? recursive(el.children, visitor) : visitor[el]
end
end
end
class Engine
include PhysicsVerlet::PhysicsForce
PRIORITY = 0
def initialize(node:, power: 1.0, dynamics: 1.0)
@node = node
@power = power
@dynamics = dynamics
end
def step
@node.force_sum += Vector[0, -500]
end
end
class Ship
def children(&block)
return enum_for(:children) unless block_given?
@engines.each(&block)
@nodes.each(&block)
@links.each(&block)
end
# https://vectr.com/tmp/aCds4rYmV/gJq52axTr
# https://www.youtube.com/watch?v=DFOynlRYT54
# https://www.reddit.com/r/starcitizen/comments/6n5nza/springy_landing_gear/
# https://www.gamedev.net/articles/programming/math-and-physics/towards-a-simpler-stiffer-and-more-stable-spring-r3227/
# http://blog.rectorsquid.com/2018/05/
# TODO: Blender https://github.com/RMKD/scripting-blender-game-engine/blob/master/tutorial.md
def initialize(position:, velocity: Vector[0, 0], size: 200, mass: 100.0)
@nodes = [
Node.new(position: position - Vector[size * 0.5, 0], velocity: velocity, mass: mass * 0.3),
Node.new(position: position + Vector[-size * 0.3, -size * 0.4], velocity: velocity, mass: mass * 0.35),
Node.new(position: position + Vector[size * 0.3, -size * 0.4], velocity: velocity, mass: mass * 0.35),
Node.new(position: position + Vector[size * 0.5, 0], velocity: velocity, mass: mass * 0.3),
Node.new(position: position + Vector[0, size * 0.05], velocity: velocity, mass: mass * 0.1),
Node.new(position: position + Vector[-size * 0.8, size * 0.3], velocity: velocity, mass: mass * 0.01),
Node.new(position: position + Vector[+size * 0.8, size * 0.3], velocity: velocity, mass: mass * 0.01)
]
@links = [
[0, 1], [1, 2], [2, 3], [3, 0], [0, 2], [1, 3], [0, 4], [4, 3], [1, 4], [2,4]
].map{ |pair| SoftLink.new *pair.map{ |i| @nodes[i] } }
@links << HardLink.new(@nodes[0], @nodes[5])
@links << SoftLink.new(@nodes[4], @nodes[5], 400)
@links << HardLink.new(@nodes[3], @nodes[6])
@links << SoftLink.new(@nodes[4], @nodes[6], 400)
@engines = []
@engines << Engine.new(node: @nodes[0])
@engines << Engine.new(node: @nodes[3])
end
end
class CollisionDetection
include PhysicsVerlet::PhysicsForce
PRIORITY = 100
def initialize(objects)
@objects = objects
end
def step
@objects.each do |o|
if o.position[1] > 400 # && o.velocity[1] > -0.01
depth = o.position[1] - 400
o.force_sum += Vector[0, -200 * depth]
# - bn(n.v)
end
end
end
end
world = Scene.new
world << Ship.new(position: Vector[250, 100])
renderer = Render.new title: 'Ship'
renderer.scene = { objects: world,
types: {
Node => proc { |r, o|
r.circle o.position, o.mass * 0.2
glColor3fv [1.0, 0.5, 0.5]
r.path [o.position, o.position + o.force * 0.1]
},
HardLink => { path: ->(a) { a.pair.map(&:position) } },
SoftLink => proc { |r, o|
glColor3fv [1 - o.displacement * 0.01, 0, 1 + o.displacement * 0.01]
r.path o.pair.map(&:position)
},
}
}
physics = PhysicsVerlet.new
world << CollisionDetection.new(world.select { |o| o.class.ancestors.include? PhysicsVerlet::PhysicsObject })
# Gravity
physics.forces << proc do |obj|
9.81 * obj.mass * Vector[0, 1]
end
class FPS
def initialize(title)
@title = title
@p_time = Time.new.to_f
@list = Array.new 100, 0
@last = Time.now.to_f
end
def ping
@list.push (Time.new.to_f - @last).to_f
@last = Time.now.to_f
@list.shift
end
def print
ping
if Time.new.to_f - @p_time > 2
avg = @list.inject(&:+) / @list.size
puts "#{@title} FPS: #{1 / avg}"
@p_time = Time.new.to_f
end
end
end
p_fps = FPS.new :Physics
fps = FPS.new :Render
renderer.run do
c = 0.1 / PhysicsVerlet::STEP_TIME
c.to_i.times do
physics.step world
p_fps.print
end
fps.print
end