Skip to content

Commit

Permalink
Improve Bounce Example to include more realistic physics
Browse files Browse the repository at this point in the history
  • Loading branch information
larskanis committed Feb 16, 2012
1 parent 08a9135 commit 94b0663
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions examples/bounce.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(r)
@x = @center.x - @radius @x = @center.x - @radius
@y = @center.y - @radius @y = @center.y - @radius
@color = FXRGB(255, 0, 0) # red @color = FXRGB(255, 0, 0) # red
@dir = FXPoint.new(-1, 0) @dir = FXPoint.new(-1, -1)
setWorldSize(1000, 1000) setWorldSize(1000, 1000)
end end


Expand All @@ -38,12 +38,20 @@ def draw(dc)
dc.fillArc(x, y, w, h, 64*270, 64*360) dc.fillArc(x, y, w, h, 64*270, 64*360)
end end


def bounce def bounce_x
@dir = -@dir @dir.x=-@dir.x
end end


def collision? def bounce_y
(x < 0) || (x+w > worldWidth) || (y < 0) || (y+h > worldHeight) @dir.y=-@dir.y
end

def collision_y?
(y<0 && dir.y<0) || (y+h>worldHeight && dir.y>0)
end

def collision_x?
(x<0 && dir.x<0) || (x+w>worldWidth && dir.x>0)
end end


def setWorldSize(ww, wh) def setWorldSize(ww, wh)
Expand All @@ -58,8 +66,12 @@ def move(units)
center.y += dy center.y += dy
@x += dx @x += dx
@y += dy @y += dy
if collision? if collision_x?
bounce bounce_x
move(units)
end
if collision_y?
bounce_y
move(units) move(units)
end end
end end
Expand Down

0 comments on commit 94b0663

Please sign in to comment.