pull/1/head
E. Almqvist 3 years ago
parent cdbd19a0d7
commit 45ede97828
  1. 13
      app.rb
  2. 17
      physobj.rb

@ -15,7 +15,7 @@ class Window < Gosu::Window
@planets = planets
@font = Gosu::Font.new(self, Gosu::default_font_name, 12)
@font2 = Gosu::Font.new(self, Gosu::default_font_name, 32)
@font2 = Gosu::Font.new(self, Gosu::default_font_name, 18)
@freeze = false
end
@ -44,9 +44,8 @@ class Window < Gosu::Window
end
def draw
if( @freeze ) then
@font2.draw("FROZEN", 0, 0, 1, 1.0, 1.0, Gosu::Color::WHITE)
end
status_text = @freeze ? "FROZEN (Escape to unfreeze)" : "(Escape to freeze)"
@font2.draw(status_text, 0, 0, 1, 1.0, 1.0, Gosu::Color::WHITE)
@physobjs.each do |obj|
obj.render
@ -66,7 +65,7 @@ end
window = Window.new("Physics!", 1600, 900)
planet = Planet.new("Earth", window, 0xff_aaffaa)
planet = Planet.new("Sol", window, 0xff_ffffaa, 1e2, 20, 120)
planet.pos = Vector[800, 450]
cube = PhysCube.new("Cube", window, 8, 8)
@ -74,8 +73,8 @@ cube.pos = Vector[800, 450 + 200]
cube.vel = Vector[2.5, 0]
cube2 = PhysCube.new("Cube2", window, 8, 8)
cube2.pos = Vector[800, 450 + 400]
cube2.vel = Vector[1.25, 0]
cube2.pos = Vector[800, 450 - 200]
cube2.vel = Vector[-2.5, 0]
planet.orbit([cube, cube2])
window.planets << planet

@ -78,11 +78,16 @@ class PhysCube < PhysObj
end
class Planet < PhysCube
attr_reader :mass
def initialize(name, world, color, mass=1e+2)
super name, world, 40, 40, color
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
private def calculate_gravity_scalar(obj, dir_vec)
@ -91,7 +96,7 @@ class Planet < PhysCube
end
private def calculate_gravity_vector(obj)
dir_vec = self.pos - obj.pos + Vector[self.width/2, self.height/2]
dir_vec = self.pos - obj.pos + Vector[@radius/2, @radius/2]
return (dir_vec/dir_vec.magnitude) * calculate_gravity_scalar(obj, dir_vec)
end
@ -103,6 +108,6 @@ class Planet < PhysCube
end
def render
Gosu.draw_circle(self.pos[0], self.pos[1], 20, @color)
Gosu.draw_circle(self.pos[0], self.pos[1], @radius, @color, 0, @circle_thickness)
end
end

Loading…
Cancel
Save