samples/04_physics_and_collisions/08_bouncing_on_collision/app/ball.rb
def update args
      @center.x += @velocity.x
      @center.y += @velocity.y
      @velocity.y += GRAVITY

      alpha = 0.2
      if @center.y-@radius <= 0
        @velocity.y  = (@velocity.y.abs*0.7).abs
        @velocity.x  = (@velocity.x.abs*0.9).abs * ((@velocity.x < 0) ? -1 : 1)

        if @velocity.y.abs() < alpha
          @velocity.y=0
        end
        if @velocity.x.abs() < alpha
          @velocity.x=0
        end
      end

      if @center.x > args.grid.right+@radius*2
        @center.x = 0-@radius
      elsif @center.x< 0-@radius*2
        @center.x = args.grid.right + @radius
      end
    end
#Move the ball according to its velocity
Move the ball according to its velocity
Move the ball according to its velocity
def update args
      @center.x += @velocity.x
      @center.y += @velocity.y
      @velocity.y += GRAVITY
      alpha = 0.2
      if @center.y-@radius <= 0
        @velocity.y  = (@velocity.y.abs*0.7).abs
        @velocity.x  = (@velocity.x.abs*0.9).abs * ((@velocity.x < 0) ? -1 : 1)
        if @velocity.y.abs() < alpha
          @velocity.y=0
        end
        if @velocity.x.abs() < alpha
          @velocity.x=0
        end
      end
      if @center.x > args.grid.right+@radius*2
        @center.x = 0-@radius
      elsif @center.x< 0-@radius*2
        @center.x = args.grid.right + @radius
      end
    end
def update args @center . x += @velocity . x @center . y += @velocity . y @velocity . y += GRAVITY alpha = 0.2 if @center . y - @radius <= 0 @velocity . y = ( @velocity . y . abs * 0.7 ) . abs @velocity . x = ( @velocity . x . abs * 0.9 ) . abs * ( ( @velocity . x < 0 ) ? - 1 : 1 ) if @velocity . y . abs ( ) < alpha @velocity . y = 0 end if @velocity . x . abs ( ) < alpha @velocity . x = 0 end end if @center . x > args . grid . right + @radius * 2 @center . x = 0 - @radius elsif @center . x < 0 - @radius * 2 @center . x = args . grid . right + @radius end end
Move the ball according to its velocity
Move the ball according to its velocity
args