Recursive AI

Make a one-way 2D platform in C#

OnePhysics is a free, open-source physics library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

A one-way platform lets a game character jump through from below while keeping its top solid.

Terminal
dotnet add package RecursiveAI.OnePhysics

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OnePhysics.Physics2D;
using RecursiveAI.OnePhysics;
var world = new PhysicsWorld2D(
    new() { Gravity = default });
world.AddPlatformEffector(
    new(0, 1), new(-5, -1), new(5, 1));
world.AddBox(new(10, 0.2f),
    type: BodyType.Static);
var ball = world.AddCircle(0.2f, at: new(0, -0.5));
ball.LinearVelocity = new(0, 2);
for (int i = 0; i < 50; i++)
    world.Step(0.01f);
Console.WriteLine(FormattableString.Invariant(
    $"Y: {ball.Position.Y:F2} m"));

Unzip the example, then dotnet run.

AddPlatformEffector takes the solid-side normal (0, 1) first, then the region’s minimum corner (-5, -1) and maximum corner (5, 1). Gravity = default sets gravity to zero so the circle won’t fall back during the demo. The circle starts below the box and ends above it. The snippet prints:

Text
Y: 0.55 m

The full program

Both runs give the same circle an upward velocity and turn off gravity so you can see the collision response by itself. The only change is whether the static box at the origin is inside a platform effector configured with an upward normal, the direction that faces the solid side.

C#
using RecursiveAI.OnePhysics.Physics2D;
using RecursiveAI.OnePhysics;
using System.Globalization;
 
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
Console.WriteLine("Start Y: -0.50 m");
Console.WriteLine($"Solid box: Y = {Rise(oneWay: false):F2} m");
Console.WriteLine($"One-way box: Y = {Rise(oneWay: true):F2} m");
 
static double Rise(bool oneWay)
{
    var world = new PhysicsWorld2D(new() { Gravity = default });
    world.AddBox(new(10, 0.2f), type: BodyType.Static);
    if (oneWay)
        world.AddPlatformEffector(new(0, 1), new(-5, -1), new(5, 1));
 
    var ball = world.AddCircle(0.2f, at: new(0, -0.5));
    ball.LinearVelocity = new(0, 2);
    for (int i = 0; i < 50; i++) world.Step(0.01f);
    return ball.Position.Y;
}

dotnet run prints:

Text
Start Y: -0.50 m
Solid box: Y = -0.30 m
One-way box: Y = 0.55 m

The solid box stops the circle below it. The one-way box lets it clear the top: the box’s upper face is at Y = 0.10 m, and the circle’s lowest point ends above that face. If the box still blocks the jump, check that the region contains the platform, not just the moving circle.

Variations

Make the bottom solid instead. Replace the effector, circle and velocity statements (keep the AddBox call) in the snippet at the top. Now the circle starts above the box and travels downward through it:

C#
world.AddPlatformEffector(
    new(0, -1), new(-5, -1), new(5, 1));
var ball = world.AddCircle(0.2f, at: new(0, 0.5));
ball.LinearVelocity = new(0, -2);
Text
Y: -0.55 m

The short answer

Add a static box for the platform, then call AddPlatformEffector with an upward unit normal (0, 1) and bounds that contain the box’s origin. The platform still has a collider. The effector removes the blocking contacts on the underside rather than turning the whole collider into a sensor. The example measures the jump-through case by sending the same circle at a solid box and a one-way box.

How do you jump through a 2D platform without making it a sensor?

Keep the platform static and the jumper dynamic. Point the effector normal at the face you want to stay solid. The contact specification says, “A platform is solid toward Axis.” That axis is the normal passed to AddPlatformEffector. Normal here means a vector perpendicular to the solid face. (0, 1) points up, so the contact under the box is the one the effector clears. The effector leaves friction and bounce on the top face unchanged.

The effector matches the surface body’s origin and layers. It doesn’t need a region that covers the jumper’s path, and making the region taller won’t help if it’s positioned around the wrong body. Conversely, a region that covers several matching static or kinematic bodies can affect more than the platform you intended. Keep the bounds around the intended platform when you have solid scenery nearby. The effector doesn’t change collisions between two dynamic bodies.

Why does a one-way platform still block a jump?

First check which way the normal points: if it points down, the circle can pass through from above instead of below. Then check how you move the character. The demo gives the circle a velocity and lets the world step it through the contact. The effector checks which way the contact faces. It doesn’t remember that a body came from below, so a scripted teleport that starts the body inside the platform doesn’t behave like the jump in this example.

If the character moves through but your game still registers a collision event, that’s a separate issue. The same specification says the effector “does not latch bodies through large teleports or suppress collision events.” Use the actual position or velocity to check passage, rather than interpreting every event as a blocked jump. This sample isolates the upward passage; it doesn’t implement player input or a drop-through command.

All onephysics recipes