|
|
|
@ -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 |
|
|
|
|