---
title: "Sweep a rotating 2D body for collisions in C#"
url: https://recursiveai.org/onephysics/cookbook/sweep-a-rotating-2d-body-for-collisions/
description: "Use OnePhysics to check whether a 2D rectangle hits a wall while rotating in C#. Read the first hit fraction without moving the body."
version: "1.3.0"
---

Use `Body.Sweep` to check whether a rectangle hits a wall during a quarter turn, without moving it.

```shell
dotnet add package RecursiveAI.OnePhysics
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics2D;

var world = new PhysicsWorld2D();
var bar = world.AddBox(new(2, 0.2f));
world.AddBox(new(3, 0.2f), at: new(0, 0.85),
    type: BodyType.Static);
if (bar.Sweep(default, MathF.PI / 2, out _))
    Console.WriteLine("Wall hit");
```

[The example](https://recursiveai.org/onephysics/cookbook/sweep-a-rotating-2d-body-for-collisions/sweep-a-rotating-2d-body-for-collisions.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

```text
Wall hit
```

## The full program

The bar is 2 m long and 0.2 m thick. The wall is horizontal, centered at `(0, 0.85)`. The bar starts clear of the wall but swings into it on a 90° turn about its center of mass.

```csharp
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics2D;

var world = new PhysicsWorld2D();
var bar = world.AddBox(new(2, 0.2f));
var wall = world.AddBox(new(3, 0.2f),
    at: new(0, 0.85), type: BodyType.Static);

float turn = MathF.PI / 2;
if (bar.Sweep(default, turn, out var hit))
{
    Console.WriteLine(hit.Body == wall
        ? "Wall hit" : "Other hit");
    Console.WriteLine(FormattableString.Invariant(
        $"Fraction: {hit.Fraction:F3}"));
    Console.WriteLine($"Distance: {hit.Distance}");
}
else
{
    Console.WriteLine("Clear");
}
```

```text
Wall hit
Fraction: 0.473
Distance: 0
```

`Distance` is 0 because the bar’s center doesn’t move during a pure rotation.

If the sweep can’t find a hit or confirm the path is clear within its calculation limit, it throws `QueryConvergenceException`; treat the move as blocked and leave the body where it is.

## Variations

**Slide and rotate together.** Change the sweep call to move the center half a meter down while turning. With the same bar and wall, this prints `Clear`:

```csharp
if (bar.Sweep(new(0, -0.5f), turn, out _))
    Console.WriteLine("Blocked");
else
    Console.WriteLine("Clear");
```

## Related recipes

- [Batch 2D raycasts](https://recursiveai.org/onephysics/cookbook/batch-2d-raycasts/) for queries along lines.
- [Read current 2D body contacts](https://recursiveai.org/onephysics/cookbook/read-current-2d-body-contacts/) to inspect contacts after a step.

## The short answer

Call the angular overload of `Body.Sweep` with zero translation and the signed turn in radians. OnePhysics sweeps the full body shape from its current pose and reports the first hit without moving the body or waking it. For the quarter turn above, the bar hits the wall at about 47% of the proposed motion.

## Can a 90° rotation cause a collision even if the center stays still?

Yes. The corners of a rectangle trace arcs around the center of mass. A ray from that center doesn’t follow those corners. An overlap check at the end of a turn only tells you about the final pose: for instance, a centered rectangle can finish a half turn occupying the same space where it began, after hitting something along the way. An angular sweep tests the intervening motion rather than only the end pose.

## What does the time of collision mean in a shape sweep?

`Fraction` runs from 0 to 1 across the proposed motion. For pure rotation, multiply it by the requested angle to estimate the angle traveled when the sweep reports a hit. `Distance` measures translation of the center, not the length of the arc a corner travels, so it is zero here. If there’s an overlap at the start, the hit fraction is zero; the sweep doesn’t push bodies apart. The default tolerance is 0.0001 m plus the solver’s relative margin, so treat the hit as a result within tolerance, not an exact pose to move to.

## What is continuous collision detection, and when should you use a sweep?

Continuous collision detection checks the path between poses instead of waiting for shapes to overlap at a sampled pose. Use this particular query when you have a proposed move and want to decide whether to make it, such as turning a bar near a wall. It doesn’t advance the simulation or predict where the obstacles will move during a later step. See [queries and layers](https://recursiveai.org/onephysics/docs/queries-and-layers/) to choose which targets your query should consider.
