Call GetContacts after a step. Positive point separation means there’s still a gap.
dotnet add package RecursiveAI.OnePhysicsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OnePhysics; using RecursiveAI.OnePhysics.Physics2D; var world = new PhysicsWorld2D(); world.AddBox(new(4, 1), at: new(0, -0.5), type: BodyType.Static); world.AddBox(new(1, 4), at: new(1.01, 0.5), type: BodyType.Static); var ball = world.AddCircle(0.5f, at: new(0, 0.49)); world.Step(1f / 60); foreach (var contact in world.GetContacts(ball)) { for (int i = 0; i < contact.PointCount; i++) { Console.WriteLine(contact.Separation(i) <= 0 ? "Touching" : "Speculative"); } }
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Touching Speculative
Speculative means the engine predicts a possible contact, but the positive separation shows the bodies aren’t touching at that point yet.
The circle starts slightly inside the floor but 0.01 m away from the wall. The contact list has a point for each surface even though only one point has nonpositive separation.
GetContacts throws InvalidHandleException if the body has been removed or belongs to another world.
Variations
Read the position of each touching point. Replace the loop in the example with this one. Point(i) is a world-space point; use the same index as for Separation(i).
foreach (var contact in world.GetContacts(ball)) for (int i = 0; i < contact.PointCount; i++) if (contact.Separation(i) <= 0) Console.WriteLine(contact.Point(i));
Related recipes
- Use collision events and sensors to react to a contact beginning or a trigger being entered.
- Sweep a rotating 2D body for collisions to check a proposed move before making it.
The short answer
After world.Step, iterate world.GetContacts(body), then read Separation(i) for each point on each contact. A point with positive separation still has a gap and is speculative; zero or negative separation means it touches or penetrates in the reported contact geometry. A contact can have more than one point, so test all its points if you want to know whether the pair is touching at any of them.
How do you get the position of a 2D collision contact?
Call Point(i) on the contact view. The point is expressed in world coordinates, not as an offset from the circle. Read or copy the fields you need before stepping or changing the world: the view reads the engine’s contact data, rather than holding a copy. The contact also gives you the two body handles, so you can distinguish a floor contact from a wall contact instead of relying on which one appears first.
How do you tell whether a 2D body is touching the ground?
Don’t use the mere presence of a contact as your grounded test. A speculative floor contact can still have positive separation. If you have a known floor body, check that it’s one of the contact’s bodies and that at least one point has nonpositive separation. If you’re deciding whether any surface counts as ground, also check the normal: a touching wall is not ground. The contact normal points from body A toward body B, so check which side your character is on before you use its direction.
Do you need collision events to read current contacts?
No. Use GetContacts when you need the current list, even if a body has been resting on the same surface for several steps. Use events when you need a transition, such as playing a sound on first contact. The list is read-only, excludes sensors and disabled zero-point contacts, and is enumerated without allocating a list. For sensor overlaps, use trigger events instead.