Broken stuff

pull/1/head
E. Almqvist 3 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)
super id
if( @controller != nil ) then
if( @controller != nil && @controller.class == Player ) then
@controller.button_up(id)
end
end
@ -46,7 +46,7 @@ class Window < Gosu::Window
@freeze = !@freeze
end
if( @controller != nil ) then
if( @controller != nil && @controller.class == Player ) then
@controller.button_down(id)
end
end
@ -54,7 +54,7 @@ class Window < Gosu::Window
# def button_up?(id)
# super id
#
# if( @controller != nil ) then
# if( @controller != nil && @controller.class == Player ) then
# @controller.button_up?(id)
# end
# end
@ -62,7 +62,7 @@ class Window < Gosu::Window
# def button_down?(id)
# super id
#
# if( @controller != nil ) then
# if( @controller != nil && @controller.class == Player ) then
# @controller.button_down?(id)
# end
# end
@ -73,7 +73,11 @@ class Window < Gosu::Window
end
@planets.each do |planet|
planet.orbit(@physobjs)
orbiters = []
orbiters += @physobjs
orbiters += @planets
orbiters.delete(planet)
planet.orbit(planets)
end
end
@ -82,11 +86,12 @@ class Window < Gosu::Window
end
def draw
if( @controller != nil ) then
if( @controller != nil && @controller.class == Player ) 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]
# camx, camy = 0, 0
@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.pos = Vector[800, 450 + 200]
cube.vel = Vector[2.5, 0]
window.controller = cube
cube2 = PhysCube.new("Beta", window, 8, 8)
cube2.pos = Vector[800, 450 + 300]
cube2.vel = Vector[-1.24, 0]
cube2.show_info = true
sol_orbiters = [cube, cube2]
sol = Planet.new("Sol", window, 0xff_ffffaa, 1e2, 5, 12)
sol = Planet.new("Sol", window, 0xff_ffffaa, 1e2, 15, 1)
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.show_info = true
sol_orbiters = [cube, cube2, planet]
sol.orbit(sol_orbiters)
window.planets << sol
window.planets << planet
window.physobjs << cube
window.physobjs << cube2
window.physobjs << planet
window.controller = planet
window.show

@ -55,6 +55,6 @@ class Player < PhysCube
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"
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

@ -15,21 +15,21 @@ class PhysObj
@show_info = false
@angle = 0
@parent_orbit = ""
@accel_vecs = []
@accel_vecs = Hash.new(Vector[0, 0])
end
private def apply_accel_vecs
@accel_vecs.each do |vec|
@accel += vec
def apply_accel_vecs
summed_vec = Vector.zero(2)
@accel_vecs.each do |planet, vec|
summed_vec += vec
end
@accel_vecs = []
@accel = summed_vec
end
def tick
@x, @y = @pos[0], @pos[1]
@angle %= 360
self.apply_accel_vecs
if( !world.freeze ) then
if( @accel.magnitude != 0 ) then
@vel += @accel
@ -52,7 +52,7 @@ class PhysObj
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"
end
@ -122,6 +122,10 @@ class Planet < PhysObj
@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
@ -136,19 +140,29 @@ class Planet < PhysObj
if( !self.world.freeze ) then
physobjs.each do |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
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"
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 @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)
end
def width
return @radius
end
def height
return @radius
end
end

Loading…
Cancel
Save