pull/1/head
E. Almqvist 3 years ago
commit 9e96d4735c
  1. 3
      Gemfile
  2. 13
      Gemfile.lock
  3. 40
      app.rb
  4. 62
      physobj.rb

@ -0,0 +1,3 @@
source "https://rubygems.org"
gem "gosu"

@ -0,0 +1,13 @@
GEM
remote: https://rubygems.org/
specs:
gosu (1.2.0)
PLATFORMS
x86_64-linux
DEPENDENCIES
gosu
BUNDLED WITH
2.2.26

@ -0,0 +1,40 @@
#!/usr/bin/ruby -w
require "gosu"
load "physobj.rb"
class Window < Gosu::Window
attr_accessor :caption, :physobjs
attr_reader :width, :height
def initialize(title, width, height, physobjs = [])
super width, height
@width, @height = width, height
self.caption = "#{title}| #{width}x#{height}"
@physobjs = physobjs
end
def update
@physobjs.each do |obj|
obj.physics
end
end
def draw
@physobjs.each do |obj|
obj.render
end
end
end
window = Window.new("Physics!", 1600, 900)
planet = PhysCube.new(window, 16, 16, 0xff_aaffaa)
cube = PhysCube.new(window, 8, 8)
cube.accel = Vector[0, 0.1]
cube.vel = Vector[0.01, 0]
window.physobjs << cube
window.show

@ -0,0 +1,62 @@
require "matrix"
class PhysObj
attr_accessor :world, :pos, :vel, :accel, :x, :y
def initialize(world)
@world = world
@pos = Vector.zero(2)
@vel = Vector.zero(2)
@accel = Vector.zero(2)
@x, @y = 0, 0
end
def tick
if( @accel.magnitude != 0 ) then
@vel += @accel
end
if( @vel.magnitude != 0 ) then
@pos += @vel
end
@x, @y = @pos[0], @pos[1]
end
end
class PhysCube < PhysObj
attr_reader :width, :height
def initialize(world, width, height, color=0xff_ffffff, ela=-0.8)
super world
@width, @height = width, height
@color = Gosu::Color.argb(color)
@ela = ela
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)]}")
end
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(@accel, 10, 0xff_00ff00)
end
def physics
self.tick
x, y = self.pos[0], self.pos[1]
x_max = world.width - self.width
y_max = world.height - self.height
if( x > x_max ) then self.pos[0] = x_max end
if( y > y_max ) then self.pos[1] = y_max end
end
end
Loading…
Cancel
Save