On this page:
13.1 Creating Rectangles
make-rect
make-frect
13.2 Rectangle Accessors
rect-x
rect-y
rect-w
rect-h
frect-x
frect-y
frect-w
frect-h
rect->values
frect->values
13.3 Collision Detection
rects-intersect?
rect-intersection
frects-intersect?
frect-intersection
13.4 Rectangle Utilities
rect-union
frect-union
rect-enclosing-points
frect-enclosing-points
13.5 Line Intersection
rect-line-intersection
frect-line-intersection
9.0.0.11

13 Rectangle Collision Detection🔗ℹ

This section covers rectangle creation and collision detection using both integer-based (SDL_Rect) and floating-point (SDL_FRect) rectangles.

13.1 Creating Rectangles🔗ℹ

procedure

(make-rect x y w h)  SDL_Rect?

  x : real?
  y : real?
  w : real?
  h : real?
Creates an integer rectangle.

Values are truncated to integers.

(define player-hitbox (make-rect 100 200 32 32))

procedure

(make-frect x y w h)  SDL_FRect?

  x : real?
  y : real?
  w : real?
  h : real?
Creates a floating-point rectangle.

(define player-hitbox (make-frect 100.5 200.0 32.0 32.0))

13.2 Rectangle Accessors🔗ℹ

procedure

(rect-x r)  exact-integer?

  r : SDL_Rect?
Returns the x coordinate.

procedure

(rect-y r)  exact-integer?

  r : SDL_Rect?
Returns the y coordinate.

procedure

(rect-w r)  exact-integer?

  r : SDL_Rect?
Returns the width.

procedure

(rect-h r)  exact-integer?

  r : SDL_Rect?
Returns the height.

procedure

(frect-x r)  real?

  r : SDL_FRect?
Returns the x coordinate.

procedure

(frect-y r)  real?

  r : SDL_FRect?
Returns the y coordinate.

procedure

(frect-w r)  real?

  r : SDL_FRect?
Returns the width.

procedure

(frect-h r)  real?

  r : SDL_FRect?
Returns the height.

Destructures a rectangle into multiple values.

(define-values (x y w h) (rect->values player-hitbox))

procedure

(frect->values r)  
real? real? real? real?
  r : SDL_FRect?
Destructures a floating-point rectangle into multiple values.

13.3 Collision Detection🔗ℹ

procedure

(rects-intersect? a b)  boolean?

  a : SDL_Rect?
  b : SDL_Rect?
Returns #t if two rectangles overlap.

(when (rects-intersect? player-hitbox enemy-hitbox)
  (handle-collision!))

procedure

(rect-intersection a b)  (or/c SDL_Rect? #f)

  a : SDL_Rect?
  b : SDL_Rect?
Returns the overlapping region of two rectangles, or #f if they don’t intersect.

(define overlap (rect-intersection a b))
(when overlap
  (printf "Overlap area: ~a x ~a~n"
          (rect-w overlap) (rect-h overlap)))

procedure

(frects-intersect? a b)  boolean?

  a : SDL_FRect?
  b : SDL_FRect?
Returns #t if two floating-point rectangles overlap.

procedure

(frect-intersection a b)  (or/c SDL_FRect? #f)

  a : SDL_FRect?
  b : SDL_FRect?
Returns the overlapping region of two floating-point rectangles, or #f if they don’t intersect.

13.4 Rectangle Utilities🔗ℹ

procedure

(rect-union a b)  SDL_Rect?

  a : SDL_Rect?
  b : SDL_Rect?
Returns the smallest rectangle that contains both input rectangles.

(define bounds (rect-union a b))

procedure

(frect-union a b)  SDL_FRect?

  a : SDL_FRect?
  b : SDL_FRect?
Returns the smallest floating-point rectangle that contains both input rectangles.

procedure

(rect-enclosing-points points [clip])  (or/c SDL_Rect? #f)

  points : (listof point?)
  clip : (or/c SDL_Rect? #f) = #f
Returns the smallest rectangle that encloses all the given points.

Points can be:
  • SDL_Point structs

  • Lists of two numbers: (list x y)

  • Vectors of two numbers: #(x y)

If clip is provided, points are first clipped to that rectangle. Returns #f if no points are enclosed (or the list is empty).

(define bounds (rect-enclosing-points
                 (list '(10 20) '(50 60) '(30 40))))

procedure

(frect-enclosing-points points [clip])  (or/c SDL_FRect? #f)

  points : (listof point?)
  clip : (or/c SDL_FRect? #f) = #f
Returns the smallest floating-point rectangle that encloses all the given points.

13.5 Line Intersection🔗ℹ

procedure

(rect-line-intersection rect x1 y1 x2 y2)

  
(or/c (values exact-integer? exact-integer?
              exact-integer? exact-integer?) #f)
  rect : SDL_Rect?
  x1 : real?
  y1 : real?
  x2 : real?
  y2 : real?
Clips a line segment to a rectangle.

Returns (values x1-clipped y1-clipped x2-clipped y2-clipped) with the clipped line endpoints, or #f if the line doesn’t intersect the rectangle.

(define-values (cx1 cy1 cx2 cy2)
  (or (rect-line-intersection viewport 0 0 800 600)
      (values 0 0 0 0)))

procedure

(frect-line-intersection rect x1 y1 x2 y2)

  (or/c (values real? real? real? real?) #f)
  rect : SDL_FRect?
  x1 : real?
  y1 : real?
  x2 : real?
  y2 : real?
Clips a line segment to a floating-point rectangle.

Returns (values x1-clipped y1-clipped x2-clipped y2-clipped) with the clipped line endpoints, or #f if the line doesn’t intersect the rectangle.