Broken stuff

pull/1/head
E. Almqvist 4 years ago
parent 9ffab0b61c
commit 960343f33c
  1. 29
      app.rb
  2. 2
      controller.rb
  3. 36
      physobj.rb

@ -34,7 +34,7 @@ class Window < Gosu::Window
def button_up(id) def button_up(id)
super id super id
if( @controller != nil ) then if( @controller != nil && @controller.class == Player ) then
@controller.button_up(id) @controller.button_up(id)
end end
end end
@ -46,7 +46,7 @@ class Window < Gosu::Window
@freeze = !@freeze @freeze = !@freeze
end end
if( @controller != nil ) then if( @controller != nil && @controller.class == Player ) then
@controller.button_down(id) @controller.button_down(id)
end end
end end
@ -54,7 +54,7 @@ class Window < Gosu::Window
# def button_up?(id) # def button_up?(id)
# super id # super id
# #
# if( @controller != nil ) then # if( @controller != nil && @controller.class == Player ) then
# @controller.button_up?(id) # @controller.button_up?(id)
# end # end
# end # end
@ -62,7 +62,7 @@ class Window < Gosu::Window
# def button_down?(id) # def button_down?(id)
# super id # super id
# #
# if( @controller != nil ) then # if( @controller != nil && @controller.class == Player ) then
# @controller.button_down?(id) # @controller.button_down?(id)
# end # end
# end # end
@ -73,7 +73,11 @@ class Window < Gosu::Window
end end
@planets.each do |planet| @planets.each do |planet|
planet.orbit(@physobjs) orbiters = []
orbiters += @physobjs
orbiters += @planets
orbiters.delete(planet)
planet.orbit(planets)
end end
end end
@ -82,11 +86,12 @@ class Window < Gosu::Window
end end
def draw def draw
if( @controller != nil ) then if( @controller != nil && @controller.class == Player ) 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) @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]
# camx, camy = 0, 0
@font2.draw_text("Frozen: #{@freeze}", 0, 0, 1, 1.0, 1.0, Gosu::Color::WHITE) @font2.draw_text("Frozen: #{@freeze}", 0, 0, 1, 1.0, 1.0, Gosu::Color::WHITE)
@ -112,27 +117,29 @@ 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]
window.controller = cube
cube2 = PhysCube.new("Beta", window, 8, 8) cube2 = PhysCube.new("Beta", window, 8, 8)
cube2.pos = Vector[800, 450 + 300] 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
sol_orbiters = [cube, cube2] sol = Planet.new("Sol", window, 0xff_ffffaa, 1e2, 15, 1)
sol = Planet.new("Sol", window, 0xff_ffffaa, 1e2, 5, 12)
sol.pos = Vector[800, 450] sol.pos = Vector[800, 450]
sol.orbit(sol_orbiters)
planet = Planet.new("Planet", window, 0xff_cccccc, 1e1, 2, 12) planet = Planet.new("Planet", window, 0xff_cccccc, 1e1, 8, 1)
planet.pos = Vector[200, 150] planet.pos = Vector[200, 150]
planet.show_info = true
sol_orbiters = [cube, cube2, planet]
sol.orbit(sol_orbiters)
window.planets << sol window.planets << sol
window.planets << planet window.planets << planet
window.physobjs << cube window.physobjs << cube
window.physobjs << cube2 window.physobjs << cube2
window.physobjs << planet
window.controller = planet
window.show window.show

@ -55,6 +55,6 @@ class Player < PhysCube
end end
def debug_string 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" 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}\naccel_vecs: #{self.accel_vecs}\n"
end end
end end

@ -15,21 +15,21 @@ class PhysObj
@show_info = false @show_info = false
@angle = 0 @angle = 0
@parent_orbit = "" @parent_orbit = ""
@accel_vecs = [] @accel_vecs = Hash.new(Vector[0, 0])
end end
private def apply_accel_vecs def apply_accel_vecs
@accel_vecs.each do |vec| summed_vec = Vector.zero(2)
@accel += vec @accel_vecs.each do |planet, vec|
summed_vec += vec
end end
@accel_vecs = [] @accel = summed_vec
end end
def tick def tick
@x, @y = @pos[0], @pos[1] @x, @y = @pos[0], @pos[1]
@angle %= 360 @angle %= 360
self.apply_accel_vecs
if( !world.freeze ) then if( !world.freeze ) then
if( @accel.magnitude != 0 ) then if( @accel.magnitude != 0 ) then
@vel += @accel @vel += @accel
@ -52,7 +52,7 @@ class PhysObj
end end
end end
private def debug_string 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\n" 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\n"
end end
@ -122,6 +122,10 @@ class Planet < PhysObj
@circle_thickness = circle_thickness @circle_thickness = circle_thickness
end end
def physics
self.tick
end
private def calculate_gravity_scalar(obj, dir_vec) private def calculate_gravity_scalar(obj, dir_vec)
grav = GRAV_CONSTANT * (self.mass/(dir_vec.magnitude**2)) grav = GRAV_CONSTANT * (self.mass/(dir_vec.magnitude**2))
return grav return grav
@ -136,19 +140,29 @@ class Planet < PhysObj
if( !self.world.freeze ) then 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_vecs << grav_vec obj.accel_vecs[self.name] = grav_vec
obj.apply_accel_vecs
obj.parent_orbit = self
end end
end end
end end
private def debug_string # 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"
end #end
def render(x_offset=0, y_offset=0) def render(x_offset=0, y_offset=0)
super @radius*3, @radius*3, Gosu::Color.argb(0xff_ffffff) super @radius*3, @radius*3, Gosu::Color.argb(0xff_ffffff)
Gosu.draw_circle(self.pos[0] + x_offset, self.pos[1] + y_offset, @radius, @color, 0, @circle_thickness) Gosu.draw_circle(self.pos[0] + x_offset, self.pos[1] + y_offset, @radius, @color, 0, @circle_thickness)
end end
def width
return @radius
end
def height
return @radius
end
end end

Loading…
Cancel
Save