render_path

pull/1/head
E. Almqvist 3 years ago
parent c07e1374e8
commit 46d1a880f1
  1. 11
      app.rb
  2. 14
      physobj.rb

@ -30,6 +30,7 @@ class Window < Gosu::Window
obj.render obj.render
obj.draw_vector(obj.vel, 10) obj.draw_vector(obj.vel, 10)
obj.draw_vector(obj.accel, 500, 0xff_aaffaa) obj.draw_vector(obj.accel, 500, 0xff_aaffaa)
obj.render_path
end end
@planets.each do |planet| @planets.each do |planet|
@ -45,9 +46,15 @@ planet = Planet.new("Earth", window, 0xff_aaffaa)
planet.pos = Vector[800, 450] planet.pos = Vector[800, 450]
cube = PhysCube.new("Cube", window, 8, 8) cube = PhysCube.new("Cube", window, 8, 8)
cube.vel = Vector[1, 0] cube.pos = Vector[800, 450 + 100]
planet.orbit([cube]) cube.vel = Vector[4, 0]
cube2 = PhysCube.new("Cube2", window, 8, 8)
cube2.pos = Vector[800, 450 + 150]
cube2.vel = Vector[1, 0]
planet.orbit([cube, cube2])
window.planets << planet window.planets << planet
window.physobjs << cube window.physobjs << cube
window.physobjs << cube2
window.show window.show

@ -3,7 +3,7 @@ require "matrix"
GRAV_CONSTANT = 1e+2 GRAV_CONSTANT = 1e+2
class PhysObj class PhysObj
attr_accessor :world, :pos, :vel, :accel, :x, :y attr_accessor :world, :saved_pos, :pos, :vel, :accel, :x, :y
attr_reader :name attr_reader :name
def initialize(name, world) def initialize(name, world)
@name = name @name = name
@ -12,6 +12,7 @@ class PhysObj
@vel = Vector.zero(2) @vel = Vector.zero(2)
@accel = Vector.zero(2) @accel = Vector.zero(2)
@x, @y = 0, 0 @x, @y = 0, 0
@saved_pos = []
end end
def tick def tick
@ -23,6 +24,13 @@ class PhysObj
@pos += @vel @pos += @vel
end end
@x, @y = @pos[0], @pos[1] @x, @y = @pos[0], @pos[1]
@saved_pos << @pos
end
def render_path
@saved_pos.each do |pos|
Gosu.draw_rect(pos[0], pos[1], 1, 1, Gosu::Color.argb(0xaa_cccccc))
end
end end
end end
@ -91,8 +99,8 @@ class Planet < PhysCube
end end
private def calculate_gravity_vector(obj) private def calculate_gravity_vector(obj)
dir_vec = self.pos - obj.pos dir_vec = self.pos - obj.pos + Vector[self.width/2, self.height/2]
return (GRAV_CONSTANT * self.gravity * dir_vec)/(dir_vec.magnitude**2) return (GRAV_CONSTANT * self.gravity * dir_vec)/(dir_vec.magnitude ** 2)
end end
def orbit(physobjs) def orbit(physobjs)

Loading…
Cancel
Save