Skip to content

collide

collide

Lightweight collision tests for games that do not want full Box2D.

This is plain geometry on rectangles, circles, polygons and line segments, with no SDL or renderer dependency, so it tests headlessly and costs nothing to call. Rectangles are given the same way rectangle draws them, as a top-left corner (x, y) with a width and height, and circles as a center and a radius, so you pass collision the same numbers you pass drawing.

There are three kinds of test here. The pointIn* family asks whether a point sits inside a shape, which is what you use for a mouse hover or a hit check. The *Overlap family asks whether two shapes touch, returning a bool. The resolve* pair goes one step further and returns the smallest move that pushes one shape out of another, so a body can slide along a wall instead of stopping dead or passing through.

When you need real dynamics, stacking, joints and forces, reach for the physics module instead. This is the cheap option for the common cases.

It is an opt-in module, imported on its own with import nim2d/collide. The core engine does not pull it in.

pointInRect

func pointInRect(p: Vec2; x, y, w, h: float): bool

Whether point p is inside the rectangle with top-left (x, y) and the given

width and height. The edges count as inside.

Parameters

  • p (Vec2)
  • x (float)
  • y (float)
  • w (float)
  • h (float)

Returns

bool

pointInCircle

func pointInCircle(p: Vec2; cx, cy, r: float): bool

Whether point p is inside the circle centered at (cx, cy) with radius r.

Parameters

  • p (Vec2)
  • cx (float)
  • cy (float)
  • r (float)

Returns

bool

pointInTriangle

func pointInTriangle(p, a, b, c: Vec2): bool

Whether point p is inside the triangle a, b, c. The winding does not

matter, and points on an edge count as inside.

Parameters

  • p (Vec2)
  • a (Vec2)
  • b (Vec2)
  • c (Vec2)

Returns

bool

pointInPolygon

func pointInPolygon(p: Vec2; poly: openArray[Vec2]): bool

Whether point p is inside the polygon outlined by poly, by counting how

many times a ray crosses the edges. Works for convex and concave outlines without holes. Fewer than three points is never inside.

Parameters

  • p (Vec2)
  • poly (openArray[Vec2])

Returns

bool

rectsOverlap

func rectsOverlap(ax, ay, aw, ah, bx, by, bw, bh: float): bool

Whether two rectangles overlap. Each is a top-left corner with a width and

height, the same as rectangle takes. Touching edges count as overlapping.

Parameters

  • ax (float)
  • ay (float)
  • aw (float)
  • ah (float)
  • bx (float)
  • by (float)
  • bw (float)
  • bh (float)

Returns

bool

circlesOverlap

func circlesOverlap(ax, ay, ar, bx, by, br: float): bool

Whether two circles overlap, each a center and a radius.

Parameters

  • ax (float)
  • ay (float)
  • ar (float)
  • bx (float)
  • by (float)
  • br (float)

Returns

bool

circleRectOverlap

func circleRectOverlap(cx, cy, r, rx, ry, rw, rh: float): bool

Whether a circle (center, radius) overlaps a rectangle (top-left, size).

The test finds the rectangle point nearest the circle center and checks whether it falls within the radius.

Parameters

  • cx (float)
  • cy (float)
  • r (float)
  • rx (float)
  • ry (float)
  • rw (float)
  • rh (float)

Returns

bool

segmentsIntersect

func segmentsIntersect(a1, a2, b1, b2: Vec2): bool

Whether the line segment a1->a2 crosses the segment b1->b2, touching ends

included. Useful for a line-of-sight check or a laser against walls.

Parameters

  • a1 (Vec2)
  • a2 (Vec2)
  • b1 (Vec2)
  • b2 (Vec2)

Returns

bool

resolveRect

func resolveRect(ax, ay, aw, ah, bx, by, bw, bh: float): Vec2

The smallest move to apply to rectangle A so it no longer overlaps

rectangle B, pushing along whichever axis it is least buried in. Returns (0, 0) when they are already clear, so you can add it to A's position unconditionally.

Parameters

  • ax (float)
  • ay (float)
  • aw (float)
  • ah (float)
  • bx (float)
  • by (float)
  • bw (float)
  • bh (float)

Returns

Vec2

resolveCircleRect

func resolveCircleRect(cx, cy, r, rx, ry, rw, rh: float): Vec2

The smallest move to apply to the circle so it no longer overlaps the

rectangle. It pushes straight away from the nearest edge, which is what lets a round body slide along a wall. Returns (0, 0) when they are clear.

Parameters

  • cx (float)
  • cy (float)
  • r (float)
  • rx (float)
  • ry (float)
  • rw (float)
  • rh (float)

Returns

Vec2

Generated with mkdocstrings-nim