Minor bug fix

pull/1/head
E. Almqvist 3 years ago
parent 430c7b2ee9
commit d7222f3a2b
  1. 31
      app.rb
  2. 60
      controller.rb
  3. 57
      physobj.rb

@ -1,6 +1,9 @@
#!/usr/bin/ruby -w #!/usr/bin/ruby -w
require "matrix"
require "gosu" require "gosu"
load "gosu_plugin.rb"
load "physobj.rb" load "physobj.rb"
load "controller.rb"
class Window < Gosu::Window class Window < Gosu::Window
attr_accessor :freeze, :caption, :physobjs, :planets, :controller, :camera attr_accessor :freeze, :caption, :physobjs, :planets, :controller, :camera
@ -34,15 +37,15 @@ class Window < Gosu::Window
if( @controller != nil ) then if( @controller != nil ) then
@controller.button_up(id) @controller.button_up(id)
end end
if( id == Gosu::KbEscape ) then
@freeze = !@freeze
end
end end
def button_down(id) def button_down(id)
super id super id
if( id == Gosu::KbEscape ) then
@freeze = !@freeze
end
if( @controller != nil ) then if( @controller != nil ) then
@controller.button_down(id) @controller.button_down(id)
end end
@ -81,6 +84,7 @@ class Window < Gosu::Window
def draw def draw
if( @controller != nil ) then if( @controller != nil ) then
@camera = Vector[self.width/2, self.height/2] - @controller.pos @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 end
camx, camy = @camera[0], @camera[1] camx, camy = @camera[0], @camera[1]
@ -103,12 +107,8 @@ end
window = Window.new("Physics!", 1600, 900) 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 = Player.new("Alpha", window, 8, 8)
cube.show_info = true cube.show_info = false
cube.thrust = 0.0075 cube.thrust = 0.0075
cube.pos = Vector[800, 450 + 200] cube.pos = Vector[800, 450 + 200]
cube.vel = Vector[2.5, 0] cube.vel = Vector[2.5, 0]
@ -119,9 +119,20 @@ cube2.pos = Vector[800, 450 + 300]
cube2.vel = Vector[-1.24, 0] cube2.vel = Vector[-1.24, 0]
cube2.show_info = true 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.planets << planet
window.physobjs << cube window.physobjs << cube
window.physobjs << cube2 window.physobjs << cube2
window.show window.show

@ -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

@ -1,6 +1,3 @@
require "matrix"
load "gosu_plugin.rb"
GRAV_CONSTANT = 1e+1 GRAV_CONSTANT = 1e+1
MAX_PATH_TRACK_POINT = 1000 MAX_PATH_TRACK_POINT = 1000
@ -41,7 +38,7 @@ class PhysObj
def render_path(x_offset=0, y_offset=0) def render_path(x_offset=0, y_offset=0)
@saved_pos.each do |pos| @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
end end
@ -126,12 +123,14 @@ class Planet < PhysObj
end end
def orbit(physobjs) def orbit(physobjs)
if( !self.world.freeze ) then
physobjs.each do |obj| physobjs.each do |obj|
grav_vec = self.calculate_gravity_vector(obj) grav_vec = self.calculate_gravity_vector(obj)
obj.accel = grav_vec obj.accel = grav_vec
obj.parent_orbit = self obj.parent_orbit = self
end end
end end
end
private def debug_string private 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" 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"
@ -144,53 +143,3 @@ class Planet < PhysObj
end 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

Loading…
Cancel
Save