Hohmann Miner, a sandbox physics game where you mine stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hohmann-miner/physobj.rb

88 lines
2.1 KiB

3 years ago
GRAV_CONSTANT = 1e+1
3 years ago
MAX_PATH_TRACK_POINT = 1000
3 years ago
3 years ago
class PhysObj
attr_accessor :world, :saved_pos, :pos, :vel, :accel, :x, :y, :show_info, :angle, :parent_orbit, :accel_vecs
3 years ago
attr_reader :name
def initialize(name, world)
@name = name
3 years ago
@world = world
@pos = Vector.zero(2)
@vel = Vector.zero(2)
@accel = Vector.zero(2)
@x, @y = 0, 0
3 years ago
@saved_pos = []
3 years ago
@show_info = false
@angle = 0
3 years ago
@parent_orbit = ""
3 years ago
@accel_vecs = Hash.new(Vector[0, 0])
end
3 years ago
def apply_accel_vecs
summed_vec = Vector.zero(2)
@accel_vecs.each do |planet, vec|
summed_vec += vec
end
3 years ago
@accel = summed_vec
3 years ago
end
def tick
3 years ago
@x, @y = @pos[0], @pos[1]
@angle %= 360
3 years ago
if( @accel.magnitude != 0 ) then
@vel += @accel
end
3 years ago
3 years ago
if( @vel.magnitude != 0 ) then
@pos += @vel
end
@saved_pos << @pos
3 years ago
3 years ago
if(@saved_pos.length > MAX_PATH_TRACK_POINT) then
@saved_pos = @saved_pos[1..-1]
3 years ago
end
3 years ago
end
3 years ago
def render_path(x_offset=0, y_offset=0)
3 years ago
@saved_pos.each do |pos|
3 years ago
Gosu.draw_rect(pos[0] + x_offset, pos[1] + y_offset, 1, 1, Gosu::Color.argb(0xaa_ccccff))
3 years ago
end
3 years ago
end
3 years ago
def debug_string
3 years ago
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"
3 years ago
end
3 years ago
def render(x_offset=0, y_offset=0, color=Gosu::Color.argb(0xaa_2222ff))
if( @show_info ) then
3 years ago
self.world.fonts[:normal].draw_text(self.debug_string, self.pos[0] + x_offset, self.pos[1] + y_offset, 1, 1.0, 1.0, color)
3 years ago
end
3 years ago
end
3 years ago
3 years ago
def draw_vector(vec, scale=2, color=0xaf_ffaaaa, x_offset=0, y_offset=0)
3 years ago
if( vec.magnitude != 0 ) then
clr = Gosu::Color.argb(color)
scaled_vec = vec*scale
3 years ago
pos1 = Vector[x_offset, y_offset] + self.pos + Vector[self.width/2, self.height/2]
3 years ago
pos2 = pos1 + scaled_vec
x1 = pos1[0]
y1 = pos1[1]
x2 = pos2[0]
y2 = pos2[1]
Gosu.draw_line(x1, y1, clr, x2, y2, clr)
end
3 years ago
end
3 years ago
def draw_direction(x_offset=0, y_offset=0)
3 years ago
rads = (self.angle * Math::PI) / 180
dir_vec = Vector[Math::cos(rads), Math::sin(rads)]
3 years ago
self.draw_vector(dir_vec, 25, 0xaf_aaaaff, x_offset, y_offset)
3 years ago
end
end