samples/04_physics_and_collisions/08_bouncing_on_collision/app/block.rb
#Determine if the ball and block are touching
Determine if the ball and block are touching
Determine if the ball and block are touching
def collision? args
    min_area = ((@block_size * 3) * args.state.ball.radius) / 2
    ax = @x1
    ay = @y1
    bx = @x2
    by = @y2
    cx = args.state.ball.center.x
    cy = args.state.ball.center.y
    current_area = (ax*(by-cy)+bx*(cy-ay)+cx*(ay-by))/2
    collision = false
    if @rotation >= 0
      if (current_area < min_area &&
        current_area > 0 &&
        args.state.ball.center.y > @y1 &&
        args.state.ball.center.x < @x2)
        collision = true
      end
    else
      if (current_area < min_area &&
        current_area > 0 &&
        args.state.ball.center.y > @y2 &&
        args.state.ball.center.x > @x1)
      collision = true
      end
    end
    return collision
  end
def collision? args min_area = ( ( @block_size * 3 ) * args . state . ball . radius ) / 2 ax = @x1 ay = @y1 bx = @x2 by = @y2 cx = args . state . ball . center . x cy = args . state . ball . center . y current_area = ( ax * ( by - cy ) + bx * ( cy - ay ) + cx * ( ay - by ) ) / 2 collision = false if @rotation >= 0 if ( current_area < min_area && current_area > 0 && args . state . ball . center . y > @y1 && args . state . ball . center . x < @x2 ) collision = true end else if ( current_area < min_area && current_area > 0 && args . state . ball . center . y > @y2 && args . state . ball . center . x > @x1 ) collision = true end end return collision end
Determine if the ball and block are touching
Determine if the ball and block are touching
#The minimum area enclosed by the center of the ball and the 2 corners of the block #If the area ever drops below this value, we know there is a collision #https://www.mathopenref.com/coordtrianglearea.html
args