Created
October 23, 2012 21:48
-
-
Save siers/3941870 to your computer and use it in GitHub Desktop.
Catching object in orbit with variable catcher force
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'matrix' | |
module Drawing | |
ZOOM = 0.5 | |
SCALE = 40 | |
def clear | |
@@clear ||= %x{clear} | |
$>.write @@clear | |
end | |
def field | |
vertial = " " * SCALE << "|" << " " * SCALE << "\n" | |
horizontal = "-" * SCALE << "x" << "-" * SCALE | |
$>.write vertial * (SCALE / 2) | |
puts horizontal | |
$>.write vertial * (SCALE / 2) | |
end | |
def putc | |
object = @object.to_a | |
object = object.each_with_index.map do |f, index| | |
f = f * SCALE * ZOOM + SCALE | |
f /= 2 if index == 0 | |
f.to_i | |
end | |
$>.write "\e[#{ object[0] + 2 };#{ object[1] + 1 }Ho" | |
end | |
end | |
class Simulation | |
FPS = 30 | |
DETAIL = 5 | |
include Drawing | |
def initialize | |
@object = Vector[ 0, 2] | |
@velocity = Vector[0.6, 0] | |
end | |
# Acceleration vector from coil at (0, 0) with strenght 1. | |
def coil_accel | |
@object / -@object.r | |
end | |
def run | |
loop do | |
clear | |
puts "Object is at %.2f x %.2f; velocity: %.2f\n" % [@object[0], @object[1], @velocity.r] | |
field | |
putc | |
slowness = 1.0 / DETAIL | |
strenght = @velocity.r | |
@velocity += coil_accel * strenght * slowness | |
@object += @velocity * slowness | |
sleep 1.0 / FPS | |
end | |
end | |
end | |
Simulation.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Object is on 2, 0 on a 2d plane. There's a coil in the middle that's pulling the object towards it.
a_anti_object = v_object / - |v_object|
a_coil = a_anti_object * |v_object|
F_coil = |a_coil|
Each second, v_object = v_object + a_coil. Soon it reaches force equilibrium and the object is orbiting the coil.