diff --git a/app.rb b/app.rb index 3559582..438fe5b 100755 --- a/app.rb +++ b/app.rb @@ -1,6 +1,9 @@ #!/usr/bin/ruby -w +require "matrix" require "gosu" +load "gosu_plugin.rb" load "physobj.rb" +load "controller.rb" class Window < Gosu::Window attr_accessor :freeze, :caption, :physobjs, :planets, :controller, :camera @@ -34,15 +37,15 @@ class Window < Gosu::Window if( @controller != nil ) then @controller.button_up(id) end - - if( id == Gosu::KbEscape ) then - @freeze = !@freeze - end end def button_down(id) super id + if( id == Gosu::KbEscape ) then + @freeze = !@freeze + end + if( @controller != nil ) then @controller.button_down(id) end @@ -81,6 +84,7 @@ class Window < Gosu::Window def draw if( @controller != nil ) then @camera = Vector[self.width/2, self.height/2] - @controller.pos + @font.draw_text(@controller.debug_string, 0, 32, 1, 1.0, 1.0, Gosu::Color::WHITE) end camx, camy = @camera[0], @camera[1] @@ -103,12 +107,8 @@ end window = Window.new("Physics!", 1600, 900) -planet = Planet.new("Sol", window, 0xff_ffffaa, 1e2, 5, 12) -planet.pos = Vector[800, 450] -planet.show_info = false - cube = Player.new("Alpha", window, 8, 8) -cube.show_info = true +cube.show_info = false cube.thrust = 0.0075 cube.pos = Vector[800, 450 + 200] cube.vel = Vector[2.5, 0] @@ -119,9 +119,20 @@ cube2.pos = Vector[800, 450 + 300] cube2.vel = Vector[-1.24, 0] cube2.show_info = true -planet.orbit([cube, cube2]) +sol_orbiters = [cube, cube2] +sol = Planet.new("Sol", window, 0xff_ffffaa, 1e2, 5, 12) +sol.pos = Vector[800, 450] +sol.orbit(sol_orbiters) + +planet = Planet.new("Planet", window, 0xff_cccccc, 1e1, 2, 12) +planet.pos = Vector[200, 150] + + +window.planets << sol window.planets << planet + window.physobjs << cube window.physobjs << cube2 + window.show diff --git a/controller.rb b/controller.rb new file mode 100644 index 0000000..103fc54 --- /dev/null +++ b/controller.rb @@ -0,0 +1,60 @@ +class Player < PhysCube + attr_accessor :engine, :thrust + def initialize(name, world, width, height, color=0xff_ffffff) + super name, world, width, height, color + + @engine = false + @thrust = 0.001 + end + + private def get_angle_vec + rads = (self.angle * Math::PI) / 180 + dir_vec = Vector[Math::cos(rads), Math::sin(rads)] + return dir_vec + end + + def tick + super + + if( @engine && !self.world.freeze ) then + self.vel += self.get_angle_vec * @thrust + end + end + + def button_up(id) + end + + def button_up?(id) + end + + def button_down(id) + if( id == Gosu::KbSpace ) then + @engine = !@engine + end + + if( id == Gosu::KbLeft ) then + self.angle -= 20 + end + + if( id == Gosu::KbRight ) then + self.angle += 20 + end + + if( id == Gosu::KbUp ) then + self.thrust += 0.0005 + self.thrust = self.thrust.round(5) + end + + if( id == Gosu::KbDown ) then + self.thrust -= 0.0005 + self.thrust = self.thrust.round(5) + end + end + + def button_down?(id) + end + + def debug_string + return "\nName: #{self.name}\nOrbit of: #{self.parent_orbit.name}\nVel: #{self.vel.magnitude.round(1)} #{self.vel.round(4)}\nAccel: #{self.accel.magnitude.round(4)} #{self.accel.round(4)}\nPos: #{self.pos.round(4)}\nAngle: #{self.angle.round(1)} deg\nEngine: #{self.engine}\nThrust: #{self.thrust}\n" + end +end diff --git a/physobj.rb b/physobj.rb index 8fa3ade..3fdfa1d 100644 --- a/physobj.rb +++ b/physobj.rb @@ -1,6 +1,3 @@ -require "matrix" -load "gosu_plugin.rb" - GRAV_CONSTANT = 1e+1 MAX_PATH_TRACK_POINT = 1000 @@ -41,7 +38,7 @@ class PhysObj def render_path(x_offset=0, y_offset=0) @saved_pos.each do |pos| - Gosu.draw_rect(pos[0] + x_offset, pos[1] + y_offset, 1, 1, Gosu::Color.argb(0x44_ccccff)) + Gosu.draw_rect(pos[0] + x_offset, pos[1] + y_offset, 1, 1, Gosu::Color.argb(0xaa_ccccff)) end end @@ -126,10 +123,12 @@ class Planet < PhysObj end def orbit(physobjs) - physobjs.each do |obj| - grav_vec = self.calculate_gravity_vector(obj) - obj.accel = grav_vec - obj.parent_orbit = self + if( !self.world.freeze ) then + physobjs.each do |obj| + grav_vec = self.calculate_gravity_vector(obj) + obj.accel = grav_vec + obj.parent_orbit = self + end end end @@ -144,53 +143,3 @@ class Planet < PhysObj end -class Player < PhysCube - attr_accessor :engine, :thrust - def initialize(name, world, width, height, color=0xff_ffffff) - super name, world, width, height, color - - @engine = false - @thrust = 0.001 - end - - private def get_angle_vec - rads = (self.angle * Math::PI) / 180 - dir_vec = Vector[Math::cos(rads), Math::sin(rads)] - return dir_vec - end - - def tick - super - - if( @engine && !self.world.freeze ) then - self.vel += self.get_angle_vec * @thrust - end - end - - def button_up(id) - end - - def button_up?(id) - end - - def button_down(id) - if( id == Gosu::KbSpace ) then - @engine = !@engine - end - - if( id == Gosu::KbLeft ) then - self.angle -= 20 - end - - if( id == Gosu::KbRight ) then - self.angle += 20 - end - end - - def button_down?(id) - end - - private def debug_string - return "\n#{self.name} - #{self.parent_orbit.name}\nVel: #{self.vel.magnitude.round(1)} #{self.vel.round(4)}\nAccel: #{self.accel.magnitude.round(4)} #{self.accel.round(4)}\nPos: #{self.pos.round(4)}\nAngle: #{self.angle.round(1)} deg\nEngine: #{self.engine}\nThrust: #{self.thrust}\n" - end -end