README.md update

pull/1/head
E. Almqvist 3 years ago
parent 0556a9fdde
commit 401b93f0b8
  1. 3
      README.md
  2. 2
      app.rb
  3. 74
      objects.rb
  4. 79
      physobj.rb
  5. 0
      planet.rb

@ -1,2 +1,3 @@
# Orbital Physics Simulator
# Hohmann Miner
Mine stuff and then get more stuff but in space.
![](preview.png)

@ -2,8 +2,10 @@
require "matrix"
require "gosu"
load "gosu_plugin.rb"
load "physobj.rb"
load "controller.rb"
load "planet.rb"
class Window < Gosu::Window
attr_accessor :freeze, :caption, :physobjs, :planets, :controller, :camera

@ -0,0 +1,74 @@
class Planet < PhysObj
attr_reader :color, :mass, :radius, :circle_thickness
def initialize(name, world, color, mass=1e+2, radius=20, circle_thickness=4)
super name, world
@color = color
@mass = mass
@radius = radius
@circle_thickness = circle_thickness
end
def physics
self.tick
end
private def calculate_gravity_scalar(obj, dir_vec)
grav = GRAV_CONSTANT * (self.mass/(dir_vec.magnitude**2))
return grav
end
private def calculate_gravity_vector(obj)
dir_vec = self.pos - obj.pos + Vector[@radius/2, @radius/2]
return (dir_vec/dir_vec.magnitude) * calculate_gravity_scalar(obj, dir_vec)
end
def orbit(physobjs)
physobjs.each do |obj|
if( self != obj ) then
grav_vec = self.calculate_gravity_vector(obj)
obj.accel_vecs[self.name] = grav_vec
obj.apply_accel_vecs
obj.parent_orbit = self
end
end
end
# def debug_string
# return "\n#{self.name}\nPos: #{self.pos.round(4)}\nMass: #{self.mass.round(4)}\nRadius: #{self.radius.round(4)} p\nGravity: #{(self.mass*GRAV_CONSTANT).round(2)} p/r^2"
#end
def render(x_offset=0, y_offset=0)
super x_offset, y_offset, Gosu::Color.argb(0xff_ffffff)
Gosu.draw_circle(self.pos[0] + x_offset, self.pos[1] + y_offset, @radius, @color, 0, @circle_thickness)
end
def width
return @radius
end
def height
return @radius
end
end
class PhysCube < PhysObj
attr_reader :width, :height
def initialize(name, world, width, height, color=0xff_ffffff)
super name, world
@width, @height = width, height
@color = Gosu::Color.argb(color)
end
def render(offset_x=0, offset_y=0)
super offset_x, offset_y, @color
x, y = offset_x + self.pos[0], offset_y + self.pos[1]
Gosu.draw_quad(x, y, @color, x + self.width, y, @color, x, y + self.height, @color, x + self.width, y + self.height, @color)
end
def physics
self.tick
end
end

@ -85,82 +85,3 @@ class PhysObj
self.draw_vector(dir_vec, 25, 0xaf_aaaaff, x_offset, y_offset)
end
end
class PhysCube < PhysObj
attr_reader :width, :height
def initialize(name, world, width, height, color=0xff_ffffff)
super name, world
@width, @height = width, height
@color = Gosu::Color.argb(color)
end
def render(offset_x=0, offset_y=0)
super offset_x, offset_y, @color
x, y = offset_x + self.pos[0], offset_y + self.pos[1]
Gosu.draw_quad(x, y, @color, x + self.width, y, @color, x, y + self.height, @color, x + self.width, y + self.height, @color)
end
def physics
self.tick
end
end
class Planet < PhysObj
attr_reader :color, :mass, :radius, :circle_thickness
def initialize(name, world, color, mass=1e+2, radius=20, circle_thickness=4)
super name, world
@color = color
@mass = mass
@radius = radius
@circle_thickness = circle_thickness
end
def physics
self.tick
end
private def calculate_gravity_scalar(obj, dir_vec)
grav = GRAV_CONSTANT * (self.mass/(dir_vec.magnitude**2))
return grav
end
private def calculate_gravity_vector(obj)
dir_vec = self.pos - obj.pos + Vector[@radius/2, @radius/2]
return (dir_vec/dir_vec.magnitude) * calculate_gravity_scalar(obj, dir_vec)
end
def orbit(physobjs)
physobjs.each do |obj|
if( self != obj ) then
grav_vec = self.calculate_gravity_vector(obj)
obj.accel_vecs[self.name] = grav_vec
obj.apply_accel_vecs
obj.parent_orbit = self
end
end
end
# def debug_string
# return "\n#{self.name}\nPos: #{self.pos.round(4)}\nMass: #{self.mass.round(4)}\nRadius: #{self.radius.round(4)} p\nGravity: #{(self.mass*GRAV_CONSTANT).round(2)} p/r^2"
#end
def render(x_offset=0, y_offset=0)
super x_offset, y_offset, Gosu::Color.argb(0xff_ffffff)
Gosu.draw_circle(self.pos[0] + x_offset, self.pos[1] + y_offset, @radius, @color, 0, @circle_thickness)
end
def width
return @radius
end
def height
return @radius
end
end

Loading…
Cancel
Save