From 401b93f0b896114eaaedea9780bceaecd975b6a4 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 18 Oct 2021 08:30:31 +0200 Subject: [PATCH] README.md update --- README.md | 3 ++- app.rb | 2 ++ objects.rb | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ physobj.rb | 79 ------------------------------------------------------ planet.rb | 0 5 files changed, 78 insertions(+), 80 deletions(-) create mode 100644 objects.rb delete mode 100644 planet.rb diff --git a/README.md b/README.md index d0519ed..eef2f71 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -# Orbital Physics Simulator +# Hohmann Miner +Mine stuff and then get more stuff but in space. ![](preview.png) diff --git a/app.rb b/app.rb index 0a60033..89c8d5f 100755 --- a/app.rb +++ b/app.rb @@ -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 diff --git a/objects.rb b/objects.rb new file mode 100644 index 0000000..e8cab04 --- /dev/null +++ b/objects.rb @@ -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 diff --git a/physobj.rb b/physobj.rb index e980589..880da5e 100644 --- a/physobj.rb +++ b/physobj.rb @@ -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 - - diff --git a/planet.rb b/planet.rb deleted file mode 100644 index e69de29..0000000