pull/1/head
E. Almqvist 3 years ago
parent 2efbc45322
commit 0240e2f63d
  1. 10
      app.rb
  2. 43
      physobj.rb

@ -23,6 +23,8 @@ class Window < Gosu::Window
def draw
@physobjs.each do |obj|
obj.render
obj.draw_vector(obj.vel, 10)
obj.draw_vector(obj.accel, 500, 0xff_aaffaa)
end
end
end
@ -30,12 +32,12 @@ end
window = Window.new("Physics!", 1600, 900)
planet = PhysCube.new(window, 16, 16, 0xff_aaffaa)
planet = PhysCube.new("Earth", window, 16, 16, 0xff_aaffaa)
planet.pos = Vector[800, 450]
cube = PhysCube.new(window, 8, 8)
cube.accel = planet.pos * 0.001
cube.vel = Vector[2, 0]
cube = PhysCube.new("Cube", window, 8, 8)
cube.accel = Vector[0, 0.1]
cube.vel = Vector[10, 0]
window.physobjs << planet
window.physobjs << cube

@ -2,7 +2,9 @@ require "matrix"
class PhysObj
attr_accessor :world, :pos, :vel, :accel, :x, :y
def initialize(world)
attr_reader :name
def initialize(name, world)
@name = name
@world = world
@pos = Vector.zero(2)
@vel = Vector.zero(2)
@ -26,8 +28,8 @@ end
class PhysCube < PhysObj
attr_reader :width, :height
def initialize(world, width, height, color=0xff_ffffff, ela=-0.8)
super world
def initialize(name, world, width, height, color=0xff_ffffff, ela=-0.8)
super name, world
@width, @height = width, height
@color = Gosu::Color.argb(color)
@ -37,7 +39,6 @@ class PhysCube < PhysObj
def render
x, y = self.pos[0], self.pos[1]
Gosu.draw_quad(x, y, @color, x + self.width, y, @color, x, y + self.height, @color, x + self.width, y + self.height, @color)
# self.draw_vector(@vel, 2)
end
def physics
@ -47,21 +48,37 @@ class PhysCube < PhysObj
x_max = world.width - self.width
y_max = world.height - self.height
if( x > x_max ) then self.pos[0] = x_max end
if( x > x_max ) then
self.pos[0] = x_max
self.vel[0] = 0
end
if( y > y_max ) then
self.pos[1] = y_max
self.vel[1] = -2
self.vel[0] = 1
self.vel[1] = 0
end
end
def draw_vector(vec, scale=0.1, color=0xaf_ffaaaa)
clr = Gosu::Color.argb(color)
dx, dy = vec[0], vec[1]
xx, yy = @x + @width/2, @y + @height/2
Gosu.draw_line(xx, yy, clr, (xx + dx).round(1) * scale, (yy + dy).round(1) * scale, clr)
puts("#{dx} #{dy} #{[xx - (xx + dx), yy - (yy + dy)]}")
def draw_vector(vec, scale=2, color=0xaf_ffaaaa)
if( vec.magnitude != 0 ) then
clr = Gosu::Color.argb(color)
scaled_vec = vec*scale
pos1 = self.pos + Vector[self.width/2, self.height/2]
pos2 = pos1 + scaled_vec
x1 = pos1[0]
y1 = pos1[1]
x2 = pos2[0]
y2 = pos2[1]
# Gosu.draw_line(x1, y1, clr, x2*scale, y2*scale, clr)
Gosu.draw_line(x1, y1, clr, x2, y2, clr)
puts("#{self.name}: pos1:#{pos1} pos2:#{pos2}")
end
end
end

Loading…
Cancel
Save